example

本文介绍了一个简单的Java程序,该程序定义了不同形状(如正方形、矩形和圆形)的面积计算方法。通过继承和接口实现的方式,实现了各种形状面积的具体计算逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class shape implements area{
	public double Area;
	public double getArea(){
		return Area;
	}
	public void setArea(){
	}
	public void setArea(double Area){
		this.Area=Area;
	}
	public double calcArea(){//not good function
		assert Area<=0 : "wrong shape";
		return Area;
	}
	public void printInfo(){
		System.out.println("This is shape: Area="+getArea());
	}
}

public interface area {
    public double getArea();
    public double calcArea();
    public void setArea(double Area);
}


public class square extends shape implements area{
    private double sideLength;

    public square(double sideLength){
        this.sideLength=sideLength;
        setArea();
    }
    public double getSideLength(){
        return sideLength;
    }

    @Override
    public void setArea() {
        // TODO Auto-generated method stub
        super.setArea(calcArea());
    }

    @Override
    public double calcArea() {
        assert sideLength>0 : "wrong sideLength";
        // TODO Auto-generated method stub
        return sideLength*sideLength;
    }
    public void printInfo(){
        System.out.println("This is square:"+" sideLength="+sideLength+", Area="+getArea());
    }
}


public class rectangular extends shape implements area{
    private double width,height;
    
    public rectangular(double width,double height){
        this.width=width;this.height=height;
        setArea();
    }
    public double getWidth(){
        return width;
    }
    public double getHeight(){
        return height;
    }
    @Override
    public void setArea() {
        // TODO Auto-generated method stub
        super.setArea(calcArea());
    }

    @Override
    public double calcArea() {
        assert width > 0 : "wrong width ";
        assert height > 0 : "wrong height ";
        // TODO Auto-generated method stub
        return width *height;
    }
    
    public void printInfo(){
        System.out.println("This is rectangular:"+" width="+width+", height="+height+", Area="+getArea());
    }
}


public class circle extends shape implements area{
    private double radius;
    private final double pi = 3.1415926;

    public circle(double radius){
        this.radius = radius;
        setArea();
    }
    public double getRadius(){
        return radius;
    }
    @Override
    public void setArea() {
        // TODO Auto-generated method stub
        super.setArea(calcArea());
    }

    @Override
    public double calcArea() {
        assert radius > 0 : "wrong radius";
        // TODO Auto-generated method stub
        return pi*radius*radius;
    }
    public void printInfo(){
        System.out.println("This is circle:"+" radius="+radius+", Area="+getArea());
    }
}


public class shapeTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        shape myShape=new shape();
        myShape.printInfo();
        
        square mySquare=new square(1);
        mySquare.printInfo();
        
        rectangular myRectangular=new rectangular(2,3);
        myRectangular.printInfo();
        
        circle myCircle=new circle(4);
        myCircle.printInfo();
        
        shape myUnknown=(shape)new circle(10);
        myUnknown.printInfo();
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值