面向对象进阶

构造方法的访问特点

父类的构造方法不会被子类继承。

子类中所有的构造方法默认先访问父类中的无参构造,在执行自己的。

为什么:

子类在初始化的时候,有可能会使用到父类的数据,如果父类没有完成初始化,子类将无法使用父类的数据。

子类初始化之前,一定要调用父类构造方法先完成父类数据空间的初始化。

怎么调用父类构造方法的:

子类构造方法的第一行语句默认都是:super(),不写也存在,且必须在第一行。

如果想要调用父类有参构造,必须手动写super进行调用。

package oop.Extends.a07oopextenddemo07;

public class Person {
    String name;
    int age;

    public Person() {
        System.out.println("父类的无参构造");
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
}
package oop.Extends.a07oopextenddemo07;

public class Student extends Person{
  public Student(){
      super();
      System.out.println("子类的无参构造");
  }
  public Student(String name,int age){
      super(name,age);
  }
}
package oop.Extends.a07oopextenddemo07;

public class Test {
    public static void main(String[] args) {
        Student s=new Student();
        Student s1=new Student("Karry",24);
    }
}

this\super使用总结

this:理解为一个变量,表示当前方法调用者的地址值。

super:代表父类的存储空间。

package oop.Extends.a08oopextendsdemo08;

public class Employee {
    private String id;
    private String name;
    private double salary;

    public Employee() {
    }

    public Employee(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 work(){
        System.out.println("员工在工作");
    }
    public void eat(){
        System.out.println("吃米饭");
    }
}
package oop.Extends.a08oopextendsdemo08;

public class Manager extends Employee{
    private double bouns;
   public Manager(){
   }

    public Manager(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("管理其他人");
    }
}
package oop.Extends.a08oopextendsdemo08;

public class Cook extends Employee{
    public Cook() {
    }

    public Cook(String id, String name, double salary) {
        super(id, name, salary);
    }

    @Override
    public void work(){
        System.out.println("炒菜");
    }
}
package oop.Extends.a08oopextendsdemo08;

public class Test {
    public static void main(String[] args) {
        Manager m=new Manager("h001","karry",15000,9000);
        System.out.println(m.getId()+m.getName()+m.getBouns()+m.getSalary());
        m.work();
        m.eat();
        Cook c=new Cook();
        c.setId("c001");
        c.setName("hy");
        c.setSalary(9000);
        System.out.println(c.getId()+c.getName()+c.getSalary());
        c.work();c.eat();
    }
}

你可能感兴趣的:(java,开发语言)