有 4 个 java 文件,分别是 Shape.java,Square.java,Circle.java,ShapeTest.java。其内容如下:
Shape.java
package graphics;
interface class Shape {
double getArea();
double getPerimeter();
}
Square.java:
package graphics;
public class Square implements Shape {
private double x;
private double y;
private double width;
private double height;
public Square(double x, double y, double width, double height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public Square(double x0, double y0, double x1, double y1) {
double xmin = x0 > x1 ? x1 : x0;
double ymin = y0 > y1 ? y1 : y0;
this.x = xmin;
this.y = ymin;
this.width = x0 + x1 - 2*xmin;
this.height = y0 + y1 - 2*ymin;
}
@Override
public double getArea() {
return this.width * this.height;
}
@Override
public double getPerimeter() {
return 2 * (this.width + this.height);
}
}
import java.util.Scanner;
public class Test extends ShapeTester{
public static void main(String[] args) {
Test t = new Test();
t.shapeTester();
}
}
class ShapeTester {
public void shapeTester(){
Scanner input = new Scanner(System.in);
System.out.println("请输入该矩形的宽:");
double width = input.nextDouble();
System.out.println("请输入该矩形的高:");
double height = input.nextDouble();
Shape s= new Shape(width,height);
Square ss = new Square();
ss.square(s.getWidth(), s.getHeight());
Circle c = new Circle();
c.circle(s.getWidth(), s.getHeight());
}
}
class Shape{
private double width;
private double height;
public Shape() {
super();
}
public Shape(double width, double height) {
super();
this.width = width;
this.height = height;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
}
class Square{
public void square(double a,double b){
double s = a*b;
System.out.println("该矩形面积是:"+s);
}
}
class Circle{
public void circle(double a, double b){
double c = 2*(a+b);
System.out.println("该矩形周长是:"+c);
}
}
不会(滑稽)
不会
可东哥demo