abstract class Message {
private String type;
public abstract String getConnectInfo();
public void setType(){
this.type = type ;
}
public String getType() {
return this.type;
}
}
class DatabaseMessage extends Message{
public String getConnectInfo() {
return "数据库连接信息";
}
}
public class Test {
public static void main(String[] args) {
DatabaseMessage msg =new DatabaseMessage();
msg.getConnectInfo();
System.out.println(msg.getConnectInfo());
}
}
子类很少会继承一个完善的类,在以后父类的设计中,要优先考虑的一定是一个抽像类。
而抽象类是不能实例化的。
要使用抽象类必须有:
1 抽象类必须有子类,子类使用一个extends继承抽象类
2 抽象类的子类不是抽象类,一定要复写抽象类中的全部抽象方法
3 抽象类的对象实例化可以利用对象多态性通过子类向上转型完成