码迷,mamicode.com
首页 > 其他好文 > 详细

【九】抽象类和抽象函数

时间:2015-01-12 23:50:21      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:

 1 class Chinese extends AbsPerson{
 2 
 3     Chinese(){
 4         super();
 5         System.out.println("Chinese的构造函数");
 6     }
 7     //上面是该抽象类的构造函数
 8     void eat(){
 9         System.out.println("用筷子吃饭");
10     }
11     //override复写的抽象类里面的抽象函数,这样Chinese就可以用了
12 }
 1 abstract class AbsPerson{
 2     AbsPerson(){
 3         System.out.println("AbsPerson的构造函数");
 4     }
 5     //抽象类的构造函数,抽象类也是有构造函数的,在生成对象的时候会被调用
 6     String name;
 7     int age;
 8     
 9     void introduce(){
10         System.out.println("my name is " + name +",my age is " + age);
11     }
12     
13     abstract void eat();
14 }
1 class TestAbs{
2     public static void main(String args []){
3         AbsPerson ap = new Chinese();
4         ap.eat();
5     }
6 }

这个test里面也用到了向上转型,还有调用的是指向的子类的方法这个点

 

 

 

要断电了,先写到这里

【九】抽象类和抽象函数

标签:

原文地址:http://www.cnblogs.com/codetochangetheworld/p/4220094.html

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