lesson 4:多态
来源:互联网 发布:淘宝自己开网店怎么开 编辑:程序博客网 时间:2024/06/05 02:28
题目:课后作业题4
代码:
abstract class Animal {abstract void cry();abstract void getAnimal();}public class Test {public static void main(String[] args) {Simulator simulator=new Simulator();//Animal animal=new Dog();Animal animal=new Cat();if(animal instanceof Dog){Dog dog=(Dog) animal;dog.cry();dog.getAnimal();}else if(animal instanceof Cat){Cat cat= (Cat)animal;cat.cry();cat.getAnimal();}}} class Dog extends Animal {void cry() { System.out.println("汪汪汪"); } void getAnimal() { System.out.println("种类为狗"); }} class Cat extends Animal {void cry() { System.out.println("喵喵喵"); } void getAnimal() { System.out.println("种类为猫"); }} class Simulator { void palySound(Animal animal) { animal.cry(); animal.getAnimal(); } }结果:
总结:多态性就是父类的某个方法被子类重写时,各自产生各自功能的行为。
注意写在同一个类中:
一个文件中有多个类,只有与文件名一致的类名,才能声明为:public
内部类不能声明为:public
一个文件中只能有一个public类
阅读全文
1 0
- lesson 4:多态
- Lesson 4
- lesson 4
- Lesson 4 上机练习题——多态
- Lesson 4上机练习题——多态
- Lesson 4上机练习题——多态
- Lesson 4上机练习题——多态
- Lesson 4上机练习题——多态
- LESSON 1 -part 4
- Ajax Lesson 4
- 新概念英语 lesson 4
- 孙鑫MFC:Lesson 4
- Java SE Lesson 4
- Lesson 4 按键
- lesson 4 磁盘
- lesson 4 communication pattern
- Lesson 4 Nehe
- New Concept English 2 -- Lesson 3 and Lesson 4
- Redis入门到精通-Redis安装与部署(单机)
- LWC 55:714. Best Time to Buy and Sell Stock with Transaction Fee
- Android3种播放视频功能
- Golang语言社区--游戏服务器框架 Leaf/go 分析
- Riak CS集群部署
- lesson 4:多态
- Unity3d
- Redis入门到精通-Redis数据类型详解
- jsp标签
- freemarker的使用
- Redis入门到精通-Redis高级命令
- Go语言学习资料下载(PDF文件)
- 移动互联网下半场的面试真经,让你进入 BAT 不再是梦
- unity调用安卓原生activity,按Home键返回被杀死问题解决