Memoryizz

java 07 多态强转

例子1

class Interfacedemoplus{
public static void main(String[] agrs){
Womanstar Won =new Womanstar();
//隐式转换
Whit w = Won;
Rich r = Won;


//无包含关系,强转
Beautiful b = (Beautiful)w;


//打印Beau
b.Beau();

//小范围 往大范围转
WRB wrb =(WRB)b;
Whit www =wrb;


}
}

 

//定义接口
interface Whit{
void vertWhit ();
}

interface Rich{
void hasmoney ();
}

interface Beautiful{
void Beau ();
}
//一个接口继承三个接口
interface WRB extends Whit,Rich,Beautiful {

}

//类实现接口多个接口
class Womanstar implements WRB {
public void vertWhit(){
System.out.println("很白~~");
}
public void hasmoney(){
System.out.println("有钱~~");
}
public void Beau(){
System.out.println("美丽~~");
}
}

-------------------------------------------------------------------------------------------------------------------

例子2

class Interfacedemochangliang{
public static void main(String[] agrs){
Jing8 jin8 =new Jing8();
jin8.meng();
//变相调用
Dog d =jin8;
d.watch();
//多态
anmial a = d;
a.cry();
//强制转换多态
Pet pet =(Pet) a;
//强制转换多态
anmial aa =(anmial)pet;
//jin8既继承了dog和anmial又implements Pet所以可以直接看
Pet jjj =jin8;
}
}

//接口中加入常量

abstract class anmial {
public void cry(){
System.out.println("汪汪汪");
}
}

class Dog extends anmial{
public void watch(){
System.out.println("有人来了~~");
}
}

//继承加接口
class Jing8 extends Dog implements Pet {
public void meng(){
System.out.println("!!重新为~~");
}
}

interface Pet{
void meng();
}

 

posted on 2019-01-08 17:31  Memoryizz  阅读(219)  评论(0编辑  收藏  举报

导航