父类:
public abstract class Shape {
private double perimeter;
private double area;
public double getPerimeter() {
return perimeter;
}
public void setPerimeter(double perimeter) {
this.perimeter = perimeter;
}
public double getArea() {
return area;
}
public void setArea(double area) {
this.area = area;
}
public Shape(double perimeter,double area){
this.perimeter=perimeter;
this.area=area;
}
public Shape(){
}
}
子类:
package com.sun;
public class Rectangle extends Shape {
public Rectangle(double perimeter, double area) {
super(perimeter, area);
// TODO Auto-generated constructor stub
}
}
子类:
package com.sun;
public class Circle extends Shape {
public Circle(double perimeter, double area) {
super(perimeter, area);
// TODO Auto-generated constructor stub
}
}
测试类:
package com.sun;
public class Test {
public static void main(String[] args) {
int x=3; int y=4; int r=5; double π=3.14;
Shape cc=new Rectangle((x+y)*2,x*y);
Shape dd=new Circle(2*π*r,π*r*r);
System.out.println("矩形的周长和面积为:"+cc.getPerimeter()+
"厘米"+" "+cc.getArea()+"平方米");
System.out.println("圆形的周长和面积为:"+dd.getPerimeter()+
"厘米"+" "+dd.getArea()+"平方米");
}
}
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦