在JAVA中,定义一个接口,接口中有四个抽象方法:求面积方法、求周长方法、显示面积方法及显示周长方法。

定义Circle类和Rectangle类分别实现接口,在主类中实现显示圆盒举行面积和周长

使用java多态RIIT运行时类型识别,对象的好处是使我们能替换对象,而接口的好处是让我们更好的替换对象,这个接口应该是 形状 Shape ,形状没有具体的概念应该为抽象类或者接口,而圆形,长方形为具体的形状

public class MainTest{

public static void main(String[] args) {

Shape circle=new Circle(20),rect=new Rectangle(100.0,50.0);
circle.showArea();
rect.showArea();

}

}

interface Shape{
public double getArea();//求面积
public double getGirth();//求周长
public void showArea();
public void showGirth();
}

class Circle implements Shape{

private double r;
public double getR() {
return r;
}

public void setR(double r) {
this.r = r;
}

public Circle(){}
public Circle(double r){
this.r=r;
}
//求面积
public double getArea() {
return Math.PI*this.r*this.r;

}

//求周长
public double getGirth() {
return 2*this.r*Math.PI;

}

public void showArea() {
System.out.println("圆面积:"+this.getArea());
}

public void showGirth() {
System.out.println("圆周长:"+this.getGirth());
}

}

//定义长方形
class Rectangle implements Shape{
private double w,h;//长方形的宽和高
public double getW() {
return w;
}

public void setW(double w) {
this.w = w;
}

public double getH() {
return h;
}

public void setH(double h) {
this.h = h;
}

//构造函数
public Rectangle(){
}
//构造函数
public Rectangle(double w,double h){
this.w=w;
this.h=h;
}
public double getArea() {
return this.w*this.h;
}

public double getGirth() {
return 2*(this.w+this.h);

}

public void showArea() {
System.out.println("长方形面积"+this.getArea());
}

public void showGirth() {
System.out.println("长方形周长"+this.getGirth());
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-04-20
/***用继承的方法****/
public class A{//已知一个类A,要想在其他地方访问A里的对象
protected String a = "A类中的a字串";
}
public class B extends A{
public void prt(){
System.out.println(a);//直接访问父类中的protected对象
}
}

/****用接口的方法****/
public interface C{
String c = "C接口类中的c字串";
}
public class D implements C{
public void prt(){
System.out.println(c);//通过实现C接口类,来直接访问C类中所有对象,C必须是接口
}
}

/****用get方法****/
public class E{
private String e = "E接口类中的e字串";
public String getE(){
return this.e;
}
}
public class F{
public void prt(){
E instance = new E();//创建一个E类对象
System.out.println(instance.getE());//通过实现C接口类,来直接访问C类中所有对象,C必须是接口
}
}
第2个回答  2011-04-20
public interface Graphics {
public double circum();
public double area();
}

public class Circle implements Graphics {
public double r;
public double getR() {
return r;
}

public void setR(double r) {
this.r = r;
}

public double area() {
return Math.PI*this.r*this.r;
}

public double circum() {
return 2*this.r*Math.PI;
}

}
public class Rectangle implements Graphics {
public double a;
public double b;

public double getA() {
return a;
}

public void setA(double a) {
this.a = a;
}

public double getB() {
return b;
}

public void setB(double b) {
this.b = b;
}

public double area() {
return this.a*this.b;
}

public double circum() {
return 2*(this.a+this.b);
}

}
public class Test {
public static void main(String[] args) {
Circle c = new Circle();
c.r = 2.0;
System.out.println("圆周长:"+c.circum()+" 圆面积:"+c.area());
Rectangle re = new Rectangle();
re.a = 2;
re.b = 4;
System.out.println("矩形周长:"+re.circum()+" 矩形面积:"+re.area());
}
}本回答被网友采纳
第3个回答  2011-04-20
interface Shape {
public double getarea();

public double getPerimeter();

public void printArea();

public void printPerimeter();
}

class Rectangle implements Shape {

private double width;

private double height;

public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}

public double getarea() {
return width * height;
}

public double getPerimeter() {
return 2 * (width + height);
}

public void printArea() {
System.out.println("Area for rectangle with width = " + width + ", height = " + height + " is: "+ getarea());
}

public void printPerimeter() {
System.out.println("Perimeter for rectangle with width = " + width + ", height = " + height + " is: "+ getPerimeter());
}
}

class Circle implements Shape {
private double radius;

public Circle(double r) {
this.radius = r;
}

public double getarea() {
return Math.PI * radius * radius;
}

public double getPerimeter() {
return 2 * Math.PI * radius;
}

public void printArea() {
System.out.println("Area for circle with r = " + radius + " is: " + getarea());
}

public void printPerimeter() {
System.out.println("Perimeter for circle with r = " + radius + " is: " + getPerimeter());
}

}

public class aa1 {
public static void main(String args[]) {
Shape circle = new Circle(2);

circle.printArea();
circle.printPerimeter();

Shape rectangle = new Rectangle(3, 12);
rectangle.printArea();
rectangle.printPerimeter();

}
}

----------------
Area for circle with r = 2.0 is: 12.566370614359172
Perimeter for circle with r = 2.0 is: 12.566370614359172
Area for rectangle with width = 3.0, height = 12.0 is: 36.0
Perimeter for rectangle with width = 3.0, height = 12.0 is: 30.0
第4个回答  2011-04-20
你是要代码?
相似回答