The second of the three major features of Java (inheritance)

inherit

  • The essence of inheritance is the abstraction of a certain batch of classes, so as to achieve better modeling of the real world.

  • The extends keyword means "extend". Subclasses inherit the extensions of the parent class.

  • There is only single inheritance in Java, no multiple inheritance! But you can implement multiple interfaces to achieve the purpose of multiple inheritance.

 

  • Inheritance is a relationship between classes and classes. In addition, the relationship between classes and classes includes dependency, composition, aggregation, etc.

  • The inheritance relationship is two classes, one is the subclass (derived class), and the other is the parent class (base class). A subclass inherits from a parent class, represented by the extends key.

  • There should be an "is a" relationship between the subclass and the parent class in a sense.

     

package com.oop.Demo4;
//Person 人  : 父类
public class Person {
    /*访问修饰符:
    public       //公共的
    protected    //受保护的
    default      //不写是默认的
    private      //私有的   一般属性是私有的
     */
    private int money=10_0000_0000;
    //say方法
    public void say(){
        System.out.println("我说一句话");
    }
    public int getMoney() {
        return money;
    }
    public void setMoney(int money) {
        this.money = money;
    }
}

 

package com.oop.Demo4;
//Student is person :子类,派生类
//子类继承了父类,拥有了父类的全部方法!
public class Student extends Person{
    //Ctrl + H 快捷键查看继承
}

program interface

package com.oop;
import com.oop.Demo4.Person;
import com.oop.Demo4.Student;
public class Application {
    public static void main(String[] args) {
        Student s2 = new Student();
        s2.say();
//        s2.setMoney(100000);
        System.out.println(s2.getMoney());
    }
}

 

 

Shortcut key to view inheritance relationship

Ctrl + H shortcut key to view the inheritance relationship of the class

Among them, in Java, all classes inherit the Object class indirectly or directly, and the Object class is the parent class of all classes, or the Object class is a capped class.

 

 

super and this keywords

Super的注意点:
    1、super调用父类方法的构造器,必须放在第一个
    2、super必须只能在子类的方法或者构造方法
    3、super和this 不能同时调用构造方法
Vs this:
    代表对象不同:
        this:代表本身调用者的对象
        super:代表父类对象的引用
    前提
        this:没有继承也可以使用
        super:只能在继承条件下才能使用
    构造方法:
        this:是本类的构造
        super:是父类的构造

 

package com.oop.Demo5;
//Student is person :子类,派生类
//子类继承了父类,拥有了父类的全部方法!
public class Student extends Person {
    private String name ="chenbinbin";
    public void print(){
        System.out.println("This is Student");
    }
    public Student() {
//        super(); //隐藏代码:默认构造器是先调用父类构造器
//        super(); //如果显示调用父类构造器,必须在第一行
        this("Student有参构造器");
        System.out.println("Student无参构造器");
    }
    public Student(String name) {
        this.name = name;
    }
    public void test(){
        print();     //This is Student
        this.print(); //This is Student
        super.print();//This is Person
    }
    public void test(String name){
        System.out.println(name);
        System.out.println(this.name);
        System.out.println(super.name);
    }
}
package com.oop.Demo5;
//Person 人  : 父类
public class Person {
    protected String name ="chen";
    public Person() {
        System.out.println("Person无参构造器");
    }
    //私有的不可以被继承
    public void print(){
        System.out.println("This is Person");
    }
}
package com.oop;
import com.oop.Demo5.*;
public class Application {
    //
        //静态方法 不是方法的重写
        //非静态方法 才能构成重写
    public static void main(String[] args) {
        A a = new A();
        a.test();
        B b = new A();
        b.test();
    }
}

 

method override

重写:需要继承关系,子类继承父类的方法(只能重写方法)
    1.方法名必须相同
    2.参数列表必须相同
    3.访问修饰符:范围可以扩大,不能扩小 public>protected>default>private
    4.抛出异常:可以扩小,但是不能扩大  classNotFoundException-->Exception
重写:子类方法和父类方法必须一致,方法体不同
为什么需要重写方法?
    父类的功能不一定子类需要,或者不能满足子类需求
    Alt + Insert 快捷键--->overrider

 

Guess you like

Origin blog.csdn.net/weixin_40294256/article/details/117606328