One, polymorphic
1, what is polymorphic?
Parsing: Different objects respond differently to the same operation
Features that have the ability to express multiple forms
2. Advantages of using polymorphism
Parsing: In order to achieve unified invocation
A small example:< the parent class type as a parameter >
Parent class (Pet) subclass (Gog,penguin) master Human (master) test Class (test)
Pet
Public Abstract class Pet { publicabstractvoid eat ();}
Dog
Public class extends pet{ @Override publicvoid eat () { System.out.println ( "Enough to eat Bones"); }
Penguin
Public class extends Pet { @Override publicvoid eat () { System.out.println ("Penguin Eats Fish" ); }
Master
Public class Master { // parent class type as parameter publicvoid feed (Pet pet) { pet.eat (); }}
Test
Public Static void Main (string[] args) { Master master=new master (); Pet Pet=new Dog (); Master.feed (PET); }
Transformation of subclasses to parent class (upward transformation)
Rules:
① a reference to a parent class to a subclass object, which becomes an upward transformation, automatic type conversion
② a method that is called by a parent class reference variable is a child class that overrides or inherits a method of the parent class, not the parent class's method
③ a method that is unique to a subclass cannot be called through a parent class reference variable
Third, two ways to implement and use polymorphism in Java
① using a parent class as a method parameter
② using the parent class as a method return value
Iv. conversion of parent class to subclass (downward transition)
Assigns a reference to a subclass object to a parent class reference, called a downward transformation. Forced type conversions are required at this time
Five, instanceof operators
Grammar:
Object instanceof class or interface
This operator is used to determine whether an object belongs to a class or implements an interface with the result of true or false
Note:
① when using the instanceof operator, the object must be of the same type as the class or interface specified by the second parameter of Instanceof, or a compilation error will occur
②instanceof commonly used in conjunction with forced type conversions
Vi.. Interface
In the Java language, interfaces have two meanings:
① conceptual interface, i.e. all services provided externally by the system
② refers to an interface defined with the interface keyword, also known as an interface type
Definition: An interface is a specification and a standard that they can constrain the behavior of a class, which is a collection of some method features
Grammar:
"Modifier" Interface interface name extends parent interface 1, connector 2 ....
Class Name extends parent class name implements Interface 1, interface 2 ....
Vii. characteristics
The member variables in the ① interface are public, static, and final type by default and must be displayed for initialization.
The methods in the ② interface are default to public, abstract type
③ interface has no constructor method and cannot be instantiated
④ an interface cannot implement another interface, but it can inherit multiple other interfaces
The ⑤ interface must implement its abstract method through a class
⑥ A class can inherit only one direct parent class, but can implement multiple interfaces
Viii. Grammar
① modifier Interface Interface name extends parent interface 1, parent interface 2 .... {
Constant definition
Method definition
}
②class class Name extends parent class name implements Interface 1, interface 2 .... {
Class Member
}
A small example:
Using the Java interface to simulate the USB interface in life
1, first define the USB interface, through the service () method to provide services
Public Interface usbinterface{void service ();
2. Define USB drive class, realize the service () method in USB interface, carry out data transmission
Public class udisk implementd usbinterface{publicvoid service () {System.out.println ("Connect USB port , Start data Transfer! ");}}
3, defines the USB fan class, realizes the service () method in the USB interface, obtains the current causes the fan to turn
Public class usbfan implementd usbinterface{publicvoid service () {System.out.println ( "Connect the USB port to get the current, the fan starts to turn!" ");}}
4. Write Test class
Public class test{publicstaticvoid Main (String[]args) {//U-Disk Usbinterface udisk=New udisk (); Udisk.service (); // USB Fan Usbinterface usbfan=New Usbfan (); Usbfan.service ();}}
Multi-State and interface