java基础:9.1 抽象类
在前几章,我们学了父类与继承等操作。有时候,一个父类设计得非常抽象,以至于它都没有任何具体的实例。这样的类称为抽象类。
在父类里定义一些方法,但这些方法不能在父类中实现,因为他们的实现取决于几何对象的具体类型。这样的方法称为抽象方法,在方法头中用abstract修饰符表示。抽象方法是非静态的
抽象类和常规类很像,但是不能使用new操作符创建它的实例。抽象方法只有定义而没有实现,它的实现由子类提供。
一个包含抽象方法的类必须声明为抽象类。
抽象类的构造方法定义为protected,因为它只被子类使用。创建一个具体子类的实例时,它的父类的构造方法被调用以初始化父类中定义的数据域。
不能使用new 操作符从一个抽象类创建一个实例,但是抽象类可以用作一种数据类型
GeometricObject[] object = new GeometricObject[10];
object[0] = new Circle(); 是合法的
// GeometricObject.java
// 包含抽象类的父类
public abstract class GeometricObject {
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;
protected GeometricObject() {
dateCreated = new java.util.Date();
}
protected GeometricObject(String color,boolean filled) {
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public boolean ifFilled() {
return filled;
}
public void setFilled(boolean filled) {
this.filled = filled;
}
public java.util.Date getDateCreated(){
return dateCreated;
}
@Override
public String toString() {
return "created on "+dateCreated + "\ncolor" + color +
"and filled :" + filled;
}
public abstract double getArea();
public abstract double getPerimeter();
}
// Circle.java
// 子类1
public class Circle extends GeometricObject{
private double radius;
public Circle(){
}
public Circle(double newRadius){
this.radius = newRadius;
}
protected Circle(double radius,String color,boolean filled) {
this.radius = radius;
setColor(color);
setFilled(filled);
}
/* return radius */
public double getRadius() {
return radius;
}
/* set radius */
public void setRadius(double radius) {
this.radius = (radius >=0) ? radius : 0;
}
public double getArea(){
return radius*radius*Math.PI;
}
public double getPerimeter(){
return 2*radius*Math.PI;
}
public void printCircle() {
System.out.println("the circle is created " + getDateCreated() +
"and the radius is " + radius );
}
}
// Rectangle.java
// 子类2
public class Rectangle extends GeometricObject{
private double width;
private double height;
public Rectangle(){
}
public Rectangle( double width,double height){
this.width = width;
this.height = height;
}
protected Rectangle(double width,double height,String color,boolean filled) {
this.width = width;
this.height = height;
setColor(color);
setFilled(filled);
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getArea(){
return height*width;
}
public double getPerimeter(){
return 2 * (height+ width);
}
}
// 测试程序
public class TestGeometric {
public static void main(String[] args) {
// TODO Auto-generated method stub
GeometricObject g1 = new Circle(5);
GeometricObject g2 = new Rectangle(5,3);
System.out.println("The two objects have the same area? "
+ equalArea(g1,g2) );
displayGeometricObject(g1);
displayGeometricObject(g2);
}
public static boolean equalArea(GeometricObject g1,GeometricObject g2) {
return g1.getArea() == g2.getArea();
}
public static void displayGeometricObject(GeometricObject g) {
System.out.println();
System.out.println("the area is " + g.getArea());
System.out.println("the perimeter is " + g.getPerimeter());
}
}