java,面向对象程序设计?
3个回答
展开全部
做好了,参考如下
public interface Shape {
public static final double PI = 3.14;
double getGirth();
double getArea();
}
public class Area {
double getShapeGrith(Shape shp) {
return shp.getGirth();
}
double getShapeArea(Shape shp) {
return shp.getArea();
}
}
public class Circle implements Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double getGirth() {
return 2*PI*radius;
}
@Override
public double getArea() {
return PI*radius*radius;
}
}
public class Rectangle implements Shape {
private double width;
private double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
@Override
public double getGirth() {
return 2*(width+height);
}
@Override
public double getArea() {
return width*height;
}
}
public interface Shape {
public static final double PI = 3.14;
double getGirth();
double getArea();
}
public class Area {
double getShapeGrith(Shape shp) {
return shp.getGirth();
}
double getShapeArea(Shape shp) {
return shp.getArea();
}
}
public class Circle implements Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double getGirth() {
return 2*PI*radius;
}
@Override
public double getArea() {
return PI*radius*radius;
}
}
public class Rectangle implements Shape {
private double width;
private double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
@Override
public double getGirth() {
return 2*(width+height);
}
@Override
public double getArea() {
return width*height;
}
}
追答
public class ShapeTest {
public static void main(String[] args) {
//创建一个圆
Area area = new Area();
Circle c = new Circle(2.0d);
System.out.println("圆的面积:"+area.getShapeArea(c));
System.out.println("圆的周长:"+area.getShapeGrith(c));
//创建一个矩形
Rectangle rect = new Rectangle(2d,5d);
System.out.println("矩形的面积:"+area.getShapeArea(rect));
System.out.println("矩形的周长:"+area.getShapeGrith(rect));
}
}
这是测试类
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
先做一个抽象类,然后把需要的抽象方法都写进去,再建两个下面的两个类,把抽象方法全部实现。接口和抽象类类似,写上方法,后面去实现,主函数直接用打印的方法出结果。
abstract class name{}
interface name{}
然后写抽象方法就行了
abstract class name{}
interface name{}
然后写抽象方法就行了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
看看以后找工作了能不能请教别人哈
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询