《接口》————————定义一个接口Area,其中包含一个计算面积的抽象方法calculateArea(), 然后设计MyCircle和MyRecaangle两个类都实现这个接口中的方法ca

public class Homework04 {
	public static void main(String[] args) {
		//测试MyCircle
		MyCircle mc = new MyCircle(2);
		double area1 = mc.calculateArea();
		System.out.println(String.format("%.3f", area1));
		//测试Myrectangle
		MyRectangle mr = new MyRectangle(2,3);
		double area2 = mr.calculateArea();
		System.out.println(area2);
	}

}
//接口Area
interface Area {
	public abstract double calculateArea();
}
//MyCircle类
class MyCircle implements Area{
	double radius;
	public MyCircle() {
		super();
	}
	public MyCircle(double radius) {
		super();
		this.radius = radius;
	}
	@Override
	public double calculateArea() {
		return Math.PI*Math.pow(radius, 2);
	}
}
//MyRectangle类
class MyRectangle implements Area{
	double length1;
	double length2;
	public MyRectangle() {
		super();
	}
	public MyRectangle(double length1,double length2) {
		super();
		this.length1 = length1;
		this.length2 = length2;
	}
	@Override
	public double calculateArea() {
		return length1*length2;
	}
}

 

你可能感兴趣的:(《接口》————————定义一个接口Area,其中包含一个计算面积的抽象方法calculateArea(), 然后设计MyCircle和MyRecaangle两个类都实现这个接口中的方法ca)