找回密码
 立即注册
上季内容回忆:
1、面向目标中接口与笼统类的详细使用。
可以通过目标的多态性,为父类实例化,以后一切的操作规范以父类为主。
本季首要知识点:
解说笼统类与接口的联系。
1、一个笼统类中能否包括一个接口呢?
abstractclassA
{
publicabstractvoidfun();
interfaceB
{
publicvoidprint();
}
}
我们来验证测试下哈~发现生成A$B.class,表明B作为A的内部接口,证实一个笼统类中可以包括一个内部接口。
那怎样使用呢?我们看下面哈~
首要我们先子类X覆写父类A方法但不覆写接口B中的方法
abstractclassA
{
publicabstractvoidfun();
interfaceB
{
publicvoidprint();
}
}
classXextendsA
{
//覆写父类fun()方法
publicvoidfun()
{
System.out.println(HelloWorld!!!);
}
}
发现编译没有问题哈~
如今我们测试覆写接口方法哈~
abstractclassA
{
publicabstractvoidfun();
interfaceB
{
publicvoidprint();
}
}
classXextendsA
{
//覆写父类fun()方法
publicvoidfun()
{
Bb=newY();
b.print();
}
classYimplementsB
{
//覆写父类接口中的方法print()
publicvoidprint()
{
System.out.println(HelloJava!!!);
}
}
}
如今我们加上主方法进行测试哈~
abstractclassA
{
publicabstractvoidfun();
interfaceB
{
publicvoidprint();
}
}
classXextendsA
{
//覆写父类fun()方法
publicvoidfun()
{
Bb=newY();
b.print();
}
classYimplementsB
{
//覆写父类接口中的方法print()
publicvoidprint()
{
System.out.println(HelloJava!!!);
}
}
}
publicclassDemo01
{
publicstaticvoidmain(Stringargs)
{
Aa=newX();
a.fun();
}
}
证实一个笼统类中可以包括一个接口,以后在笼统类的子类中可以有选择的是不是终结笼统类中的接口。
2、一个接口中能否包括一个笼统类呢?同上,都是可以选择是不是终结
interfaceA
{
publicvoidfun();
abstractclassB
{
publicabstractvoidprint();
}
}
下面我们写上个子类,查验一下是不是可以有选择终结笼统类B
interfaceA
{
publicvoidfun();
abstractclassB
{
publicabstractvoidprint();
}
}
classXimplementsA
{
publicvoidfun()
{
}
}
验证可以哈~
下面覆写笼统类B中print()方法
interfaceA
{
publicvoidfun();
abstractclassB
{
publicabstractvoidprint();
}
}
classXimplementsA
{
publicvoidfun()
{
newY().print();
}
classYextendsB
{
publicvoidprint()
{
System.out.println(HelloWorld!!!);
}
}
}
publicclassDemo02
{
publicstaticvoidmain(Stringargs)
{
Aa=newX();
a.fun();
}
}
3、调查以下的程序,验证输出成果
abstractclassA
{
publicA()
{
this.print();
}
publicabstractvoidprint();
}
classBextendsA
{
privateinti=30;
publicB(inti)
{
this.i=i;
}
publicvoidprint()
{
System.out.println(i=+i);
}
}
publicclassDemo03
{
publicstaticvoidmain(Stringargs)
{
newB(50);
}
}
输出成果为0哈,这与子类目标实例化进程相关哈
回忆:子类目标的实例化进程
·子类目标使用new关键词要调用结构方法
·调用结构方法时的次序:先去调用父类中的结构方法(默以为无参结构),以后再调用子类中的结构方法。
·调用结构方法的意图:为其内部的属性初始化
·子类中的属性在没有终结父类中的结构方法之前,一切的内容都是默认值,整型是“0”。
考虑:
我们认识一个子类假如终结了一个接口则肯定要覆写接口中的全面笼统方法。那问?是不是可以选用一种方式,让子类可以有选择的去覆写自己真实需求的方法。
interfaceDoor
{
//开门
publicvoidopenDoor();
//关门
publicvoidcloseDoor();
//修理
publicvoidrepairDoor();
//拆除
publicvoidremoveDoor();
//锁门
publicvoidlockDoor();
}
classDimplementsDoor
{
//子类如今肯定要覆写里边全面的笼统方法
}
对立点:
·子类不需求覆写全面的方法
·假如子类直接终结了接口则肯定要覆写全面的方法
·解决:假如中间加入了一个过渡端呢?
接口--gt;过渡端--gt;子类
-此过渡端不该该被直接使用。--gt;笼统类
过渡端使用笼统类终结最合适,由于笼统类可以终结一个接口同时可以有一些笼统方法,而且不能直接被new关键词实例化。
interfaceDoor
{
//开门
publicvoidopenDoor();
//关门
publicvoidcloseDoor();
//修理
publicvoidrepairDoor();
//拆除
[零基础学JAVA]JavaSE面向目标部分-18.面向目标高档(06).doc

(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)

[下载]10400003212.rar




上一篇:JDBC学习笔记~~~
下一篇:[零基础学JAVA]Java SE面向对象部分-19.面向对象高级(07)