抽象类与抽象方法
1.抽象类
//抽象类是个普通类,不能实例化new
abstract class A{
//使用抽象类,限制实例化
}
public class Point {
public static void main(String[] args) {
//抽象类不能被new
A a =new A() ;
}
}
2.抽象方法
//非抽象类TA继承抽象类A,必须重写父类的所有方法。
abstract class A{
public abstract void run();
}
class TA extends A{
@Override
public void run(){
System.out.println("测试A");
}
}
//通常在继续关系中,父类为了规范代码,而且父类本身new出来没有意思,我们就使用抽象类来修饰父类,也可以设置为抽象方法。
public class Point {
public static void main(String[] args) {
TA ta = new TA();
ta.run();
}
}
3.final关键字
//需求
//Penguin类不希望再被其他类继承 使用final类
//不希望方法被重写 使用final方法
//属性值不希望被修改 使用常量 常量名通常全大写