Java中的abstract详解
abstract定义
abstract class Person {//抽象类
public abstract String getDescription();//抽象方法
}注意:
abstract class Person {//抽象类
private int id = 10;//抽象类可以包含具体的数据
public abstract String getDescription();//抽象方法
public int getId(){//抽象类可以包含具体的方法
return id;
}
}
public class Main {
public static void main(String[] args){
Person my;
Person mine = new Person();//error 抽象类不能被实例化
}
}总结:
简单示例:
/**
* 抽象类
* @author wangcan
*
*/
abstract class Person {//抽象类
private String name;
public Person(String name) {
this.name = name;
}
public abstract String getDescription();//抽象方法
public String getName(){
return name;
}
}
class Student extends Person{
private String school;
public Student(String name,String school) {
super(name);
this.school = school;
}
@Override
public String getDescription() {
return "a student "+super.getName()+" study in" + school;
}
}public class Main {
public static void main(String[] args){
Person[] people = new Person[2];
people[0] = new Student("小强","优快云");
people[1] = new Student("小明", "CUMT");
for(Person p:people){
System.out.println(p.getDescription());
}
}
}
195



被折叠的 条评论
为什么被折叠?



