(Memories from last season:
1. Detailed use of interfaces and general classes in target-oriented.
It can be instantiated for the parent class through the polymorphism of the target, and all the operation specifications in the future are mainly based on the parent class.
Top knowledge points this season:
Explain the connection between generic classes and interfaces.
1. Can a general class include an interface?
abstractclassA
{
publicabstractvoidfun();
interfaceB
{
publicvoidprint();
}
}
Let's verify the test and find that A$B.class is generated, indicating that B is the internal interface of A, confirming that a general class can include an internal interface.
How to use it? Let's see below~
First of all, let's first subclass X override the parent class A method but not the method in interface B
abstractclassA
{
publicabstractvoidfun();
interfaceB
{
publicvoidprint();
}
}
classXextendsA
{
//Override the parent class fun() method
publicvoidfun()
{
System.out.println(HelloWorld!!!);
}
}
I found that there is no problem with the compilation~
Now we test overriding the interface method ha~
abstractclassA
{
publicabstractvoidfun();
interfaceB
{
publicvoidprint();
}
}
classXextendsA
{
//Override the parent class fun() method
publicvoidfun()
{
Bb=newY();
b.print();
}
classYimplementsB
{
//Override the method print() in the parent class interface
publicvoidprint()
{
System.out.println(HelloJava!!!);
}
}
}
Now we add the main method for testing~
abstractclassA
{
publicabstractvoidfun();
interfaceB
{
publicvoidprint();
}
}
classXextendsA
{
//Override the parent class fun() method
publicvoidfun()
{
Bb=newY();
b.print();
}
classYimplementsB
{
//Override the method print() in the parent class interface
publicvoidprint()
{
System.out.println(HelloJava!!!);
}
}
}
publicclassDemo01
{
publicstaticvoidmain(Stringargs)
{
Aa=newX();
a.fun();
}
}
It is proved that an abstract class can include an interface, and the subclass of the abstract class can choose whether to terminate the interface in the abstract class.
2. Can an interface include a general class? Same as above, you can choose whether to terminate or not
interfaceA
{
publicvoidfun();
abstractclassB
{
publicabstractvoidprint();
}
}
Next, let's write a subclass to check whether it is possible to selectively terminate the general class B
interfaceA
{
publicvoidfun();
abstractclassB
{
publicabstractvoidprint();
}
}
classXimplementsA
{
publicvoidfun()
{
}
}
It can be verified~
The following overrides the print() method in the generic class B
interfaceA
{
publicvoidfun();
abstractclassB
{
publicabstractvoidprint();
}
}
classXimplementsA
{
publicvoidfun()
{
newY().print();
}
classYextendsB
{
publicvoidprint()
{
System.out.println(HelloWorld!!!);
}
}
}
publicclassDemo02
{
publicstaticvoidmain(Stringargs)
{
Aa=newX();
a.fun();
}
}
3. Investigate the following procedures and verify the output
abstractclassA
{
publicA()
{
this.print();
}
publicabstractvoidprint();
}
classBextendsA
{
privateinti=30;
publicB(inti)
{
this.i=i;
}
publicvoidprint()
{
System.out.println(i= i);
}
}
publicclassDemo03
{
publicstaticvoidmain(Stringargs)
{
newB(50);
}
}
The output is 0, which is related to the subclass target instantiation process.
Recall: The instantiation process of the subclass target
The subclass target uses the new keyword to call the structure method
·Order of calling the structure method: first call the structure method in the parent class (default is no parameter structure), and then call the structure method in the subclass.
The intent of calling the structure method: to initialize its internal properties
·The properties in the subclass are all default values ??before the structure method in the superclass is terminated, and the integer type is "0".
consider:
We know that if a subclass terminates an interface, it must override the comprehensive general method in the interface. Then ask? Is there a way that subclasses can selectively override their real needs?
interfaceDoor
{
// open the door
publicvoidopenDoor();
//close the door
publicvoidcloseDoor();
//repair
publicvoidrepairDoor();
//tear down
publicvoidremoveDoor();
// lock the door
publicvoidlockDoor();
}
classDimplementsDoor
{
//Subclasses must now override the comprehensive general methods inside
}
Opposite:
· Subclasses do not need to override comprehensive methods
·If the subclass directly terminates the interface, it must override the comprehensive method
·Solution: What if a transition end is added in the middle?
interface --gt; transition end --gt; subclass
- This transition should not be used directly. --gt; general class
It is most suitable to use a general class for the transition end, because a general class can terminate an interface and also have some general methods, and it cannot be directly instantiated by the new keyword.
interfaceDoor
{
// open the door
publicvoidopenDoor();
//close the door
publicvoidcloseDoor();
//repair
publicvoidrepairDoor();
//tear down
[Zero-based JAVA] JavaSE target-oriented part-18. Target-oriented high-grade (06).doc)