Java learning summary: 9

inherit

Inheritance is the second main feature of object-oriented, and inheritance is to solve the problem of code reuse, using inheritance can continue to derive new subclasses from existing classes, or you can use subclasses to extend more Operating functions.

Inherited implementation

Inherited format

class 子类 extends 父类 {}

The subclass is actually a means to define the parent class more concretely

Explanation:
1. For extends, it should be translated as an extension, but for convenience of understanding, it is uniformly called inheritance;
2. The subclass is also called a derived class;
3. The parent class is also called a super class (Super Class) .

Inherited implementation:

class Person{
    private String name;
    private int age;
    public void setName(String name){
        this.name=name;
    }
    public void setAge(int age){
        this.age=age;
    }
    public String getName(){
        return this.name;
    }
    public int getAge(){
        return this.age;
    }
}
class Student extends Person{   //Student类继承了Person类
    private String school;  //子类扩充的属性
    public void setSchool(String school){   //扩充的方法
        this.school=school;
    }
    public String getSchool(){     //扩充的方法
        return this.school;
    }
}
public class Test1_1_3_3 {
    public static void main(String args[]){
        Student stu=new Student();  //实例化的是子类
        stu.setName("小关");  //Person类定义
        stu.setAge(19);
        stu.setSchool("西安邮电大学");    //Student类扩充方法
        System.out.println("姓名:"+stu.getName()+",年龄:"+stu.getAge()+",学校:"+stu.getSchool());
    }
}
//结果:
//姓名:小关,年龄:19,学校:西安邮电大学

Inheritance restrictions

1. Java does not allow multiple inheritance, but allows multiple inheritance, Java has a single inheritance limitation.

Wrong inheritance: (multiple inheritance)

class A{}
class B{}
class C extends A,B{}	//一个子类继承两个父类

Multi-level inheritance:

class A{}
class B extends A{}	//B类继承A类
class C extends B{}	//C类继承B类

2. When the subclass inherits the parent class, strictly speaking, it will inherit all operations in the parent class, but all private operations are implicit inheritance, and all non-private operations are explicit inheritance.

class A{
    private String msg;
    public void setMsg(String msg){
        this.msg=msg;
    }
    public String getMsg(){
        return this.msg;
    }
}
class B extends A{//继承自A类
}
public class Test1_1_3_4 {
    public static void main(String args[]){
        B b=new B();
        b.setMsg("HelloWorld");
        System.out.println(b.getMsg());
    }
}
//结果:
//HelloWorld

It can be seen from the above that the msg attribute must exist in class B, and this attribute is inherited from the parent class.
However , you cannot directly access the msg attribute in class B, because it is a private declaration in class A, and you can only use setter or getter methods to access private attributes indirectly.

3. Before the subclass object is constructed, the parent class construction will be called by default (the parameterless structure is used by default) to ensure that the parent class object is instantiated first, and then the subclass object is instantiated.

1. The parent class provides a parameterless construction method:

class A{
    public A(){		//父类提供的无参构造方法
        System.out.println("A类的构造方法");
    }
}
class B extends A{//继承自A类
    public B(){
        System.out.println("B类的构造方法");
    }
}
public class Test1_1_3_4 {
    public static void main(String args[]){
        new B();
    }
}
//结果:
//A类的构造方法
//B类的构造方法

2. The parent class does not provide a parameterless construction method:

class A{
    public A(String title){     //父类提供的有参构造方法
        System.out.println("A类的构造方法"+title);
    }
}
class B extends A{//继承自A类
    public B(String title){
        super(title);   //明确调用父类构造,否则将出现编译错误
        System.out.println("B类的构造方法");
    }
}
public class Test1_1_3_4 {
    public static void main(String args[]){
        new B("!!!");   //实例化子类对象
    }
}
//结果:
//A类的构造方法!!!
//B类的构造方法
49 original articles published · 25 praised · 1540 visits

Guess you like

Origin blog.csdn.net/weixin_45784666/article/details/104266605