1.概念
抽象类是一种不能被实例化的类,定义抽象类的目的主要是为了让其它的类继承和实现。我们通过关键字 abstract 来定义一个抽象类。
注意及要求:
(1) 在抽象类中可以定义零个或者多个抽象方法,也可以定义非抽象方法。
(2)注意抽象类可以不定义抽象方法,都是如果一个类中要定义抽象方法,那这个类必须是抽象类。
(3) 抽象类可以派生子类,但是需要注意的是,如果要派生实体子类(非抽象子类),那么就要把抽象父类中的所有抽象方法实现,而派生抽象子类则不做要求。
(4) 抽象类中,不能定义变量和实现方法(不能实例化对象)。
我的理解:我们可以大概简单理解一下抽象类,其实就是一个模板,它自己不用去实现对象,只是告诉子类有这个对象要去实现。
2.代码示例
我们先定义一个抽象类 Shape,其中包含一个可以返回面积的抽象方法 getArea(),且该类还实现了一个 Comparable接口
abstract class Shape implements Comparable<Shape> {
public abstract Double getArea();
public int compareTo(Shape shape) {
if (this.getArea() > shape.getArea()) {
return 1;
} else if (this.getArea() < shape.getArea()) {
return -1;
} else {
return 0;
}
}
public static Shape max(Shape s1, Shape s2) {
if( s1.compareTo(s2) >= 0 ) {
return s1;
}
else {
return s2;
}
}
}
其中使用了接口Comparable中的非抽象方法 comparableTo(Shape shape) 用来比较当前形状面积和传入形状面积的大小,静态方法 max(Shape s1, Shape s2) 用来比较形状s1和s2面积的大小,并返回大值。
然后我们定义一个Circle(圆)类,继承抽象类Shape。
class Circle extends Shape {
private double radius;
public Circle() {
this.radius = 1.0;
}
public Circle( double radius) {
if( radius > 0 ) {
this.radius = radius;
}
else {
throw new IllegalArgumentException("圆的半径必须大于0");
}
}
public void setRadius( double radius) {
if( radius > 0.0 ) {
this.radius = radius;
}
else {
throw new IllegalArgumentException("圆的半径必须大于0");
}
}
public Double getRadius() {
return radius;
}
public String toString() {
return "Circle(" + radius + ")";
}
public Double getArea() {
return this.radius * this.radius * Math.PI;
}
}
在Circle类中,我们定义了一个私有变量radius(半径),定义了俩个构造方法Circle() 、Circle(double radius),其中Circle()无参构造方法用于初始化圆半径,而Circle(double radius)有参构造方法用传入参数初始化半径,且判断传入参数是否合法,如果不合法则抛出一个异常对象(IllegalArgumentException)。toString()方法用来返回圆形数据信息的字符串。
在Circle类中我们实现了父类Shape中的抽象方法getArea(),来返回圆的面积。
3.代码综合
import java.util.Scanner;
abstract class Shape implements Comparable<Shape> {
public abstract Double getArea();
public int compareTo(Shape shape) {
if (this.getArea() > shape.getArea()) {
return 1;
} else if (this.getArea() < shape.getArea()) {
return -1;
} else {
return 0;
}
}
public static Shape max(Shape s1, Shape s2) {
if( s1.compareTo(s2) >= 0 ) {
return s1;
}
else {
return s2;
}
}
}
class Circle extends Shape {
private double radius;
public Circle() {
this.radius = 1.0;
}
public Circle( double radius) {
if( radius > 0 ) {
this.radius = radius;
}
else {
throw new IllegalArgumentException("圆的半径必须大于0");
}
}
public void setRadius( double radius) {
if( radius > 0.0 ) {
this.radius = radius;
}
else {
throw new IllegalArgumentException("圆的半径必须大于0");
}
}
public Double getRadius() {
return radius;
}
public String toString() {
return "Circle(" + radius + ")";
}
public Double getArea() {
return this.radius * this.radius * Math.PI;
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
try {
Shape c1 = new Circle(scanner.nextDouble());
Shape c2 = new Circle(scanner.nextDouble());
System.out.printf("The max Circle of " + c1 + " and " + c2 + " is " + Shape.max(c1, c2));
} catch (IllegalArgumentException e){
System.out.println(e.getMessage());
}
}
}