java007 抽象类 抽象函数

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Birdmotianlun/article/details/50202607

抽象类
抽象函数:只有定义,没有函数体 abstract void fun();
1 一个类中有抽象函数,那么这个类是抽象类
2 抽象类不能生命成对象
3 抽象类的子类可以生成对象。抽象类只能被继承

=========================================================

abstract class person{
String name;
int age;
void introduce(){
System.out.println("我的名字是"+name+"我的年龄是"+age);
}
abstract void eat();//抽象函数
 person(String name,int age){
 this.name=name;
 this.age=age;
 System.out.println("两个参数的构造函数");

 }
}

============主类=======================================================

class test{

public static void main(String args[]){
 person p=new chinese();//向上转型
 p.eat();

}
}

=====================子类===================================

 class chinese extends person{
     String address;
     chinese(String name.int age,String address){//构造函数
         super(name,age);
         this.address=address;
     }
    void eat(){  //复写
      System.out.println("用筷子吃饭");
    }

  }

猜你喜欢

转载自blog.csdn.net/Birdmotianlun/article/details/50202607
今日推荐