您的位置:首页 > 其它

面向对象之经典讲解(抽象类)

2016-10-29 00:12 190 查看
public **abstract** class GeometricObject {
private String color="white";
private boolean filled;
private java.util.Date dateCreated;

**protected** GeometricObject()
{
}

**protected** GeometricObject(String color,boolean filled)
{
dateCreated=new java.util.Date();
this.color=color;
this.filled=filled;
}

public String color()
{
return this.color;
}

public void setColor(String color)
{
this.color=color;
}

public boolean isFilled()
{
return this.filled;
}

public void setFilled(boolean filled)
{
this.filled=filled;
}

public java.util.Date getDateCreated()
{
return dateCreated;
}

public String toString()
{
return "created on "+dateCreated+"\ncolor: "+color+
" and filled: "+filled;
}

**public abstract double getArea();**

**public abstract double getPerimeter();**
}


抽象类不能用new操作符创建它的实例。抽象方法只有定义而没有实现,它的实现由子类提供。一个包含抽象方法的类必须声明为抽象类。

抽象类的构造方法定义为protected,因为它只能被子类使用。

public class Circle extends GeometricObject{
private double radius;

public Circle()
{
}

public Circle(double radius)
{
this.radius=radius;
}

public Circle(double radius,String color,boolean filled)
{
super(color, filled);
this.radius=radius;
}

public double getRadius()
{
return this.radius;
}

public void setRadius(double radius)
{
this.radius=radius;
}

@Override
public double getArea() {
return radius*radius*Math.PI;
}

@Override
public double getPerimeter() {
return 2*radius*Math.PI;
}

}


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;
}

public Rectangle(double width,double height,String color,boolean filled)
{
super(color, filled);
this.width=width;
this.height=height;
}

public double getWidth()
{
return this.width;
}

public void setWidth(double width)
{
this.width=width;
}

public double getHeight()
{
return this.height;
}

public void setHeight(double height)
{
this.height=height;
}

@Override
public double getArea()
{
return this.width*this.height;
}

@Override
public double getPerimeter()
{
return 2*(this.width+this.height);
}

}


public class TestGeometricObject {
public static void main(String[] args) {
GeometricObject geoObject1=new Circle(5);
GeometricObject geoObject2=new Rectangle(5,3);

System.out.println("The two objects have the same area? "+
equalArea(geoObj
4000
ect1, geoObject2));

displayGeometricObject(geoObject1);
displayGeometricObject(geoObject2);
}

public static boolean equalArea(GeometricObject object1,
GeometricObject object2)
{
return object1.getArea()==object2.getArea();
}

public static void displayGeometricObject(GeometricObject object)
{
System.out.println("");
System.out.println("The area is "+object.getArea());
System.out.println("The perimeter is "+object.getPerimeter());
}

}


【运行结果】:

The two objects have the same area? false

The area is 78.53981633974483

The perimeter is 31.41592653589793

The area is 15.0

The perimeter is 16.0

抽象类的几点说明:

(1)抽象方法不能包含在非抽象类中。 如果抽象父类的子类不能实现所有的抽象方法,那么子类也必须定义为抽象的。换句话说,在抽象类扩展的非抽象子类中,必须实现所有的抽象方法。还要注意到,抽象方法是非静态的。

(2)抽象类是不能用new操作符来初始化的。但是,仍然可以定义它的构造方法,这个构造方法在它的子类的构造方法中调用。例如,GeometriObject类的构造方法在Circle类和Rectangle类中调用。

(3)包含抽象方法的类必须是抽象的。 但是,可以定义一个不包含抽象方法的抽象类。在这种情况下,不能使用new操作符创建该类的实例。这种类是用来定义新子类的基类的。

(4)子类可以覆盖父类的方法并将它定义为abstract。这是很少见的,但是它在当父类的方法实现在子类中变得无效时是很有用的。在这种情况下,子类必须定义为abstract。

(5)即使子类的父类是具体的,这个子类也可以是抽象的。例如Object类是具体的,但是它的子类如GeometricObject可以是抽象的。

(6)不能使用new操作符从一个抽象类创建一个实例。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: