Document directory
- Four encapsulated Permissions
- Four encapsulated Permissions
.
### Encapsulation
Encapsulation
1. encapsulation is to privatize attributes and provide public methods to access private attributes. The carrier that encapsulates the attributes and behaviors of objects is classes. Classes usually hide the Implementation Details of objects, this is the idea of encapsulation.
Encapsulation in life, such as a lamp, you only need to know how to turn on the switch, the light is on, and the lamp is off, but you do not know the principle, because the complicated lines are encapsulated by the beautiful appearance of the desk lamp.
2. Why is encapsulation required?
Through encapsulation, data access to attributes can be restricted, and the maintainability of the program is increased.
Because the value method and value assignment method hide the implemented changes, the classes that read or modify this attribute are not affected. This avoids large-scale modifications and enhances the maintainability of the program.
Public class demo {
Private int I; // Private Property
Private string STR;
// Public property method and access private property
Public int Geti (){
Return I;
}
Public void SETI (int I ){
This. I = I;
}
Public String getstr (){
Return STR;
}
Public void setstr (string Str ){
This. Str = STR;
}
}
Four encapsulated Permissions
1. PRIVATE: if the member variables and member methods of this class are modified using private, the member variables or member methods can only be used in this class, classes in subclass or other packages are invisible.
2. By default, this permission cannot be transferred across packages. This member's face change or member method can only be called in this class or its subclass.
3. protected by protected, if the member variable or member method is modified by protected, the member variable or member method is visible to the class or subclass of the same package. If you want to cross-package, it must be visible in the subclass.
4. Public public: if the member variables or member methods are modified using public, they are visible anywhere.
The permission levels of these four permission modifiers are low ----------> high
Private-> default-> protected-> Public
### Inheritance
### Polymorphism
.
.
.
.
.
.
.
.
.
Encapsulation
1. encapsulation is to privatize attributes and provide public methods to access private attributes. The carrier that encapsulates the attributes and behaviors of objects is classes. Classes usually hide the Implementation Details of objects, this is the idea of encapsulation.
Encapsulation in life, such as a lamp, you only need to know how to turn on the switch, the light is on, and the lamp is off, but you do not know the principle, because the complicated lines are encapsulated by the beautiful appearance of the desk lamp.
2. Why is encapsulation required?
Through encapsulation, data access to attributes can be restricted, and the maintainability of the program is increased.
Because the value method and value assignment method hide the implemented changes, the classes that read or modify this attribute are not affected. This avoids large-scale modifications and enhances the maintainability of the program.
Public class demo {
Private int I; // Private Property
Private string STR;
// Public property method and access private property
Public int Geti (){
Return I;
}
Public void SETI (int I ){
This. I = I;
}
Public String getstr (){
Return STR;
}
Public void setstr (string Str ){
This. Str = STR;
}
}
Four encapsulated Permissions
1. PRIVATE: if the member variables and member methods of this class are modified using private, the member variables or member methods can only be used in this class, classes in subclass or other packages are invisible.
2. By default, this permission cannot be transferred across packages. This member's face change or member method can only be called in this class or its subclass.
3. protected by protected, if the member variable or member method is modified by protected, the member variable or member method is visible to the class or subclass of the same package. If you want to cross-package, it must be visible in the subclass.
4. Public public: if the member variables or member methods are modified using public, they are visible anywhere.
The permission levels of these four permission modifiers are low ----------> high
Private-> default-> protected-> Public