多态
来源:互联网 发布:氮素网络是什么意思 编辑:程序博客网 时间:2024/05/18 00:04
1.
宝马快速地跑了起来...黑色
保时捷慢慢地跑了起来...白色
宝马快速地跑了起来...黑色
保时捷慢慢地跑了起来...白色
保时捷慢慢地跑了起来...白色
2. 接口关系下的多态
运行结果:
增
删
abstract class Car{String name;String color;public Car(){}public Car(String name, String color){this.name = name;this.color = color;}public abstract void run();}class Bmw extends Car{public Bmw(){super();}public Bmw(String name, String color){super(name,color);}public void run(){System.out.println(this.name+"快速地跑了起来..."+this.color );}}class Bsj extends Car{public Bsj(){super();}public Bsj(String name, String color){super(name,color);}public void run(){System.out.println(this.name+"慢慢地跑了起来..."+this.color );}}class CarFactory{String name;//Car bmw = new Bmw();//Car bsj = new Bsj();public CarFactory(String name){this.name = name;}//public Car produce()//{//int a = (int)Math.round(Math.random()*1);//if(a == 0)//return bmw;//else //return bsj;//}public Car produce(){int a = (int)Math.round(Math.random()*1);if(a == 0)return new Bmw("宝马","黑色");elsereturn new Bsj("保时捷","白色");}}public class duotaiPractice{public static void main(String[] args){int x = 0;CarFactory cf = new CarFactory("红太阳制造车厂");while(x<5){Car c = cf.produce();c.run();x++;}}}
宝马快速地跑了起来...黑色
保时捷慢慢地跑了起来...白色
宝马快速地跑了起来...黑色
保时捷慢慢地跑了起来...白色
保时捷慢慢地跑了起来...白色
2. 接口关系下的多态
interface Dao{public abstract void add();public abstract void delete();}class UserDao implements Dao{public void add(){System.out.println("增");}public void delete(){System.out.println("删");}}public class InterfaceDuotai {public static void main(String[] args) {Dao d = new UserDao();d.add();d.delete();}}
运行结果:
增
删
0 0
- 多态
- 多态
- 多态
- 多态
- 多态
- 多态
- 多态
- 多态
- 多态
- 多态
- 多态
- 多态
- 多态
- 多态
- 多态
- 多态
- 多态
- 多态
- codeforces 725C Hidden Word(构造)
- java 选择排序
- Android4.3工程代码移植Android5.1
- 队列02:应用
- 【c语言问题系列教程之四】指针
- 多态
- 自定义AlertDialog对话框
- 《CLR via C#》读书笔记-.NET多线程(一)
- 使用Fragment和Viewpager实现Tab嵌套,仿网易云音乐tab
- PAT乙级1021
- Linux系统下的简单命令【一】
- 函数库
- javascript 中 typeof的返回值
- 用字节数组存放二维地图数据