Java抽象类

2017-04-23  本文已影响8人  133sheiya

/**

*/

abstract class TestAb{
//定义抽象类

String name;
int    age;
String occupation;
//声明抽象方法()
public abstract String talk() ;
//抽象类定义了默认的无参构造函数 子类默认调用父类的构造函数
//若是没有重写默认的构造无参函数,则必须在子类明确调用父类的有参构造函数

// public TestAb () {

// }

public TestAb (String name) {
    
}

}

class StudentTest extends TestAb
{

public StudentTest (String name,int age,String occupation) {
    super("李四");
    this.name = name;
    this.age  = age;
    this.occupation = occupation;
}
//子类必须重写父类的抽象方法

public String talk() {
    return "我的姓名:"+this.name +"年龄:"+this.age +" 职业:"+this.occupation;
    
}

}
public class Abstract {

public static void main(String[]args) {
    StudentTest pS = new StudentTest("张三", 24, "学生");
    System.out.println(pS.talk());
    
}

}

上一篇 下一篇

猜你喜欢

热点阅读