JAVA 设计正方形类和长方形类以及一个测试类
发布网友
发布时间:2022-05-20 14:32
我来回答
共1个回答
热心网友
时间:2023-10-29 05:14
首先纠正一下,抽象方法没有方法体,只有在子类的实现中才必须要完成方法体。
/**
* 接口及两个矩形类在Polygon包下。测试类位于另外一个包testboard。
*/
/**
* 1、接口
*/
package Polygon;
/**
*
* @author Henry Sun
*/
public abstract interface Circumf {
public abstract double shapeCir();
}
/**
* 2、正方形类
*/
package Polygon;
/**
*
* @author Henry Sun
*/
public class Square implements Circumf{
double length;
public Square(double length){
this.length = length;
}
public double shapeCir(){
return length*4;
}
}
/**
* 3、长方形类
*/
package Polygon;
/**
*
* @author Henry Sun
*/
public class Rectangel implements Circumf{
double length;
double width;
public Rectangel(double length,double width){
this.length = length;
this.width = width;
}
public double shapeCir() {
return (length + width)*2;
}
}
/**
* 4、测试类
*/
package testboard;
import Polygon.*;
/**
*
* @author Henry Sun
*/
public class MainTest {
public void Calculate(){
Square oSquare = new Square(1.5);
Rectangel oRectangel = new Rectangel(1.2, 1.6);
double dPerimeterS = oSquare.shapeCir();
double dPerimeterP = oRectangel.shapeCir();
System.out.println("Square.Perimeter = " + dPerimeterS + "\nRectangle.Perimeter = " + dPerimeterP);
}
}