因为操作失误,将返回类型写成了double,变量设定的是int,但是编译通过了,详见代码,什么原理??- class Rectangle implements AreaMethod
- {
- private int len,wid; //设定的变量类型为int
- Rectangle(int len,int wid)
- {
- if (len<=0 || wid<=0)
- {
- throw new WrongException("出现非法值");
- }
- this.len = len;
- this.wid = wid;
- }
- public double getArea() //误将返回类型写成了double,可是编译通过了。。。。。
- {
- return len*wid;
- }
- }
- class Demo
- {
- public static void main(String[] args)
- {
- Rectangle r = new Rectangle(4,5);
- System.out.println(r.getArea());
- }
- }
- [b][/b]
复制代码 |