Java study notes (3): object-oriented

1. Inheritance

  • grammar

    class 父类{}
    class 子类 extends 父类{}
    Only multi-level inheritance, not multiple inheritance, subclasses cannot directly access private members in the parent class, which can be obtained through getter and setter methods
  • Subclass object new instantiation (when)
  1. By default, the constructor of the parent class will be called first, and then the constructor of the subclass will be called to instantiate. Therefore, if the parent class and subclass constructors contain output statements, they will be printed when new
  2. In fact, super() is implied in the subclass construction; the superclass construction is called
  • Overrides of methods ("method overrides")
  1. Methods overridden by subclasses cannot have more restrictive access rights than methods in parent classes
    (eg: parent class --public, and subclasses must also be --public )
  2. After the parent class method is overridden by the child class method, how to access the parent class method?
    ````
  3. Use the super(); keyword, such as: super.print();
  4. In the subclass, the method in the parent class can be accessed directly through the "super.method()" form
    .
  5. .Special case: private-->to default is not overriding, but redefining a new method!
  • property override
  1. Redefine the property after inheritance, which is consistent with the overriding method
  2. In particular: in a subclass :

    1.super.name-->访问父类中的name属性
    2.this.name-->访问“本“类中的name属性
  • The difference between overloading and overriding
  1. Overload:

    01.方法名相同参数类型或个数不同
    02.对权限没有要求
    03.发生在同一个类中
  2. Override:

    01.方法名称,参数类型相同
    02.被覆写的方法(即子类方法)不能拥有(比父类方法)更严格的访问权限
    03.发生在继承类中
  • The role of super keyword
  1. super calls the super class constructor

    **同this一样,该语句必须放在构造方法的句首**
  2. The difference between this and super:

this:

01.属性:调用本类中的属性,若没有,则从父类中寻找
02.方法:调用本类中的方法,若没有,则从父类中寻找
03.构造:调用本领构造,必须放在构造方法的首行
04.特殊:表示当前的对象

super:

01.属性:访问父类中的属性
02.方法:直接访问父类中的方法
03.构造:调用父类构造,必须放在子类的构造方法里的首行,如:super();
  • final keyword

Notice:

1. 使用final声明的“类‘’不能有子类
2. 使用final声明的”方法”不能被子类所覆写
3. 使用final声明的变量即为常量,常量不可修改!
4. public static final同时声明的变量称为全局”常“量

2. Abstract class

  • define rules
    ````
  1. A class containing an abstract method must be an abstract class
  2. Both abstract classes and abstract methods must be declared using the abstract keyword
  3. Abstract methods only need to be declared and not implemented
  4. Abstract classes must be inherited by subclasses, and subclasses (if not abstract classes) must override all abstract methods in the abstract class

  5. Abstract classes cannot be instantiated directly, and subclasses must be instantiated through non- abstract subclasses```
  • Define the format
    ````
  1. Abstract class: abstract class A {}
  2. Abstract method: access permission abstract return value type method name (parameter);
    //There is no method body in an abstract method, no need for {}
  3. The definition method of the subclass: class B extends A{ }
    The subclass must also override all abstract methods in the abstract class
  4. Note: Do not declare abstract methods with the private keyword, because abstract methods must be overridden by subclasses
  5. Essence: Abstract classes have more abstract methods than ordinary classes, except that they cannot directly instantiate objects
    .

    3. Interface

  • composition:
  1. one of the most important concepts
  2. Consists of global constants and public abstract methods

    interface 接口名称 {
    全局常量;
    抽象方法;
    }
  3. The abstract method here must be defined with public access and cannot be changed! ! !
  4. Of course, you can also not write it, the default is public, not default
  5. Usually, we know that global constants are public static final, and abstract methods are public abstract, but this is special, so the definition can be omitted by default. In fact, there are

interface A{
    String s="作者"; //定义全局常量
    void print(); //定义抽象方法
    String getInfo(); //抽象方法不需{}
}
  • use of the interface
  1. class subclass name implements interface A, interface B{ } ! ! !
    A subclass can implement multiple interfaces, class A implements B,C{}
  2. After implementation, all abstract methods must be overridden
  • Inheritance of interfaces
  1. An interface cannot inherit abstract classes, but an interface can inherit multiple interfaces

    interface A{} interface B{} interface C extends B,C{}
  2. That is, an interface can inherit multiple interfaces at the same time, pay attention to inherit extends!!!
  3. An interface cannot implement an interface, it can implement inheritance

    4. Object polymorphism

  • two
  1. Method overloading and overriding
  2. Object polymorphism
    01. Up transformation: subclass object --> parent class object, it will be automatically completed
    02. Down transformation: parent class object --> subclass object, the subclass type of transformation must be specified
  • Format
  1. Object upcast: parent class parent class object = child class instance;

    如:B b =new B(); //子类实例化过程
      A a =b; //向上转型
  2. Object downcast: subclass subclass object = (subclass) parent class instance;

    如:A a =new B(); //向上转型
    B b =(B)a; //向下转型:A-->B
  • Call: To call the subclass's own method, it must only use the instantiated object of the subclass
  • Note: ! ! ! Before the downcast of the object, the upcast of the object must first occur, otherwise an exception will be thrown
  • How to read: A program should go from right to left:

    如:A a =new B();
    是B实例化了一个对象,该B的对象再向上转型成为了A的对象a

    5.instanceof keyword

  • Used to determine who an object is an instance of which class
  1. Object instanceof class --> return boolean value
  • use:
  1. as follows:

    A a1=new B(); 
    A a2=new A();
    01.a1 is the object of A and B at the same time --> the object instantiated by the subclass is also the instance of the parent class and the subclass, so it can be directly transformed up or down
    02.a2 is only the object of A --> at this time Can't downcast, because there is no upcast first --> directly use the parent class to instantiate the object of this class, it is no longer a subclass instance, so it can't be transformed
  2. Perform judgment operation before downcasting to avoid exception
    ```
    A a=new B();
    if( a instanceof B){ }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325048136&siteId=291194637