Why learn packaging ?

There is no perfect verification mechanism for the attribute data of existing objects , Can pass at will “ Object name . Attribute name ” Access in the form of , It may eventually lead to incomplete data , unsafe .
Attribute hiding :private( Private )
private Is a modifier , Can decorate attributes , method , Construction method , Meaning and public contrary private Modified content , It can only be used in the internal code of this class , Cannot access outside class
Hide attributes : General code development , All properties of the object are private
Provide public attribute access channels :set( entrance )—get( Export )
1. Provide public methods to set property values : ① grammar : public void set Attribute name ( Parameter type Parameter name ){ Code for setting attribute value } ② The method name is usually set +
Attribute name initial capitalization setName setAge ③ Parameter type : Determined by the set attribute type ④ case : by name Property setting value public void
setName(String name){ this.name = name; }
be careful : Attribute name must be lowercase , Must consist of more than two characters
2. Provide public methods to get attribute values : ① grammar : public return type get Attribute name (){ Return the code of the specific attribute value } ② The method name is usually get +
Attribute name initial capitalization getName getAge ③ return type : Determined by the specific attribute value obtained ④ case : obtain name Value of attribute public String
getName(){ return this.name; }
How to access attributes after encapsulation ?
// Set attribute value Object name .set Attribute name ( Specific value ); student.setName("tom"); // Get attribute value Object name .get Attribute name ();
student.getName();
What is inheritance ?
Between classes embodied by inheritance "is-a" relationship Water cup It's a kind of container Pen It's a kind of pen dog It's a kind of animal A is-a B If A is-a B
Relationship established , be A and B There is an inheritance relationship between ; Namely :A inherit B,A Called subclasses ,B Called parent class Subclass inherit Parent class (2) grammar : class A extends B{}
A inherit B,A Is a subclass ,B Is a parent class (3) By inheritance , Subclasses can directly use properties and member methods in the parent class (4) be careful : Inheritance is a kind of inheritance between classes is-a relationship ;
You cannot inherit from another class in order to obtain properties or methods in another class (5) Subclasses can inherit properties and methods in the parent class through inheritance relationships ; At the same time, you can also define your own unique attributes and methods .
matters needing attention :
a. Definition in subclass and method name of parent class , Same method as parameter list , But the return value type is different , Compilation error class Animal{ public void eat(){ } }
class Dog extends Animal{ // This method cannot override the eat method , Compilation error public int eat(){ return
0;// } } b. Definition in subclass and method name of parent class , Same return value , But the formal parameter list has different methods , Compilation passed , Operation results depend on parameters class Animal{ public
void eat(){ } } class Dog extends Animal{ // Is a special method overload public void eat(int n){ }
}
With inheritance , What content in the parent class can be inherited by a subclass ?
(1) Through inheritance relationship , A subclass cannot inherit a constructor from a parent class reason : The method name of the constructor needs to be consistent with the class name of this class , Subclasses and superclasses have their own class names ;
So every class , Whether it is a subclass or a parent class, you need to define the construction method in this class according to your class name (2) Through inheritance relationship , Whether the subclass can inherit the properties and member methods in the parent class , Depends on access decoration a.
Java Access modifiers in total 4 individual b. An access scope or permission represented by an access modifier
Access modifier this class is a subclass of the same package but not the same package
private√
default√√
protected√√√
public√√√√
What is polymorphism ?
A reference of a parent type stores objects of a child type .
Polymorphic grammar :
Parent class name Reference name = new Subclass class name (); Parent type Subtype quote object be careful : A reference of a parent type can store sub type objects of different types (1)
Invoke properties and methods with references to parent types , Only properties and methods in the parent class can be called --> Compile phase detection (2) Runtime ,jvm Automatically detect whether the subclass overrides the methods in the parent class ;
If the subclass overrides the method in the parent class , Then call the method after subclass coverage first ; If there is no override, the method in the parent class will be called directly .
Conversion between references :
(1) Reference of parent type Reference assigned to subtype , Need to cast Large type Small type a. grammar : Subclass class name Reference name = ( Subclass class name ) Reference name of parent type ; b.
The results are divided into two cases : I. If the actual storage object type is consistent with the type to be converted , Compilation passed , Operation also passed Animal a = new Dog(); Dog d =
(Dog)a; II. If the actual storage object type is inconsistent with the type to be converted , Compilation passed , Operation error , The error message is : java.lang.ClassCastException;
Type conversion exception Animal a = new Cat(); Dog d = (Dog)a; (2) Subtype reference Assign to Reference of parent type , Can be assigned directly , No forced rotation
-》 Reflect the application of polymorphism Small type Large type Dog d = new Dog(); Animal a =d; // Application of embodied polymorphism (3)
There is no inheritance relationship between the conversion parties , Cast is not allowed , Compilation error -> Both sides of the conversion class Animal a = new Dog(); Person p =
(Person)a; // Compilation error ,Person and Animal There is no inheritance relationship , There is no possibility of success
Application of polymorphism :
(1) Polymorphic application form parameters : If the formal parameter type is the parent type , Then all subtypes can be passed as arguments . (2) Polymorphism is applied to the return value :
If the return value type is the parent type , Then all subtypes can be returned as return values .
be careful :

Technology