面向对象09:什么是继承

继承

  • 在Java中,所有的类,都默认直接或者间接继承object

子类 extends 父类

修饰符

  • public:一般方法公有
  • protected
  • default
  • private:一般属性私有
  • 优先级从上到下

父类

public class Person {
    /*
    修饰符:public:一般方法公有
          protected
          default
          private:一般属性私有
          优先级从上到下
    */
    //属性
    private int money = 10_0000_0000;
    //方法
    public void say(){
        System.out.println("说了一句话");
    }

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }
}

子类1

public class Student extends Person{
    //Ctrl+H

}

子类2

public class Teacher extends Person {
}
posted @ 2022-07-01 13:27  夜月明  阅读(25)  评论(0)    收藏  举报