Three characteristics of object-oriented programming: encapsulation inheritance polymorphism

Package

Some information of the class is hidden inside the class, and no external program is allowed to directly access it, and the operation and access to the hidden information can be realized through the methods provided by the class. In other words, hide the information of the object and leave the access interface. To achieve the goal directly by manipulating the class object, there is no need to have a good understanding of the specific implementation, and the specific implementation of the class attributes and methods is invisible to the outside. It is not only convenient but also protective.

for example

public class Student {
    
    
    private int id;//id属性私有化
    private String name;//name属性私有化

    //获取id的方法
    public int getId() {
    
    
        return id;
    }

    //设置id的方法
    public void setId(int id) {
    
    
        this.id = id;
    }

    //获取name的方法
    public String getName() {
    
    
        return name;
    }

    //设置name的方法
    public void setName(String name) {
    
    
        this.name = name;
    }
}

 
 

inherit

Inheritance is a technology that uses the definition of an existing class as a basis to create a new class. The definition of a new class can add new data or new functions, or use the functions of the parent class, but cannot selectively inherit the parent class. The previous code can be reused very easily by using inheritance.

Remember the following 3 points about inheritance:

  • The subclass has all the properties and methods of the parent class object (including private properties and private methods), but the private properties and methods in the parent class are inaccessible to the subclass, but only owned.

  • Subclasses can have their own properties and methods, that is, subclasses can extend the parent class.

  • Subclasses can implement the methods of the parent class in their own way.
     
     

Polymorphism

Polymorphism, as the name implies, means that an object has multiple states. Specifically, the reference of the parent class points to the instance of the child class.

Definition of polymorphism

Refers to the specific type pointed to by the reference variable defined in the program and the method call issued through the reference variable is not determined at the time of programming, but is determined during the running of the program , that is, the instance object of which class a reference variable will point to, The method in which the method call issued by the reference variable is the method implemented in the class must be determined during the running of the program.

Advantages of polymorphism

Because the specific class is determined when the program is running, the reference variable can be bound to a variety of different class implementations without modifying the source code, which will cause the specific method called by the reference to change accordingly, that is, not to modify The program code can change the specific code bound when the program is running, so that the program can select multiple running states, which is polymorphism.

Characteristics of polymorphism

  • There is an inheritance (class)/implementation (interface) relationship between object type and reference type;
  • The method in which the method issued by the reference type variable calls the method in which class must be determined during the running of the program;
  • Polymorphism cannot call the method "only exists in the child class but does not exist in the parent class";
  • If the subclass overrides the method of the parent class, the method that the subclass covers is actually executed. If the subclass does not override the method of the superclass, the method of the superclass is executed.

Implementation of polymorphism

There are two forms of polymorphism in Java: inheritance (rewriting the same method by multiple subclasses) and interface (implementing the interface and overriding the same method in the interface).
 

for example

public class Animal {
    
    
    int id = 1;
    public void name(){
    
    
        System.out.println("动物");
    }
}
class Cat extends Animal{
    
    
    int id = 2;
    public void name(){
    
    
        System.out.println("猫");
    } 
}
//子类对象的多态性使用前提:1.要有类的继承 2.要有子类对父类方法的重写
public class Test {
    
    
    public static void main(String[] args) {
    
    
    	//子类对象的多态性:父类的引用指向子类对象
          Animal a = new Cat();//向上转型
          
        //虚拟方法调用:通过父类的引用指向子类对象的实体,调用方法时,实际执行子类重写父类的方法
          a.name();//输出:猫
          
        //输出的是Person对象的id属性
          System.out.println(p.id);//输出:1
    }
}

Guess you like

Origin blog.csdn.net/weixin_43901865/article/details/112672546