java定义一个接口
发布网友
发布时间:2022-04-25 20:26
我来回答
共3个回答
热心网友
时间:2022-06-17 03:49
将以下四个文件放在同一个目录下编译运行:
文件1:
/**MyApp.java**/
public class MyApp {
public static void main(String[] args) {
IBaseGraphics c1 = new Circle(5);
IBaseGraphics r1 = new Rectangle(3,4);
c1.printData();
r1.printData();
}
}
文件2:
/**IBaseGraphics.java**/
public interface IBaseGraphics {
public double computeCircumference();//计算周长
public double computeSquare ();//计算面积
public void printData();
}
文件3:
/**Circle.java**/
public class Circle implements IBaseGraphics {
private double r = 0.0;
public Circle(double r){
this.r = r;
}
public double computeCircumference() {
return 2*Math.PI*r;
}
public double computeSquare() {
return Math.PI*r*r;
}
public void printData() {
System.out.println("该圆形的周长:"+this.computeCircumference()+";面积:"+this.computeSquare());
}
}
文件4:
/**Rectangle.java**/
public class Rectangle implements IBaseGraphics {
private double width = 0;
private double length =0;
public Rectangle(double width, double length){
this.width = width;
this.length = length;
}
public double computeCircumference() {
return 2*(width+length);
}
public double computeSquare() {
return width * length;
}
public void printData() {
System.out.println("该长方形的周长:"+this.computeCircumference()+";面积:"+this.computeSquare());
}
}
热心网友
时间:2022-06-17 03:49
package day0422;
public interface Shap {
public double getPerimeter();//计算周长
public double getArea();//计算面积
}
、、、、、、、、、、、、、、、、、、、、、、、、、、
package day0422;
public class Circle implements Shap {
public static double radius;//园的半径
public void setRadius(double d){
radius=d;
}
public double getRadius(){
return radius;
}
public double getArea() {
// TODO Auto-generated method stub
return Math.PI*radius*radius;
}
public double getPerimeter() {
// TODO Auto-generated method stub
return 2*Math.PI*radius;
}
}
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
package day0422;
public class Rectangle implements Shap{
public double longer;//长
public double wid;//宽
public Rectangle(){
}
public Rectangle(double longer,double wid){
this.longer=longer;
this.wid=wid;
}
public double getLonger() {
return longer;
}
public void setLonger(double longer) {
this.longer = longer;
}
public double getWid() {
return wid;
}
public void setWid(double wid) {
this.wid = wid;
}
public double getArea() {
// TODO Auto-generated method stub
return longer*wid;
}
public double getPerimeter() {
// TODO Auto-generated method stub
return 2*longer+2*wid;
}
}
热心网友
时间:2022-06-17 03:50
public interface Cal{
public double calArea(double args[]);
public double calCircleLen(double args[]);
}