Object-oriented programming has three major characteristics:
Packaging
Inherited
Polymorphic
First, package 1, benefits
Narrow package: That is, the encapsulation of attributes , avoid the risk of arbitrary assignment, improve the security of the data !
① Hide Implementation Details that are not required to be provided externally in a class
② users can only access the data by implementing a custom-made method, it is convenient to add the control logic and restrict the unreasonable operation of the attribute.
③ for easy modification and enhanced maintainability of code
2, concrete implementation steps ★
① Property Privatization (private)
② provides public methods:Set method and get method
Example:
Class person{
Private String name;
Public void SetName (String name) {
THIS.name = name;
}
Public String GetName (){
return name;
}
}
3. Attention
In general, all properties are recommended for encapsulation. Of course, if you want to set the property to read-only, you can just provide a get method, if it is write-only, you can simply provide a set method
Ii. Inheritance 1, concept
When a class does not have a member defined, it has a member of another class, which is known as inheritance
The inherited class is called a parent class or a superclass or base class
Classes that inherit other classes are called subclasses or derived classes
2. Benefits
① improve the reusability of code
② improve the maintainability and extensibility of code
3. Grammar
Class Name extends parent class name {}
4. Features ★
The ① subclass inherits all the properties and methods of the parent class, including the private, but accesses the member's access first, and, of course, through the public way.
② subclass does not inherit the constructor of the parent class
③java supports single inheritance , that is, there can only be one class name behind extends
④ cannot abuse inheritance. Subclasses and parent classes must satisfy is-a relationships
All classes in ⑤java inherit the object class directly or indirectly
The inheritance of members of the ⑥ class is not limited to the direct parent class
5. Subclass Call Parent class constructor ★
① subclasses cannot inherit the constructor of a parent class
The ② subclass must call the constructor of the parent class to initialize the information of the parent class
③ if the subclass does not explicitly call the constructor of the parent class, the system will call the parent class's parameterless constructor by default
④ if the parent class does not have a parameterless constructor, the child class must explicitly call the parent class's parameter constructor, or else the error will be correct!
Syntax: Super (parameter list)
The call to the ⑤ constructor, not limited to the immediate parent class, will go all the way up to the object class
Iii. Polymorphism 1, understanding
Concept: A thing has a variety of manifestations, the previous package and inheritance are to lay the groundwork for polymorphism
Benefits:
① improve the reusability and extensibility of code
② does not need to care about the actual type of each element , just know its common type , call is more convenient, more extensibility
2. Embody ★
overloading and rewriting of the ① method Yes
② Object polymorphism (object's upward transformation) ★
Note: For an object, there is an inconsistency between the compilation type and the run type , which is called: polymorphic
Compilation Type = Type on the left , type detected by the compiler
Run Type = Type on the right , type detected by the interpreter
Related concepts:
Native invocation: Person p = new person ();
polymorphic invocation: Person p = new Student ();
Normal method invocation: Student s = new Student (); S.say ();
Virtual method invocation: Person p = new Student ();p. Say ();
3. Object Transformation ★(1) Upward transformation
Syntax: parent class type reference name = new Subclass type ();
Understanding: References to the parent class point to the Child class object
Characteristics:
① compile look left, run look right
Which members can be called, depends on which members are defined in the compilation type
Run effect, depends on method execution in run type
The ② attribute is not rewritten or polymorphic.
(2) Downward transformation
Syntax: Subclass type reference name = (subclass type) A reference to the parent class;
Characteristics:
① cannot strongly transfer the parent class's object, only strongly to the parent class's reference
A reference to the parent class of the ②, which must point to the object of the target subclass
③ can invoke all members that are inherited from the parent class and defined by the child class itself
4. Polymorphic applications (1) polymorphic arrays
Understanding: The definition type of an array is the parent class type, and the element can be a subclass type
(2) polymorphic parameters
Understanding: The parameter type of the method is the parent class type, and the argument can be a subclass type
instanceof: Used to determine whether an object's run type is a subtype of type XX or XX to the right
notation: a instanceof XX
Results: True|false
Application scenario: Generally put in the if condition for judgment
Javase Basics (5)-Object oriented (5.4 object-oriented three features: encapsulation, inheritance, polymorphism)