建立一个Shape类,有Circle(圆形)和Rect(矩形)子类

Shape类有zhouchang()和area()两种方法。
Rect有cha()用于比较长宽的差,若长大于宽输出“长比宽大”,否则输出“宽比长大”。(正方形)Squ为Rect子类,在Squ类中,重写cha()方法,并输出“长等于宽”
要求:(1)要提供Circle和Rect类要重写父类Shape的zhouchang()和area()方法
      (2)Circle类要有静态常量PI
      (3)为Circle类,Rect类,Squ类提供构造方法,为属性赋初值
      (4)编写测试类,测试上述程序

注意:学习完抽象类后,将该题目中的Shape类改为抽象类,将该类中的zhouchang()方法和area()方法定义成抽象方法

package javasky;
public abstract class Shape {
 private double dLong = 0.00;
 private double width = 0.00;
 private double height = 0.00;
 
 public double getDLong() {
  return dLong;
 }
 public void setDLong(double long1) {
  dLong = long1;
 }
 public double getWidth() {
  return width;
 }
 public void setWidth(double width) {
  this.width = width;
 }
 public double getHeight() {
  return height;
 }
 public void setHeight(double height) {
  this.height = height;
 }
 public void girth() {
  
 }
 
 public void area() {
  
 }
}
 
package javasky;
public class Circle extends Shape { private final static double PI=3.14;
 @Override
 public void area() {
  System.out.println( this.getDLong());
 }

@Override
 public void girth() {
  System.out.println( this.getDLong());
 }

}
package javasky;
public class Rect extends Shape {
 @Override
 public void area() {
  System.out.println("面积为:"+this.getDLong()*this.getWidth());
 }

@Override
 public void girth() {
  System.out.println("周长为:"+(double)2*this.getDLong()+(double)2*this.getWidth());
 }
 
 public void cha(){
  //用于比较长宽的差,若长大于宽输出“长比宽大”,否则输出“宽比长大”。(正方形)
  double result = this.getDLong()-this.getWidth();
  String shape="长为"+this.getDLong()+"长为"+this.getWidth();
  String str="";
  if(result>0){
   str="此图形为长方形。";
  }else{
   str="长等于宽,此图形为正方形。";
  }
  System.out.println(shape+str);
 }
 
 public Rect(double dlong,double width) {
   System.out.println("赋初始值");
   this.setDLong(dlong);
   this.setWidth(width);
 }

}
package javasky;
public class T {
 public static void main(String[] args) {
  Rect rect = new Rect(2.00,2.00);
  rect.cha();
 }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值