I. Explanation of the packageWhat is encapsulation? Why encapsulation? How to implement encapsulation?
The purpose of encapsulation is to improve the security of the program. Encapsulation is a way to hide the attributes that you don't want the third party to see.
The encapsulation is implemented by: 1. Modify the visibility of the property to restrict access.
2. Set the Read method for the property.
3. In the method of reading the property, add the limit of the heap property read.
Packagecom.chen.test; Public classTest9 {Private intAge//Define Age PrivateString name;//Define name Private DoublePrice//Define Price//encapsulating attributes with the Get/set method Public intGetage () {returnAge ; } //age limit is set when age is passed in Public voidSetage (intAge ) { if(Age > | |-age < 0) { This. Age = 18; } This. Age =Age ; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public DoubleGetPrice () {returnPrice ; } Public voidSetprice (DoublePrice ) { This. Price =Price }
}
Several key words in the package are summarized:
Private: Can only be called by the class itself
Public:1. Can be called by methods in the class itself. 2. You can call the public properties and methods of the parent class directly in the subclass. 3. Can be called directly by the object.
Protected:1. Can be called by a method of the class itself. 2. The protected property and methods of the parent class can be called directly in the subclass.
Default keyword: 1. The default keyword-decorated properties and methods of the parent class can be called directly in the subclass. 2. Can be called directly by the object of this package.
The comparison of keywords, from three angles to contrast: sub-class, inside the package, outside the package.
The purpose of the protected keyword design is to call the properties and methods of the parent class for the subclass.
The default keyword design is intended to be callable within the package and cannot be called outside the package.
two. Inheritance
Java inheritance is a single-line inheritance, does not allow multiple inheritance;
subclasses can inherit which methods and properties of the parent class:
1. Private properties and methods of the parent class that cannot be inherited
2. The protected property and methods of the parent class can inherit
3. The public properties and methods of the parent class can inherit
4. The default keyword-decorated properties and methods of the parent class, if the child class and the parent class are in a package, you can inherit
abstract classes and abstract methods:
The class that cannot be instantiated is modified with abstract, which is abstract.
Abstract classes can write abstract methods, can also write non-abstract methods----------------Non-abstract classes cannot define abstract methods;
Abstractions defined in abstract classes must be overridden in subclasses------subclasses must override the abstract method of the parent class (unless the subclass is also an abstract class)
the role of abstract methods?
subclasses must override the abstract method of the parent class, and can implement polymorphism;
Summary of final keywords:
1. The final modifier variable-----give the variable an opportunity to initialize once and cannot be modified later
Public final Double dburl;
The final modified reference variable-------------------lock the referenced address and can no longer be changed;
Final Dog dog = new Dog ("Huanhuan");
dog = null;
2. Final modifier class-----This class cannot inherit from the quilt class
Public Final class Dog extends pet{}
3. The final modification method------indicates that this method cannot be overridden by a quilt class
Public final void swim (pool pool) {}
Override: override
The subclass has a method with the parent class, the same name, the same parameter, and the same return value, then it is said that this method of the subclass overrides the parent class's method of the same name;
what was rewritten?
the nature of the method------>>> is a function pointer
The essence of rewriting is: replace the first address of the same name method in the memory area of the parent class with the first address of the Chinese method in the memory area of the subclass
Interface vs. abstract class
1. Interface is the representation of capability
Abstract class is class, it has properties and behavior
2. Constants can be defined in the interface, but no attributes
3. The methods in the interface are public abstract (a common abstraction method that cannot define other types of methods)
4. Methods in an abstract class can be abstract or non-abstract. But the interface must be abstract;
Some knowledge of object-oriented in Java (II.)