易错代码合集

易错代码合集

文章目录

一 多态类型

1

  1. 代码
    public class Test{
    	public static void main(String[] args){
    		Base b1=new Base();
    		Base b2=new Sub();
    	}
    }
    
    class Base{
    	Base(){
    		method(100);
    	}
    	
    	public void method(int i){
    		System.out.println("base:"+i);
    	}
    }
    
    class Sub extends Base{
    	Sub(){
    		super.method(i);
    	}
    	
    	public void method(int j){
    		System.out.println("sub:"+j);
    	}
    }
    
  2. 输出答案
    • base:100
    • sub:100
    • base:70
  3. 总结
    • new 子类,在加载其父类时,如果父类所调用的自身的方法被其子类所重写了,那么此时调用的是子类所重写的方法

2

  1. 代码
    public class Test{
    	public static void main(String[] args){
    		A c=new B();
    		c.setA(5);
    		System.out.println(c.getA());
    	}
    }
    
    class A{
    	private int a;
    	
    	public void setA(int a){
    		this.a=a;
    	}
    	
    	public void getA(){
    		return a;
    	}
    }
    
    class B extends A{
    	private int a;
    	
    	public void setA(int a){
    		this.a=a;
    	}
    }
    
  2. 输出答案
    • 0
  3. 总结
    • getter 和 setter 也算在了重写的范围内

3

  1. 代码

    public class Test{
    	public static void main(String[] args){
    		Fu f=new Zi();
    		Fu z=new Zi();
    		System.out.println(f.num);
    		System.out.println(z.num);
    		f.show();
    		z.show();
    	}
    }
    
    class Fu{
    	int num=4;
    	
    	void show(){
    		System.out.println("show Fu");
    	}
    }
    
    class Zi extends Fu{
    	int num=5;
    	
    	void show(){
    		System.out.println("show Zi");
    	}
    }
    
  2. 输出答案

    • 4
    • 5
    • show Zi
    • show Zi
  3. 总结

    • 以下结论成立在子类对象指向父类引用的情况下(保险起见,防止意外情况)
    • 只能访问父类的属性
    • 只能访问父类没被重写的方法
    • 只能访问子类已经重写父类的方法
  4. 资料

    资料
    Java子类父类属性的覆盖(转载)
### 示例一:catch 体里遇到 return 的情况 ```java public class TryCatchReturnInCatch { public static int test() { try { int result = 1 / 0; return result; } catch (ArithmeticException e) { System.out.println("进入 catch 块"); return 2; } } public static void main(String[] args) { System.out.println(test()); } } ``` 在这个例子中,当 `try` 块中发生 `ArithmeticException`(除零异常)时,程序进入 `catch` 块。如果对 `catch` 块中 `return` 的执行机制理解不清晰,可能会对最终返回值产生误解。实际上,当 `catch` 块遇到 `return` 时,该 `return` 语句会被执行并返回相应的值 [^2]。 ### 示例二:try 中有返回值,finally 也有返回值 ```java public class TryReturnFinallyReturn { public static int test() { try { return 1; } finally { return 2; } } public static void main(String[] args) { System.out.println(test()); } } ``` 这里 `try` 中有返回值,但 `finally` 也有返回值。按照规则,`try` 中有返回值只是暂存,若 `finally` 存在 `return`,则以 `finally` 中的返回值为主。这是很多人容易出错的点,可能会误以为会返回 `try` 中的值 [^3]。 ### 示例三:finally 体里有 `System.exit()` 方法 ```java public class FinallyWithSystemExit { public static void test() { try { int result = 1 / 0; } catch (ArithmeticException e) { System.out.println("进入 catch 块"); } finally { System.out.println("进入 finally 块"); System.exit(0); } } public static void main(String[] args) { test(); System.out.println("main 方法后续代码"); } } ``` 当 `finally` 体里有 `System.exit()` 方法时,程序会直接退出,不会再执行 `main` 方法中 `test()` 调用之后的代码。如果对 `System.exit()` 在 `finally` 块中的作用不了解,可能会预期后续代码会执行 [^2]。 ### 示例四:当 catch 和 finally 体里同时遇上 `return` ```java public class CatchAndFinallyReturn { public static int test() { try { int result = 1 / 0; return result; } catch (ArithmeticException e) { return 1; } finally { return 2; } } public static void main(String[] args) { System.out.println(test()); } } ``` 在这个例子中,`catch` 和 `finally` 体里都有 `return`。最终会返回 `finally` 中的值,这也是容易出错的地方,很多人可能会认为会返回 `catch` 中的值 [^2]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值