-
继承中:构造方法的访问特点
-
代码实现
-
为成员进行赋值
-
总结
-
-
this、super使用总结
-
this
-
super
-
关键字
-
用this访问构造
-
使用场景
要给数据做默认值时使用
-
-
-
练习
-
员工类
package yuangong; public class yuangong { private String id; private String name; private int gz; public yuangong() { } public yuangong(String id, String name, int gz) { this.id = id; this.name = name; this.gz = gz; } 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 int getGz() { return gz; } public void setGz(int gz) { this.gz = gz; } public void eat(){ System.out.println("员工在吃饭"); } public void word(){ System.out.println("员工在工作"); } }
-
厨师类
package yuangong; public class chushi extends yuangong{ public chushi() { } public chushi(String id, String name, int gz) { super(id, name, gz); } @Override public void word(){ System.out.println(getName()+"在炒菜"); } }
-
经理类
package yuangong; public class jingli extends yuangong{ private int guanli; public jingli() { } public jingli(int guanli) { this.guanli = guanli; } public jingli(String id, String name, int gz, int guanli) { super(id, name, gz); this.guanli = guanli; } public int getGuanli() { return guanli; } public void setGuanli(int guanli) { this.guanli = guanli; } @Override public void word() { System.out.println(getName()+"在管理其他人"); } }
-
测试
package yuangong; public class test { public static void main(String[] args) { //创建厨师对象并赋值 chushi c = new chushi("heima001","蔡徐坤",1000); //使用类中的方法 c.word(); c.eat(); System.out.println(c.getName()+","+c.getId()+","+c.getGz()); //创建经理对象 jingli j = new jingli(); //赋值 //工资 j.setGz(10000); //奖金 j.setGuanli(20); //id j.setId("heima002"); //姓名 j.setName("吴亦凡"); //使用类中的方法 j.word(); j.eat(); System.out.println(j.getName()+","+j.getId()+","+ j.getGz()+","+j.getGuanli()); } }
-
-
练习2