如果创建一个类的实例没有任何意义的话,且还想阻止使用者将其实例化,则将其申明为abstract。
###抽象类的官网说明https://docs.oracle.com/javase/tutorial/java/IandI/abstract.htmlAn abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.###接口的官网说明//https://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html Interfaces in JavaInterfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces.Extension is discussed later in this lesson.
抽象类的定义:
包含抽象方法的类一定是抽象类;但抽象类可以不包含抽象方法(有可能是为了阻止使用者实例化该类)
public abstract class Instrument {
public void info(){
System.out.println("========");
}
public static void main(String[] args) {
// Instrument instrument = new Instrument();
Instrument instrumentInner = new Instrument()
{
@Override
public void info() {
System.out.println("通过内部类的形式进行了重写");
}
};
instrumentInner.info();
}
}
编码建议:
一般情况下,将可变的方法声明为抽象方法
