1.继承中构造方法的访问特点是什么?
●子 类不能继承父类的构造方法,但是可以通过super调用
●子类构造方法的第一行, 有一个默认的super();
●默认先访问父类中无参的构造方法,再执行自己。
●如果想要方法文父类有参构造,必须手动书写。
package demo3; /** * ClassName: Employer * Package: demo3 * Description: * * @Author: 20213923ysf * @Create: 2023/7/21 - 22:54 */ public class Employer { private String id; private String name; private double salary; public Employer() { } public Employer(String id, String name, double salary) { this.id = id; this.name = name; this.salary = salary; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } public void eat(){ System.out.println("吃饭(吃米饭)"); } public void work(){ System.out.println("员工在工作"); } }
package demo3; /** * ClassName: cook * Package: demo3 * Description: * * @Author: 20213923ysf * @Create: 2023/7/21 - 22:54 */ public class Cook extends Employer{ public Cook() { } public Cook(String id, String name, double salary) { super(id, name, salary); } @Override public void work() { System.out.println("厨师正在炒菜"); } }
package demo3; /** * ClassName: manger * Package: demo3 * Description: * * @Author: 20213923ysf * @Create: 2023/7/21 - 22:54 */ public class Manger extends Employer{ private double bouns; //空参构造 public Manger() { } //带全部参数的构造 //父类+子类 public Manger(String id, String name, double salary, double bouns) { super(id, name, salary); this.bouns = bouns; } public double getBouns() { return bouns; } public void setBouns(double bouns) { this.bouns = bouns; } @Override public void work() { System.out.println("经理管理其他人"); } }
1 package demo3; 2 3 /** 4 * ClassName: Test 5 * Package: demo3 6 * Description: 7 * 8 * @Author: 20213923ysf 9 * @Create: 2023/7/21 - 23:05 10 */ 11 public class Test { 12 public static void main(String[] args) { 13 Manger m = new Manger("2021","张三", 10000,8000); 14 System.out.println(m.getId()+","+m.getName()+","+m.getSalary()+","+m.getBouns()); 15 m.eat(); 16 m.work(); 17 18 Cook c = new Cook("2022","李四",5000); 19 System.out.println(c.getId()+","+c.getName()+","+c.getSalary()); 20 c.eat(); 21 c.work(); 22 c.setId("2023"); 23 c.setName("王五"); 24 c.setSalary(8000); 25 System.out.println(c.getId()+","+c.getName()+","+c.getSalary()); 26 //输出 27 /*2021,张三,10000.0,8000.0 28 吃饭(吃米饭) 29 经理管理其他人 30 2022,李四,5000.0 31 吃饭(吃米饭) 32 厨师正在炒菜 33 2023,王五,8000.0*/ 34 35 } 36 }
标签:salary,name,构造方法,继承,double,id,super,public,String From: https://www.cnblogs.com/louwangshayu/p/17572604.html