[零基础学JAVA]Java SE面向对象部分-16.面向对象高级(04)
上季内容回忆:1、final关键词
·润饰类不能被承继
·润饰方法不能被覆写
·润饰的变量即是一个常量,大局常量(publicstaticfinal)
2、笼统类和接口
·笼统类:只包括一个笼统方法的类,笼统方法只需声明而不需求实现,必须有子类
·接口:只包括笼统方法和大局常量的类——接口,也是必须有子类
在实际中一个类很少会去承继一个已经完全实现好的类,基本上都是承继笼统类和实现接口。
本季首要知识点:
1、目标的多态性
2、instanceof关键词
3、Object类
目标的多态性
注意点:
为了清理的论述出概念,如今先使用一般类的承继联系。
向上转型:
classA
{
publicvoidfun1()
{
System.out.println(A类===gt;publicvoidfun1());
}
publicvoidfun2()
{
//fun2方法调用的是fun1方法
this.fun1();
}
}
classBextendsA
{
//覆写A类中的fun1()方法
publicvoidfun1()
{
System.out.println(B类===gt;publicvoidfun1());
}
publicvoidfun3()
{
System.out.println(B类===gt;publicvoidfun3());
}
}
publicclassDemo01
{
publicstaticvoidmain(Stringargs)
{
Bb=newB();
Aa=newA();
b.fun1();
a.fun2();
b.fun3();
}
}
目标多态性体如今目标彼此转型上面哈~
classA
{
publicvoidfun1()
{
System.out.println(A类===gt;publicvoidfun1());
}
publicvoidfun2()
{
//fun2方法调用的是fun1方法
this.fun1();
}
}
classBextendsA
{
//覆写A类中的fun1()方法
publicvoidfun1()
{
System.out.println(B类===gt;publicvoidfun1());
}
publicvoidfun3()
{
System.out.println(B类===gt;publicvoidfun3());
}
}
publicclassDemo02
{
publicstaticvoidmain(Stringargs)
{
//声明一个父类目标
Aa=null;
//newB()是子类目标向父类目标转换
a=newB();
a.fun1();
}
}
如今我们来看下a.fun1()调用的是哪个类的方法哈~
classA
{
publicvoidfun1()
{
System.out.println(A类===gt;publicvoidfun1());
}
publicvoidfun2()
{
//fun2方法调用的是fun1方法
this.fun1();
}
}
classBextendsA
{
//覆写A类中的fun1()方法
publicvoidfun1()
{
System.out.println(B类===gt;publicvoidfun1());
}
publicvoidfun3()
{
System.out.println(B类===gt;publicvoidfun3());
}
}
publicclassDemo02
{
publicstaticvoidmain(Stringargs)
{
//声明一个父类目标
Aa=null;
//newB()是子类目标向父类目标转换
//子类目标向父类目标转型以后,所调用的方法一定是被覆写过的方法
a=newB();
a.fun1();
a.fun2();
}
}
子类目标向父类目标转型以后,所调用的方法一定是被覆写过的方法,这即是目标的向上转型哈~
向下转型:
classA
{
publicvoidfun1()
{
System.out.println(A类===gt;publicvoidfun1());
}
publicvoidfun2()
{
//fun2方法调用的是fun1方法
this.fun1();
}
}
classBextendsA
{
//覆写A类中的fun1()方法
publicvoidfun1()
{
System.out.println(B类===gt;publicvoidfun1());
}
publicvoidfun3()
{
System.out.println(B类===gt;publicvoidfun3());
}
}
publicclassDemo03
{
publicstaticvoidmain(Stringargs)
{
//声明一个父类目标
Aa=null;
//newB()是子类目标向父类目标转换
//子类目标向父类目标转型以后,所调用的方法一定是被覆写过的方法
a=newB();
a.fun1();
a.fun2();
a.fun3();
}
}
如今我们来看下能否调用a.fun3()哈~
程序提示找不到fun3()方法,A类中没有fun3()方法哈,如果我们一定要调用的话,我们就要使用向下转型哈~
classA
{
publicvoidfun1()
{
System.out.println(A类===gt;publicvoidfun1());
}
publicvoidfun2()
{
//fun2方法调用的是fun1方法
this.fun1();
}
}
classBextendsA
{
//覆写A类中的fun1()方法
publicvoidfun1()
{
System.out.println(B类===gt;publicvoidfun1());
}
publicvoidfun3()
{
System.out.println(B类===gt;publicvoidfun3());
}
}
publicclassDemo03
{
publicstaticvoidmain(Stringargs)
{
//声明一个父类目标
Aa=null;
//newB()是子类目标向父类目标转换
//子类目标向父类目标转型以后,所调用的方法一定是被覆写过的方法
a=newB();
//可以进行向下转型,需求使用强制性手法哈~
Bb=(B)a;
(Memories from last season:
1. final keywords
· Retouch classes cannot be inherited
The retouch method cannot be overridden
The modified variable is a constant, the overall constant (publicstaticfinal)
2. Generic classes and interfaces
Generic class: a class that includes only one generic method. The generic method only needs to be declared and does not need to be implemented. There must be subclasses
Interface: A class that only includes general methods and overall constants - an interface, which must also have subclasses
In practice, a class seldom inherits a fully implemented class, basically it inherits the general class and implements the interface.
Top knowledge points this season:
1. The polymorphism of the target
2. instanceof keywords
3. Object class
target polymorphism
be careful:
In order to clarify the concept, the general class inheritance relationship is now used first.
Upward transformation:
classA
{
publicvoidfun1()
{
System.out.println(Class A ===gt;publicvoidfun1());
}
publicvoidfun2()
{
//The fun2 method calls the fun1 method
this.fun1();
}
}
classBextendsA
{
//Override the fun1() method in class A
publicvoidfun1()
{
System.out.println(Class B ===gt;publicvoidfun1());
}
publicvoidfun3()
{
System.out.println(Class B ===gt;publicvoidfun3());
}
}
publicclassDemo01
{
publicstaticvoidmain(Stringargs)
{
Bb=newB();
Aa=newA();
b.fun1();
a.fun2();
b.fun3();
}
}
The target polymorphisms are now transforming each other on top of each other~
classA
{
publicvoidfun1()
{
System.out.println(Class A ===gt;publicvoidfun1());
}
publicvoidfun2()
{
//The fun2 method calls the fun1 method
this.fun1();
}
}
classBextendsA
{
//Override the fun1() method in class A
publicvoidfun1()
{
System.out.println(Class B ===gt;publicvoidfun1());
}
publicvoidfun3()
{
System.out.println(Class B ===gt;publicvoidfun3());
}
}
publicclassDemo02
{
publicstaticvoidmain(Stringargs)
{
//declare a parent class target
Aa=null;
//newB() is the conversion of the subclass target to the parent class target
a=newB();
a.fun1();
}
}
Now let's see which class method is called by a.fun1()~
classA
{
publicvoidfun1()
{
System.out.println(Class A ===gt;publicvoidfun1());
}
publicvoidfun2()
{
//The fun2 method calls the fun1 method
this.fun1();
}
}
classBextendsA
{
//Override the fun1() method in class A
publicvoidfun1()
{
System.out.println(Class B ===gt;publicvoidfun1());
}
publicvoidfun3()
{
System.out.println(Class B ===gt;publicvoidfun3());
}
}
publicclassDemo02
{
publicstaticvoidmain(Stringargs)
{
//declare a parent class target
Aa=null;
//newB() is the conversion of the subclass target to the parent class target
//After the subclass target is transformed to the parent class target, the method called must be an overridden method
a=newB();
a.fun1();
a.fun2();
}
}
After the subclass target is transformed to the parent class target, the method called must be the overridden method, which is the upward transformation of the target~
Downcast:
classA
{
publicvoidfun1()
{
System.out.println(Class A ===gt;publicvoidfun1());
}
publicvoidfun2()
{
//The fun2 method calls the fun1 method
this.fun1();
}
}
classBextendsA
{
//Override the fun1() method in class A
publicvoidfun1()
{
System.out.println(Class B ===gt;publicvoidfun1());
}
publicvoidfun3()
{
System.out.println(Class B ===gt;publicvoidfun3());
}
}
publicclassDemo03
{
publicstaticvoidmain(Stringargs)
{
//declare a parent class target
Aa=null;
//newB() is the conversion of the subclass target to the parent class target
//After the subclass target is transformed to the parent class target, the method called must be an overridden method
a=newB();
a.fun1();
a.fun2();
a.fun3();
}
}
Now let's see if we can call a.fun3() ha~
The program prompts that the fun3() method cannot be found. There is no fun3() method in class A. If we must call it, we must use downcasting~
classA
{
publicvoidfun1()
{
System.out.println(Class A ===gt;publicvoidfun1());
}
publicvoidfun2()
{
//The fun2 method calls the fun1 method
this.fun1();
}
}
classBextendsA
{
//Override the fun1() method in class A
publicvoidfun1()
{
System.out.println(Class B ===gt;publicvoidfun1());
}
publicvoidfun3()
{
System.out.println(Class B ===gt;publicvoidfun3());
}
}
publicclassDemo03
{
publicstaticvoidmain(Stringargs)
{
//declare a parent class target
Aa=null;
//newB() is the conversion of the subclass target to the parent class target
//After the subclass target is transformed to the parent class target, the method called must be an overridden method
a=newB();
// Downward transformation can be performed, and the mandatory method is required~
Bb=(B)a;)
页:
[1]