m0_70564251 2022-05-05 14:03 采纳率: 100%
浏览 49
已结题

java 继承方法 这个问题的编写谁会

img


上面的解题代码请求请求,不是很会,想找一个正确的解题思路进行模仿的思考

  • 写回答

3条回答 默认 最新

  • 溪风沐雪 2022-05-05 15:14
    关注

    给个例子做参考,文件要保存为MyTest.java,或者修改代码里的MyTest为你的文件名:

    class Staff{
        String name;
        int age;
        String sex;
        public Staff(String name, int age, String sex){
            this.name =name;
            this.age = age;
            this.sex = sex;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public String getSex() {
            return sex;
        }
        public void setSex(String sex) {
            this.sex = sex;
        }
        public void showInfo(){
            System.out.println("姓名:"+this.name+" 年龄:"+this.age+" 性别:"+this.sex);
        }
    } 
    
    class Manager extends Staff {
        String post;
        int salary;
    
        public Manager(String name, int age, String sex, String post, int salary) {
            super(name, age, sex);
            this.post = post;
            this.salary = salary;
        }
    
        public String getPost() {
            return post;
        }
    
        public void setPost(String post) {
            this.post = post;
        }
    
        public int getSalary() {
            return salary;
        }
    
        public void setSalary(int salary) {
            this.salary = salary;
        }
        public void showInfo(){
            System.out.println("姓名:"+this.name+" 年龄:"+this.age+" 性别:"+this.sex+" 职务:"+this.post+" 年薪:"+this.salary);
        }
    
    }
    
    class Employee extends Staff {
        String department;
        int monthly;
        public Employee(String name, int age, String sex, String department, int monthly) {
            super(name, age, sex);
            this.department = department;
            this.monthly = monthly;
        }
        public String getDepartment() {
            return department;
        }
        public void setDepartment(String department) {
            this.department = department;
        }
        public int getMonthly() {
            return monthly;
        }
        public void setMonthly(int monthly) {
            this.monthly = monthly;
        }
        public void showInfo(){
            System.out.println("姓名:"+this.name+" 年龄:"+this.age+" 性别:"+this.sex+" 部门:"+this.department+" 月薪:"+this.monthly);
        }
    }
    
    public class MyTest{
        public static void main(String[] args) {
            Manager manager = new Manager("张三", 38, "男", "总经理", 3000000);
            Employee employee = new Employee("李四", 28, "女", "研发部", 5300);
            manager.showInfo();
            employee.showInfo();
        }
    }
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 5月13日
  • 已采纳回答 5月5日
  • 修改了问题 5月5日
  • 创建了问题 5月5日

悬赏问题

  • ¥15 CATIA有些零件打开直接单机确定终止
  • ¥15 请问有会的吗,用MATLAB做
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 ARIMA模型时间序列预测用pathon解决
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址