Java抽象类

声明:本博客参考《Java语言程序设计与数据结构》此书以及部分资料

抽象类的基本概念

  • 所有的对象都是通过类来描绘的。 但并不是所有的类都是用来描绘对象。如果一个类中没有包含足够的信息来描绘一个具体的对象,这样的类就是抽象类。
  • 抽象类可以含有零个至多个普通方法,也可以含零个或多个抽象方法。
  • 不论抽象类是否含抽象方法,其都不允许实例化,只能作为其他类的基类。
  • 若父类是抽象类,子类不想要成为抽象类,则子类必须将父类所有抽象方法重写为带方法体的普通方法
  • 只能被抽象类或类继承,不支持多重继承

抽象类的实例使用

抽象类的书写

/*抽象类的书写*/
package exercise;

//public abstract class + 类名   抽象类的书写
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 isFilled() {
		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;
	}
	
	//	Abstract method getArea      抽象方法
	public abstract double getArea();
	
	//	Abstract method getPerimeter  
	public abstract double getPerimeter();
	
}

抽象类的继承(同一般继承)

package exercise;

/*定义一个圆 类*/
public class Circle extends GeometricObject{  //依旧使用extends引用
	private double radius;
	
	public Circle() {
	}
	
	public Circle(double radius) {
		this.radius=radius;
	}
	
	public Circle(double radius,String color,boolean filled) {
		this.radius=radius;
		setColor(color);
		setFilled(filled);
	}
	
	public double getRadius() {
		return radius;
	}
	
	public void setRadius(double radius) {
		this.radius=radius;
	}
	
	public double getArea() {
		return radius * radius * Math.PI;
	}
	
	public double getDiameter() {
		return 2 * radius;
	}
	
	public double getPerimeter() {
		return 2 * radius * Math.PI ;
	}
	
	public void printCicle() {
		System.out.println("Thecircle is created " + getDateCreated() + " and the radius is " + radius );
	}
}
package exercise;

/*定义一个矩形 类*/
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) {
		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 setHeoght(double height) {
		this.height = height;
	}
	
	public double getArea() {
		return width * height;
	}
	
	public double getPerimeter() {
		return 2 * (width + height);
	}
}

抽象类的测试

package exercise;

public class TestGeometricObject {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		GeometricObject geoObject1 = new Circle(5);
		GeometricObject geoObject2 = new Rectangle(5,3);
		
		System.out.println("The  two objects have the same area? " + equalArea(geoObject1,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());
	}
}

结果:
测试结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值