1.继承实例分析。。。
2016-01-20 11:27
288 查看
class Animal { int eye=2; //定义动物属性有眼睛和腿 int leg=4; public void eat() { //无参构造方法 System.out.println("正在吃。。。。。。"); } public void run() { System.out.println("正在飞。。。。。。"); } } class Bird extends Animal { //小鸟类继承父类动物 public void fly(){ //定义小鸟类 属性在飞 System.out.println("正在飞"); } } public class Inherite { public static void main(String[] args) { //创建Inherite类调用父类属性方法 Animal an=new Animal(); //创建Animal类的对象并为其分配内存 System.out.println(an.eye); System.out.println(an.leg); an.eat(); an.run(); System.out.println("%%%%%%%%%%"); Bird bird1=new Bird(); //创建鸟的对象并为其分配内存 System.out.println(bird1.eye); System.out.println(bird1.leg); bird1.eat(); bird1.fly(); } }
4
正在吃。。。。。。
正在飞。。。。。。
%%%%%%%%%%
2
4
正在吃。。。。。。
正在飞
在Bird类中并没有定义eye属性但是因为他继承了Animal类,所以可以直接输出System.out.println (bird1.eye);
也可以重新给leg属性赋值,Animal类中的方法可以直接调用。
相关文章推荐
- 很好用的canvas
- jsonp跨域问题
- JS 添加千分位,测试可以使用
- jsp标签库(jstl)
- 函数名前后加const修饰符的区别
- C语言实现龙贝格求积
- Ecslipse安装Svn插件的两个方法
- CodeIgniter配置之database.php用法实例分析
- NYOJ 246 Human Gene Functions dp
- 责任链模式【Chain of Responsibility Pattern
- 几秒前,几分钟前,几小时前,几天前,几月前,几年前的java实现
- Android中的IPC机制(3)-Messenger
- 电话号码等常见的正则表达式
- vsftpd 500
- Bugtags:移动时代首选 Bug 管理系统
- Gradle
- Ubuntu 12.04上编译Vim7.4的时候遇到“no terminal library found”问题
- kuangbin_MST B (POJ 1287)
- django 错误整理
- Java 多字符分割字符串