Java object oriented Foundation

Object oriented has always been a very popular idea. Its essence lies in its three characteristics: encapsulation, inheritance and polymorphism. This paper briefly talks about the object-oriented basis of Java in these three aspects.

1. Packaging:

Encapsulation, as the name suggests, is to hide the properties and methods of some objects in this class, and other classes cannot access these encapsulated properties and methods of this class. That is, these methods and attributes are only for this class of services. In addition to this kind of service, encapsulation can only expose the services they want to provide to others, and do not want others to see or call some special underlying services. In general, encapsulation has the following functions or benefits:

2. Succession:

The term inheritance is also very vivid, that is, the subclass obtains the common or protected properties and methods of the parent class, and then expands it again to further improve the class code and functions. Inheritance intuitively saves us the amount of code. We don't need to write the existing methods and properties in the parent class again, but inheritance is not proposed to save the amount of code, but because it shows the relationship between classes. Generally, we inherit only when there is an indirect or direct relationship between the two classes, not just to save code.

1. Main functions:

2. There are also some points that need to be paid attention to:

Java only supports single inheritance and does not support multiple inheritance. Multiple inheritance will lead to functional disorder. For example, multiple parent classes have the same method, but at the same time inheritance will cause the problem of not knowing which method to inherit. However, Java still retains the multi inheritance mechanism of C + +, which is called multi implementation, that is, it supports multi inheritance (Implementation) on the interface. However, when inheriting, you should pay attention to the relationship between is and a, that is, each subclass object is a parent object. The subclass object is a a parent object.

3. Super and this in inheritance

1. The super pointer refers to the object of the parent class

be careful:

2. This represents the object to which the function belongs

Simply put, this refers to which function this is in, which object this function is, and which object this refers to. However, note that this is not the current object, but points to the current object. It is similar to the application of a pointer this:

class PersonDemo2{
    private int age;
    private String name;

    private PersonDemo2(){
        System.out.println("no params ");
    }

    private PersonDemo2(String name){
        this();
        this.name=name;
    }

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

The mutual call of this in the constructor is not allowed to call each other repeatedly, which will cause an endless loop

3. Polymorphism:

Polymorphism is not only a complex function in object-oriented, but also an extremely easy-to-use function. To summarize polymorphism in one sentence, we can say, "one interface, multiple implementations". It is the multiple forms of the same thing. Polymorphism allows the object of a subclass to be used as the object of the parent class. The reference of a parent type points to the object of its subtype, and the method invoked is the method of the subtype. Here, the code of reference and calling methods has been determined before compilation, and the object pointed to by the reference can be bound dynamically during runtime.

1. His specific embodiment is:

Here, the reference of the parent class points to the instance of the child class, so an automatic upward type conversion will occur, that is, the object of the child class will be automatically promoted to the object of the parent class, but this will cause the loss of precision, that is, the characteristics of the child class will be destroyed, and the unique methods and properties of the child class can no longer be called. However, since upward type conversion is possible, there must be downward type conversion, that is, to convert the parent object into a child object. However, this conversion can only occur in the parent object because of the upward type conversion object, and can not convert a parent object into a child object for no reason.

2. Premise of polymorphism:

3. Dynamic binding of parameters:

To put it simply, the compiler looks at the right and the runtime looks at the left (the son acts as the father. When people compile, they see you as a father, so it depends on whether your father has this method, and when running, the son will do what he will do instead of what his father does). In a word, "compile declaration check and run dynamic binding"

Variables are always aligned with reference types (aligned to the left). Whether they are static or non-static, it's easy to say that they are bound to classes. Naturally, it depends on references. Frankly, member variables have no dynamic binding of methods

Always keep up with reference types, because it is static binding, static method and class binding

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>