(39)异常练习2:类覆盖接口方法,要考虑接口方法是否应该抛出异常,还有两个异常类应该继承哪个选取

有一个圆形和长方形。

都有获取面积。如果面积出现非法的数值,视为是获得面积出现异常

问题通过异常来表示。

现在对这个程序进行基本设计

方法一:本人写的大笑大笑



public class Rectangle {

	private int length;
	private int width;
	Rectangle(int length,int width ){
		this.length=length;
		this.width=width;
	}
	public int getLength() {
		return length;
	}
	public void setLength(int length) {
		this.length = length;
	}
	public int getWidth() {
		return width;
	}
	public void setWidth(int width) {
		this.width = width;
	}
	
}
public class Circle {

    private double radius;
    Circle (double radius){
        this.radius=radius;
    }
    public double getRadius() {
        return radius;
    }
    public void setRadius(double radius) {
        this.radius = radius;
    }
    
}
public class FeiZhengException extends RuntimeException {

    private String message;
    FeiZhengException(String message){
        super(message);    
    }
    
}
public class Test {
    
    public int area(Rectangle re) {
        if(re.getLength()<=0|re.getWidth()<=0) {
            throw new FeiZhengException("输入参数有非正数");
        }
        
        
        return re.getWidth()*re.getLength();
        
    }
    public double area(Circle ci) {
        if(ci.getRadius()<=0) {
            throw new FeiZhengException("输入参数有非正数");
        }
        return 3.14*ci.getRadius()*ci.getRadius();
        
    }

}
public class Demo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Rectangle re=new Rectangle(3,2);
        Test t=new Test();
        System.out.println("长方形的面积为:"+t.area(re));
        Circle ci=new Circle(-7);
        
        System.out.println("圆形的面积为:"+t.area(ci));

    }

}


改进后的代码①:自定义异常类继承异常类

将求面积这个方法,作为一个附加方法,放到接口中

如果父类或者接口的方法中没有异常抛出,那么子类在覆盖方法时,也不可以抛出异常,所以若实现类抛出异常,则接口必须抛出异常


public interface Shape {
    public abstract void area() throws NoValueException;
        
}
public class NoValueException extends Exception {

    private String message;
    NoValueException(String message){
        super(message);
    }
    
}
public class Rectangle implements Shape {
	private int length;
	private int width;
	public Rectangle(int length,int width ){
		/*if(length<=0||width<=0) {
			System.out.println("错了");
		}
		else {
		
		this.length=length;
		this.width=width;
	    }
	    
	    *不能这样做,原因是成员变量都是含有各自的默认值的,所以即使用户输入的数据是错误
	    *的,在这提示“错了”,该调用的面积方法照样会执行,输出0,这是不符合常理的,
	    *因为如果出现负数或者零,这个长方形就不存在,就不应该计算面积。
	    *
	    *犯得错误是:正常流程代码和问题处理代码关系紧密,阅读性差
	    *
	    *只保留正常流程代码,当问题发生时,再进行写问题处理代码
	    */
		this.length=length;
		this.width=width;
	}
	
	public void area() throws NoValueException //如果父类或者接口的方法中没有异常抛出,那么子类在覆盖方法时,也不可以抛出异常
	{
		// TODO Auto-generated method stub
		if(length<=0||width<=0) {
			throw new NoValueException("输入了非法值!!");
		}
		  System.out.println( "长方形的面积为:"+width*length);
	}
}
public static void main(String[] args) {
        // TODO Auto-generated method stub
        Rectangle re=new Rectangle(-3,2);
        try {
            re.area();
        } catch (NoValueException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
}


改进代码三:自定义异常继承RuntimeException类

其实当输入负值时,这个对象就不应该建立成功,程序应立刻停止



public interface Shape {
    public abstract void area() ;
        
}
public class Rectangle implements Shape {
	private int length;
	private int width;
	public Rectangle(int length,int width ){
		if(length<=0||width<=0) {
			throw new NoValueException("输入了非法值!!");
		}
		this.length=length;
		this.width=width;
	}
	
	public void area() //如果父类或者接口的方法中没有异常抛出,那么子类在覆盖方法时,也不可以抛出异常
	{
		// TODO Auto-generated method stub
		  System.out.println( "长方形的面积为:"+width*length);
	}
}
public class Demo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Rectangle re=new Rectangle(-3,2);
    
            re.area();
        }
        
    }






















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值