package bean;
public abstract class Shape {
public abstract double getPeremeter();
public abstract double getArea();
}
package bean;
public class Square extends Shape{
private double length;
private double width;
private double pemimter;
private double area;
@Override
public double getPeremeter() {
// TODO Auto-generated method stub
return (this.length+this.width)*2;
}
@Override
public double getArea() {
// TODO Auto-generated method stub
return this.width*this.length;
}
}
package bean;
public class Circle extends Shape{
public double radius;
@Override
public double getPeremeter() {
// TODO Auto-generated method stub
return this.radius*2*Math.PI;
}
@Override
public double getArea() {
// TODO Auto-generated method stub
return this.radius*this.radius*Math.PI;
}
}
package test;
import bean.Circle;
import bean.Square;
public class ShapeTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Square h=new Square(8,8);
System.out.println("长方形的面积:"+h.getArea());
System.out.println("长方形的周长:"+h.getPeremeter());
Circle i=new Circle(8);
System.out.println("圆的面积:"+i.getArea());
System.out.println("圆的周长:"+i.getPeremeter());
}
}
这2条语句错误,因为2个类里面没有对应的构造函数
Square h=new Square(8,8);
Circle i=new Circle(8);
修改代码如下(测试类不用改):
package bean;
public abstract class Shape {
public abstract double getPeremeter();
public abstract double getArea();
}
package bean;
public class Square extends Shape{
private double length;
private double width;
private double pemimter;
private double area;
//添加构造函数
public Square(double length,double width){
this.length = length;
this.width = width;
}
@Override
public double getPeremeter() {
// TODO Auto-generated method stub
return (this.length+this.width)*2;
}
@Override
public double getArea() {
// TODO Auto-generated method stub
return this.width*this.length;
}
}
package bean;
public class Circle extends Shape{
public double radius;
//添加构造函数
public Circle(double radius){
this.radius = radius;
}
@Override
public double getPeremeter() {
// TODO Auto-generated method stub
return this.radius*2*Math.PI;
}
@Override
public double getArea() {
// TODO Auto-generated method stub
return this.radius*this.radius*Math.PI;
}
}
请采纳,谢谢!
Square和Circle没有写构造函数
Square中加入构造函数
public Square(int a, int b) { super(); }
Circle中加入构造函数
public Circle(int i) { }
构造函数里面的内容你自己根据需要补充
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632
非常感谢您使用有问必答服务,为了后续更快速的帮您解决问题,现诚邀您参与有问必答体验反馈。您的建议将会运用到我们的产品优化中,希望能得到您的支持与协助!
速戳参与调研>>>https://t.csdnimg.cn/Kf0y