Summary of object-oriented knowledge points in Java

1、 Concepts of objects and classes

Class: an abstraction of a class of things with the same properties and methods.

Object: a specific thing that represents some of its own properties and methods.

2、 Relationships between classes (objects)

Association (combination, aggregation), inheritance, dependency, implementation

3、 Object oriented design idea

Object oriented -- "which classes are considered, object --" classes and objects have attributes and methods -- "classes and the relationship between classes

4、 Overloading, overriding, and hiding

1). Overload:

Method overloading refers to multiple methods with the same method name but different parameter types or parameter numbers, regardless of the return value type and modifier

The first five are overloaded with each other. Although the return value types of the first and sixth are different, the parameters are the same, so the first and sixth parameters are not overloaded

2). Override:

When a subclass inherits from the parent class, if the method name, parameter type and number of parameters of the subclass are exactly the same as those of the parent class, it is considered that the subclass overrides the methods of the parent class.

Method override rule:

3). Hide:

Hiding is for member variables and static methods of the parent class. If the subclass declares the same variable name or static method as the parent class (the same method name, the same parameter list and the same return type), the parent class member variables and static methods are hidden. The following example is helpful for understanding:

Output results:

1 4 3 2 4 33 44

If there are static methods or variables with the same name in the subclass, the parent class will be hidden. If there are static methods or variables with the same name in the subclass, the static methods or variables in the parent class will be hidden. At this time, the subclass calls its own static methods or variables in the subclass; If there is no static method or variable with the same name in the subclass, the static method or variable in the parent class will be called; The parent class always calls its own static methods and variables.

5、 Encapsulation:

Encapsulation is to combine the attributes and operations of an object into an independent whole and hide the implementation of the internal operations of the object. Users only need to access the object through its external methods without knowing its internal implementation details.

advantage:

6、 Inheritance:

Inheritance is the process that one object obtains the properties of another object. The keywords are extensions and implements.

1). Is-a relationship (one object belongs to another object):

Mode 1 Use extensions to implement inheritance:

Mode II Implement inheritance with implements:

No matter in mode 1 or mode 2, we can check with the instanceof keyword: MMAL is an animal (mammal is also an animal); Dog is both a mammal and an animal (dog is both a mammal and an animal).

Output results:

true true true

2). Has-a relationship (one object contains some properties of another object):

Benz has a spend attribute, but Benz is not spend

7、 Polymorphism:

Three necessary conditions for polymorphism: inheritance, rewriting and parent class reference pointing to child class objects.

1). Upward Transformation:

We can instantiate a dog object with dog d = new dog(); We can also use animal d = new dog(), The latter is the upward transformation (the parent reference points to the child object). For the object D created in the above two ways, call d.eat(); The output results are all dog eating, This reflects the polymorphism of Java. Objects created by upward transformation will lose methods and variables different from those of the parent class (they cannot be used to call methods and variables unique to subclasses).

2). For example:

Output results:

A and A // ① A and A // ② A and D // ③ B and A // ④ B and A // ⑤ A and D // ⑥ B and B // ⑦ B and B // ⑧ A and D // ⑨

The first three are relatively simple and not easy to make mistakes. Take a look at the following:

summary

The above is all about the sorting of java object-oriented knowledge points. I hope the content of this article can bring some help to you in learning or using Java. If you have any questions, you can leave a message.

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
分享
二维码
< <上一篇
下一篇>>