java例子6:抽象类,形状

[root@gdc1000 java]# cat Shape.java 
public abstract class Shape {
	public abstract double area();
	public abstract double circumference();
}

class Circle extends Shape {
	public static final double PI = 3.1415;
	protected double r;
	public Circle(double r) { this.r = r; }
	public double getRadius() { return r; }
	public double area() { return PI*r*r; }
	public double circumference() { return 2*PI*r; }
}

class Rectangle extends Shape {
	protected double w, h;
	public Rectangle(double w, double h) {
		this.w = w; 
		this.h = h;
	}
	public double area() {
		return w * h;
	}
	public double circumference()
	{
		return 2*(w+h);
	}
}
[root@gdc1000 java]# 


[root@gdc1000 java]# cat ShapeTest.java 
public class ShapeTest {
	public static void main(String[] args) {
		int input = 0;
		if(0 == args.length)
                {
			System.out.println("default input");
			input = 0;		
		}
		else
		{
			input = Integer.parseInt(args[0]);
		}	
		Shape c = new Circle(input);
		
		System.out.println(c.area());
		Shape rect = new Rectangle(input, input);
		System.out.println(rect.area());
	}
	
}


[root@gdc1000 java]# 

这个类中抽象了2个属性,使用了关键词abstract.韩国的继承者们使用了什么东东?extends。

接口才会用到implements




你可能感兴趣的:(java例子6:抽象类,形状)