1. Encapsulation
Encapsulation: Is the property of the hidden object and implementation details, only provide public access to the external way.
Encapsulation steps: Modify the visibility of attributes to restrict access to properties, create a pair of assignment (setter) methods and value (getter) methods for each property, and access the properties, and in the assignment method, add an access control statement to the property.
The main advantages of encapsulation are: the implementation details of the hidden class, so that the user can access the data only through the method prescribed by the programmer, it is convenient to add access control statements and restrict unreasonable operation.
The permissions control at the time of encapsulation differs from the following:
1 /*2 * Package Demo3 */4 Public classDog {5 //Privatization Properties6 PrivateString name;7 PrivateString sex;8 PrivateString color;9 Ten //Encapsulate Fields One PublicString GetName () { A returnname; - } - the Public voidsetName (String name) { - This. Name =name; - } - + PublicString Getsex () { - returnsex; + } A at Public voidsetsex (String sex) { - This. Sex =sex; - } - - PublicString GetColor () { - returncolor; in } - to Public voidsetcolor (String color) { + This. color =color; - } the *}
Penguin class.
2. Inheritance
Grammar:
Modifier Subclass extends Superclass{
Class Definition Section
}
In Java, inheritance is implemented through the extends keyword, where subclass is called a subclass, superclass is called a parent class, a base class, or a superclass. If the modifier is public, the class is visible throughout the project, and the public modifier is not written until the current package is available; You cannot use the private and protected modifiers.
Inheritance (inheritance): is one of the important means of implementing code reuse in Java. Only single inheritance is supported in Java, that is, each class can have only one parent class. The inheritance expresses the relationship between Isa, or a special and general relationship.
In Java, all Java classes inherit the Java.lang.long.Object class directly or from each other. The object class is the ancestor of all Java classes. When you define a class without using the extends keyword, the class inherits directly from the object class.
In Java, subclasses can inherit from the parent class:
Ø inherit public and protected modified properties and methods, regardless of whether the subclass and parent class are in the same package.
o Inherit the properties and methods decorated by the default permission modifier, but the subclass and parent class must be in the same package.
Subclasses cannot inherit from a parent class:
Ø cannot inherit properties and methods of private adornment
Ø cannot inherit the parent class's construction method
If the method inherited from the parent class does not meet the requirements of the subclass, the parent class can be overridden (overwritten) by the same name method in the subclass to meet the requirements.
Abstract the Dog class and the Penguin class and the parent class Pet Class (Pet class is abstract class, cannot be instantiated)
/*** Dog and Penguin's parent pet, while the pet object is meaningless, just abstract out of a concept, so the Pet class Tim * plus the abstract modifier, let it become an abstract class, abstraction cannot be instantiated.*/ Public Abstract classPet {//Privatization Properties PrivateString name; PrivateString sex; //Encapsulate Fields PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicString Getsex () {returnsex; } Public voidsetsex (String sex) { This. Sex =sex; } //Non-parametric construction method PublicPet () {}/*** Method of construction of a parameter * *@paramName * *@paramSex * Gender **/ PublicPet (string name, string sex) { This. SetName (name); This. Setsex (Sex); } //abstract method of Pet class eat (), abstract method required Quilt class rewrite Public Abstract voideat (); //The final decorated method cannot be overridden by a quilt class Public Final voidprintln () {//method Body ... }}//dogs inherit Animal classclassDogextendsPet {PrivateString color; //Encapsulate Fields PublicString GetColor () {returncolor; } Public voidsetcolor (String color) { This. color =color; } //Non-parametric construction method PublicDog () {}//a method for constructing a parameter PublicDog (string name, string sex, string color) {//The constructor that passes to the parent class cannot be used here with this.name = Name .... Super(name, sex); //Call the SetColor () method to assign a value to the property color This. SetColor (color); } //overriding the parent class eat () method Public voideat () {//method body .... }}//penguins inherit Animal classclassPenguinextendsPet {Private Doubleweight; Public Doublegetweight () {returnweight; } Public voidSetweight (Doubleweight) { This. Weight =weight; } //overriding the parent class eat () method Public voideat () {//method body .... }}//Testing class TestclassTest { Public Static voidMain (string[] args) {String name= "Dog1"; String Sex= "Male"; String Color= "Red"; //instantiating an objectDog dog =NewDog (); Dog.setname (name); //set the property of the dog by the method inherited from the parent class namedog.setsex (Sex); //instantiating an object by using a parameter constructorDog Dog1 =NewDog ("Dogname", "Man", "Black"); //call the Eat () method, this method is overridden by thedog1.eat (); }}
Constructor method invocation rules under inheritance conditions are as follows
If the constructor method of the subclass is not called by the super display to call the parent class's parameter constructor method, and does not show the other constructor method to call itself through the this display, the system will call the parent class's parameterless constructor by default. In this case, the super () statement effect is the same.
Ø if the constructor method of a subclass is called by the super display to call the parent class, it will execute the corresponding construction method of the parent class without executing the parent class without the parameter construction method.
Ø If the subclass is constructed by using this to display other constructor methods of its own, the above two rules are applied in the corresponding construction method.
Ø in particular, if there is a multilevel inheritance relationship, when a subclass object is created, the above rules are applied more than once to the higher-level parent class until the non-parametric construction method of the top-level parent class object class is executed.
Abstract and final are two keywords that are functionally reversed, and abstract can be used to decorate classes and methods, not to modify properties and construct methods. Final can be used to decorate classes, methods, and properties, and cannot be decorated with construction methods.
3. polymorphic
Polymorphism (polymorphism): refers to the ability characteristics of many forms. A more professional argument is that the same implementation interface uses different instances to perform different operations.
Three conditions for polymorphism:
1. The existence of inheritance (inheritance is the basis of polymorphism, there is no polymorphism without inheritance).
2. Subclasses override the method of the parent class (the method that the subclass overrides is called under polymorphism).
3. The parent class reference variable points to the subclass object (the subclass to the parent class's type conversion).
Rules when a subclass is converted to a parent class:
Ø a reference to a parent class is directed to the object of a subclass, called upward transformation (UPCASTIOG), for automatic type conversion.
Ø the method that is called by a parent class reference is a subclass of a method that overrides or inherits from a parent class, not a method of the parent class.
Ø a method that is unique to a subclass cannot be called by referring to a variable through the parent class.
If the parent class is to invoke a specific method of a subclass, it is necessary to assign a reference to a subclass of the parent class reference to a child class, called a downward transformation, at which time the cast must be coerced.
Public classPerson//Human{String name; intAge ; Public voideat () {System.out.println ("People are eating!"); }}classChineseextendsPerson {//overriding the parent class method Public voideat () {System.out.println ("The Chinese are eating!"); } //subclass-specific methods that cannot be called when a parent class reference points to a subclass object Public voidshadowboxing () {System.out.println ("Practicing Tai Chi!"); }}class中文版extendsPerson {//overriding the parent class method Public voideat () {System.out.println ("The English are eating!"); }}//Test ClassclassTesteat { Public Static voidMain (string[] args) {testeat test=Newtesteat (); //reference to Chinese, create Chinese human objectPerson Person1 =NewChinese (); //The Chinese Eat () method is called at this timetest.showeat (Person1); Person Person2=New中文版 (); //The Eat () method of the Chinese is called at this timetest.showeat (Person2); //forced type conversion (downward transition)Chinese Chinese =(Chinese) person1; //the unique method of calling subclasses after a downward transformationchinese.shadowboxing (); } //use the parent class as the formal parameter of the method to implement Polymorphic Public voidshoweat (person person) {//The Eat () method that invokes which object is passed inperson.eat (); }}
The understanding of encapsulation, inheritance, polymorphism.