Bored to rely on JavaEE from entry to abandon (7) inheritance

table of Contents

1. Inheritance

1. Points for inheritance and use

Two.instanceof keyword

3. Method rewriting (override)

Four.final keywords

Five. Combination


I won't talk about the definition, let's talk about the main points, and run the code.

1. Inheritance

1. Points for inheritance and use

1. The parent class is also called super class, base class, derived class, etc.
2. There is only single inheritance in Java, not multiple inheritance like C++. Multiple inheritance can cause confusion, making the inheritance chain too complicated and the system difficult to maintain.
3. There is no multiple inheritance of classes in Java, but multiple inheritance of interfaces.
4. The child class inherits the parent class, and can get all the properties and methods of the parent class (except the construction method of the parent class), but not necessarily directly accessible (for example, the private properties and methods of the parent class).
5. If you don't call extends when defining a class, its parent class is: java.lang.Object.

Two.instanceof keyword

Grammatical structure: object instanceof class name

Role: Determine whether an object belongs to a certain class

Return value: Boolean

3. Method rewriting (override)

By overriding the method of the parent class, the subclass can replace the behavior of the parent class with its own behavior. Method rewriting is a necessary condition for achieving polymorphism. (The name of the rewritten function and the parameter list must be the same, and the two-overload parameter list is different!)
The rewriting of the method needs to comply with the following three points:

1. "=": The method name and parameter list are the same.
2. "<=": Return value type and declared exception type, the subclass is less than or equal to the parent class.
3.">=": Access authority, the child category is greater than or equal to the parent category.

class Person{
    int age;
    String name;

    Person(){

    }

    public void hello(){
        System.out.println("hello,sons!");
    }
}

public class Student extends Person{
    String sex;

    public void hello(String str){
        System.out.println("hello,"+str);
    }
    public Person object(){ //如果这里Person改成Object的话,就会报错,因为超出了父类的范围!
        return new Person();
    }

    Student(int age,String name,String sex){
        this.age=age;
        this.name=name;
        this.sex=sex;
    }

    public static void main(String[] args) {
        Student stu1=new Student(22,"hh","女");
        System.out.println(stu1.name);
        System.out.println(stu1 instanceof Person);
        stu1.hello("Dad!");
        Person stu2=stu1.object();
        System.out.println(stu2 instanceof Student);

    }
}

Four.final keywords

The role of the final keyword:
1. Modified variable: The variable modified by him cannot be changed. Once the initial value is assigned, it cannot be reassigned.
final int MAX_ SPEED = 120;
2. Modification method: This method cannot be overridden by subclasses. But it can be overloaded!
final void study(){}
3. Modified class: Modified class cannot be inherited. For example: Math, String, etc.
final class A{}

Five. Combination

The combination is more flexible. Inheritance can only have one parent class, but the combination can have multiple attributes. Therefore, some people claim that "combination is better than inheritance, and inheritance is not necessary in development", but it is not recommended that you go to extremes.
In addition to code reuse, inheritance can also facilitate us to model transactions. Therefore, it is recommended to use inheritance for the "is-a" relationship, and to use the combination for the "has-a" relationship.
For example: In the above example, the logic of Student is a Person is fine, but there is a problem with: Student has a Person. At this time, it is obvious that the inheritance relationship is more appropriate.
Another example: the relationship between the notebook and the chip is obviously a "has-a" relationship, and it is better to use a combination.

eg:

class Chip{
    public void output(){
        System.out.println("我是芯片!");
    }
}

public class Computer {

    Chip chip=new Chip();//将其他类对象放进来当作此类的一个属性,非继承而是组合

    public static void main(String[] args) {
        Computer computer=new Computer();
        computer.chip.output();

    }
}

 

 

Guess you like

Origin blog.csdn.net/weixin_44593822/article/details/115323848