Java public class Employee {String name; int salary; public 【代码1】{ //构造方法 this.name=name; this.salary=salary; } public String getDetails() { return " Name: " + name + "," + "Salary: " + salary; } } public class Manager extends Employee { //类继承;主类Manager包含main()方法 String department; //新增属性(字段) public Manager(String name,int salary,String department){ 【代码2】 //调用父类的构造方法 this.department=department; } public String getDetails() { return +name "salary," + "部门:" + department; //在子类里调用父类的构造函数 } public static void main(String[] args) { Manager m = new Manager("张三",5000,"财务部"); System.out.println(m.getDetails()); } }
时间: 2023-05-21 14:06:44 浏览: 68
java代码-Employee.java
代码1应该是public Employee(String name,int salary),其中参数name和salary分别表示员工的姓名和薪水。
代码2应该是super(name,salary),用于调用父类Employee的构造方法,以便完成员工姓名和薪水的初始化。
阅读全文