一、继承中构造方法的访问特点:
1.父类中的构造方法不会被子类继承
2.子类中所有的构造方法默认先访问父类中的无参构造,再执行自己
为什么:
(1)子类在初始化时,有可能会使用父类中的数据,如果父类没有完成初始化,子类将无法使用父类的数据
(2)子类初始化之前,一定要调用父类构造方法先完成父类数据空间的初始化
怎么调用父类构造方法的:
(1)子类构造方法的第一行语句默认是:super(),不写也存在,且必须在第一行
super()表示调用父类的无参构造
(2)如果想调用父类有参构造,必须手动写super进行调用
例如:
父类:Person
package a08extendsdemo5;
public class Person {
String name;
int age;
public Person() {
System.out.println("父类的无参构造");
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
子类:Student
package a08extendsdemo5;
public class Student extends Person {
public Student(){
//子类构造方法中有一个隐藏是super()去访问父类的无参构造
super();
System.out.println("子类的无参构造");
}
public Student(String name,int age){
//想给s直接赋值张三23
super(name,age);//相当于调用父类带参构造
}
}
测试类:Test
package a08extendsdemo5;
public class Test {
public static void main(String[] args) {
//创建学生对象
//调用无参构造创建对象:
Student s1=new Student();
//调用带参构造创建对象
Student s=new Student("zhangsan",23);
System.out.println(s.name+", "+ s.age);
}
}
运行结果:
二、this、super使用总结
this:理解为一个变量,表示当前方法调用者的地址值(图片来自哔站黑马程序员)
示范:Student
package a09extendsdemo6;
public class Student {
String name;
int age;
String school;
//需求:默认为哔站大学
public Student() {
//表示调用本类其他构造方法
//细节:虚拟机不会添加super();
//因为通过this找到下面带参构造,带参构造第一行会默认一个super();
//且这个this必须写在第一行
this(null,0,"哔站大学");
}
public Student(String name, int age, String school) {
this.name = name;
this.age = age;
this.school = school;
}
}
测试类:Test
package a09extendsdemo6;
public class Test {
public static void main(String[] args) {
Student s=new Student();
System.out.println(s.name+", "+s.age+", "+s.school);
}
}
运行结果:
练习:带有继承结构的标准JavaBean类
经理:成员变量:工号,姓名,工资,管理奖金
成员方法:工作(管理其他人),吃饭(吃米饭)
厨师:成员变量:工号,姓名,工资
成员方法:工作(炒菜),吃饭(吃米饭)
父类:Employee
package a10extendsdemo10;
public class Employee {
private String id;
private String name;
private double salsry;
public Employee() {
}
public Employee(String id, String name, double salsry) {
this.id = id;
this.name = name;
this.salsry = salsry;
}
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 getSalsry() {
return salsry;
}
public void setSalsry(double salsry) {
this.salsry = salsry;
}
//工作:
public void work(){
System.out.println("员工在工作");
}
//吃饭
public void eat(){
System.out.println("吃米饭");
}
}
子类1:经理Manager
package a10extendsdemo10;
public class Manager extends Employee {
private double bouns;
//空参构造
public Manager(){
}
//带全部参数的构造:父类+子类
public Manager(String id, String name, double salsry, double bouns) {
super(id, name, salsry);
this.bouns = bouns;
}
public double getBouns() {
return bouns;
}
public void setBouns(double bouns) {
this.bouns = bouns;
}
public void work(){
System.out.println("管理其他人");
}
}
子类2:厨师Cook
package a10extendsdemo10;
public class Cook extends Employee{
public Cook() {
}
public Cook(String id, String name, double salsry) {
super(id, name, salsry);
}
//get/set在父类中有现成的,所以不用写了
public void work(){
System.out.println("厨师在炒菜");
}
}
测试类:Test
package a10extendsdemo10;
public class Test {
public static void main(String[] args) {
//创建对象并赋值调用
Manager m=new Manager("kongge001","鹿晗",15000,7777);
System.out.println(m.getId()+", "+m.getName()+", "+m.getSalsry()+", "+m.getBouns());
m.work();
m.eat();
Cook c=new Cook();
c.setId("kongge002");
c.setName("天空");
c.setSalsry(8000);
System.out.println(c.getId()+", "+c.getName()+", "+c.getSalsry());
c.work();
c.eat();
}
}
运行结果:
标签:Java,String,构造方法,Student,父类,super,public,name From: https://blog.csdn.net/TNTxd/article/details/136872444