abstract用来修饰类和方法
使用规则
- 修饰的类即为抽象类,不能被实例化
- 构造方法和static方法不能是抽象的
- 父类的抽象方法可以在子类中实现,抽象类可以具有指向子类对象的对象引用
抽象类中的方法既可以是抽象方法也可以是具体方法,但是一个类中只要含有抽象方法就是就是抽象类
abstract class Shape {//形状类
protected double length;//长
protected double width;//宽
public Shape(double length,double width){
this.length=length;
this.width=width;
}
public abstract double area();//计算面积(抽象方法)
}
public class Rectangle extends Shape {//矩形
Rectangle(final double num,final double num1){
super(num,num1);
}
public double area(){
return length*width;
}
}
public class Triangle extends Shape {//三角形
Triangle(final double num,final double num1){
super(num,num1);
}
public double area(){
return length*width/2;
}
}
public class ShapeTest {
public static void main(String[] args){
Judge judge = new Judge();
System.out.println("矩形的面积是:"+judge.result(new Rectangle(4,5)));
System.out.println("三角形的面积是:"+judge.result(new Triangle(4,5)));
}
}