当前位置:网站首页>2021-05-07 package inheritance super this

2021-05-07 package inheritance super this

2022-06-23 10:07:00 Deer like deer

encapsulation

High cohesion Low coupling
Property private get/set

The significance of encapsulation :
Improve the security of the program , Protection data
Hide the implementation details of the code
Unified interface
System maintainability increases

Inherit

The essence of inheritance is to abstract a batch of classes , In order to achieve better modeling of the real world
extends It means to extend , A subclass is an extension of a parent

java Class has only singleton inheritance , No more inheritance

Subclass inherits parent , Use keywords extends To express

//Person  people   Parent class 
public class Person {
    
    public void say(){
    
        System.out.println(" Said a word ");
    }
}
package oop.demo5;

// Student  is  people   Subclass 
public class Student extends Person{
    
}

public class Application {
    
    public static void main(String[]args){
    
        Student student = new Student();
        student.say();
    }
}

Private things cannot be inherited ( Unless with get,set extract )

super Be careful

  1. super Call the constructor of the parent class , Must be in the first... Of the construction method
  2. super Must only appear in a subclass's method or constructor
  3. super and this Cannot call constructor at the same time

super VS this

Different objects represented :
this: Calling the object itself
super: Represents the application of the parent object

Premise :
this: You can use without inheritance
super: It can only be used under the condition of inheritance

Construction method :
this(): Call the constructor of this class
super(): Call the construction of the parent class
.

原网站

版权声明
本文为[Deer like deer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230954195650.html