老师检查下
来源:4-4 编程练习
罗杰明
2019-11-27 20:39:10
public abstract class Shape {
public abstract void area();
}
public class Circle extends Shape{
//属性:圆的半径r
private int r;
//创建带参构造方法以及无参构造方法
public Circle(){}
public Circle(int r){
this.setR(r);
}
//创建针对半径的赋值和取值方法
public void setR(int r){
this.r=r;
}
public int getR(){
return r;
}
//重写父类中area()方法
public void area(){
double i;
System.out.println("圆的面积为:"+(i=(this.r*Math.PI)));
}
}
public class Rectangle extends Shape {
//属性:矩形的长lenghth、宽wide
private int lenghth;
private int wide;
//创建带参构造方法以及无参构造方法
public Rectangle(){}
public Rectangle(int lenghth,int wide){
this.setLenghth(lenghth);
this.setWide(wide);
}
//创建针对长、宽的赋值和取值方法
public int getLenghth() {
return lenghth;
}
public void setLenghth(int lenghth) {
this.lenghth = lenghth;
}
public int getWide() {
return wide;
}
public void setWide(int wide) {
this.wide = wide;
}
//重写父类的area()方法
public void area(){
double i;
System.out.println("矩形的面积为:"+(i=(this.lenghth*this.wide)));
}
}
public class Test1 {
public static void main(String[] args) {
//创建类的实例,并分别对圆的半径和矩形的长宽进行赋值
Shape s=new Circle(22);
Shape s1=new Rectangle(12,3);
//调用area()方法,输出结果
s.area();
s1.area();
}
}
1回答
好帮手慕小尤
2019-11-28
已完成练习,棒棒哒~ 继续加油!
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
相似问题