码迷,mamicode.com
首页 > 编程语言 > 详细

java 07 多态强转

时间:2019-01-08 19:34:13      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:extend   tar   ace   继承   new   smon   smo   关系   sys   

例子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();
}

 

java 07 多态强转

标签:extend   tar   ace   继承   new   smon   smo   关系   sys   

原文地址:https://www.cnblogs.com/simly/p/10240248.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!