面向对象之经典讲解(抽象类)
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操作符从一个抽象类创建一个实例。
相关文章推荐
- 面向对象题目,经典点的(java的 也可以用c#做下)
- 面向对象--接口与抽象类的恩恩怨怨
- 深入浅出讲解什么是面向对象思想
- 面向对象高级续2(instanceof关键字、抽象类和接口的应用、Object类、包装类)
- C#基础系列(9)-- 第三部分 面向对象相关 -- 抽象类与接口(2)
- [原创]面向对象理解·抽象类和派生类理解和使用
- js面向对象经典实例
- php面向对象全攻略 (十二) 抽象方法和抽象类
- PHP5面向对象中抽象类和抽象方法的应用
- java面向对象之接口与抽象类
- 经典的文章----面向对象思想 1
- c#接口和抽象类的区别 ---- 面向对象概念
- 面向对象多态的实现(抽象类Abstract和接口Interface的用法区别)
- 面向对象讲解(2)
- c#接口和抽象类的区别 ---- 面向对象概念
- 经典的文章----面向对象思想 2
- 体现JAVA中的面向对象思想,接口(抽象类)的用处 :饲养员给动物喂食物
- PHP面向对象之旅:抽象类继承抽象类
- 面向对象讲解(二)
- 面向对象高级续1(抽象类、接口的概念、对象的多态性)