一个程序尽可能多的实现Java面向对象编程的概念
来源:互联网 发布:苹果手机提高数据网速 编辑:程序博客网 时间:2024/06/06 17:34
abstract class Animal{//这是一个抽象类String name;String furcolor;String family;Animal(){//抽象类中的无参构造函数String name = null;String furcolor = null;this.name = name;this.furcolor = furcolor;}Animal(String name, String furcolor){//抽象类中的有参构造函数this.name = name;this.furcolor = furcolor;}void setName(String name){this.name = name;}void setFurcolor(String furcolor){this.furcolor = furcolor;}void setFamily(String family){this.family = family;}String getName(){return name;}String eatRecipe(Animal ani){if (ani.family == "primate" || ani.family == "bear")return "杂食动物";elsereturn "我不知道,可能食草";}abstract String excitement();//设置对象兴奋的样子,显然为抽象函数}interface Valuable{//接口String value(Animal ani);}interface Protective{//接口String protect(Animal ani);}class Particular extends Animal implements Protective{String name;String furcolor;String family;Particular(){String name = null;String furcolor = null;super.name = name;super.furcolor = furcolor;}Particular(String name, String furcolor){super.name = name;super.furcolor = furcolor;}String excitement(){return "嚎叫";}public String protect(Animal ani){return "在保护区生活";}}class RareAnimal extends Animal implements Protective, Valuable{String name;String furcolor;String family;RareAnimal(){String name = null;String furcolor = null;super.name = name;super.furcolor = furcolor;}RareAnimal(String name, String furcolor){super.name = name;super.furcolor = furcolor;}public String excitement(){return "上蹿下跳,龇牙咧嘴";}public String value(Animal ani){return "无价之宝";}public String protect(Animal ani){return "在保护区或者室内生活";}}public class TestPanda {public static void main(String[] args) {//Animal firefox = new Animal(); 抽象类不能实例化对象出来//全功能测试。这里调用了抽象类的无参构造函数RareAnimal panda = new RareAnimal();panda.setName("panda");panda.setFurcolor("Black and White");panda.setFamily("bear");System.out.println("就" + panda.getName() + "而言," + "它是" + panda.eatRecipe(panda) + "。兴奋起来" + panda.excitement() + "。然而这等" + panda.value(panda) + "只能" + panda.protect(panda));//全功能测试完毕//测试父类引用勾搭子类对象。这里调用了抽象类的带参数的构造函数Animal chiken = new Particular("chiken","White Feather");System.out.println("就" + chiken.getName() + "。兴奋起来" + chiken.excitement() + "。而" + chiken.eatRecipe(chiken) + "主要吃什么。" );//测试父类引用勾搭子类对象完毕//强制类型转换Protective rhino = (RareAnimal)panda;//rhino作为protect的函数参数时,再一次被转换。因为参数被定义为animal,而非ProtectiveSystem.out.println("就犀牛而言,它只能" + rhino.protect((Animal) rhino) + "。");//强制类型转换完毕//随手玩儿Particular elephant = new Particular("elephant","gray");elephant.setFamily(null);System.out.println("就" + elephant.getName() + "而言," + "它吃什么" + elephant.eatRecipe(elephant) + "。兴奋起来" + elephant.excitement() + "。然而这等动物" + "只能" + elephant.protect(elephant) + "。");}}
在编写过程中,该程序使我明确了很多概念。
- 抽象类中可以定义构造函数,有参数,跟无参数皆可。
- 其实在全功能测试中,我没在派生类重写构造函数,而直接调用的无参构造函数。
- 而为什么我又重写了呢?在第二个父类引用勾搭子类对象时,我用了(String,String)建立对象。但是IDE报错了。
- 上面的细节,我没搞懂。理论上,应该是要重写构造函数。所以统统重写了。
- 抽象类的构造函数能声明为抽象的么?No!!
- 对于强制类型转换有问题的博友,可以在该段带入不同定义的模块实验。
执行结果也很可爱。
就panda而言,它是杂食动物。兴奋起来上蹿下跳,龇牙咧嘴。然而这等无价之宝只能在保护区或者室内生活就chiken。兴奋起来嚎叫。而我不知道,可能食草主要吃什么。就犀牛而言,它只能在保护区或者室内生活。就elephant而言,它吃什么我不知道,可能食草。兴奋起来嚎叫。然而这等动物只能在保护区生活。
0 0
- 一个程序尽可能多的实现Java面向对象编程的概念
- Java中面向对象的编程概念
- 面向对象编程的概念
- 面向对象编程的概念
- 面向对象编程的概念
- Java面向对象【经典案例】参数:尽可能简单的转换
- JAVA面向对象的概念
- Java面向对象的概念
- java学习之路 之 面向对象编程-面向对象与面向过程及面向对象的思想概念
- VB面向对象编程的一个简单的演示程序
- 课程:面向对象编程的概念
- 面向对象编程的概念和原理
- javan面向对象编程的概念
- Java 面向对象编程概念
- java作业-一个简单的面向对象程序
- 面向对象的概念
- 面向对象的概念
- 面向对象的概念
- c++第一次上机实验-1
- Linux学习笔记1:查看监听所有端口netstat -lntp
- asp.net框架
- 调试工装代码出现 identifier GPIOE is undefined 的解决方案
- 自定义adapter实现对listview内item的控制
- 一个程序尽可能多的实现Java面向对象编程的概念
- Need to include strsafe.h after tchar.h
- org.springframework.dao.DataIntegrityViolationException
- ios开发UI篇-iOS应用数据存储方式(XML属性列表 -plist)e二
- JVM堆栈跟踪之Jstack命令
- Distributed Systems-leader based分布式一致性协议
- 2016.3.7__HTML 基础_第一天
- QeePHP的ACL设置
- 分享截屏总结