package test;
//创建Cricle类实现接口Shape2D,重写其中的方法
public class Cricle implements Shape2D {
double radius;
public Cricle(double radius){
this.radius=radius;
}
@Override
public double grith() {
// TODO Auto-generated method stub
return 2*PI*radius;
}
@Override
public double area() {
// TODO Auto-generated method stub
return PI*radius*radius;
}
}
package test;
import java.util.Scanner;
//创建InterfaceExample类实现图形的周长、面积的计算(要求在控制台输入圆形的半径、矩形的长和宽)
public class InterfaceExample {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.print("请输入圆形的半径:");
double radius=input.nextDouble();
System.out.print("请输入长方形的长:");
double length=input.nextDouble();
System.out.print("请输入长方形的宽:");
double width=input.nextDouble();
Cricle cr=new Cricle(radius);
Rectangle re=new Rectangle(length,width);
System.out.println("输出圆的周长:"+cr.grith());
System.out.println("输出圆的面积:"+cr.area());
System.out.println("输出长方形的周长:"+re.grith());
System.out.println("输出长方形的面积:"+re.area());
}
}
package test;
//创建Rectangle类实现接口Shape2D,重写其中的方法
public class Rectangle implements Shape2D {
double length;
double width;
public Rectangle(double length,double width){
this.length=length;
this.width=width;
}
@Override
public double grith() {
// TODO Auto-generated method stub
return 2*(length+width);
}
@Override
public double area() {
// TODO Auto-generated method stub
return length*width;
}
}
package test;
//创建接口Shape2D,其中定义常量PI为3.14并定义周长与面积的计算方法grith()和area()
public interface Shape2D {
final double PI=3.14;
double grith();
double area();
}