Created
December 9, 2011 12:30
-
-
Save ledsun/1451360 to your computer and use it in GitHub Desktop.
DownCastSample.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.List; | |
| public class DownCastSample { | |
| public List<Integer> 警告が出る() { | |
| Object obj = null; | |
| List<Integer> ret = (List<Integer>) obj; | |
| return ret; | |
| } | |
| public List<Integer> tryでくくっても警告が出る() { | |
| try { | |
| Object obj = null; | |
| List<Integer> ret = (List<Integer>) obj; | |
| return ret; | |
| } catch (ClassCastException cce) { | |
| throw cce; | |
| } | |
| } | |
| public Integer[] ジェネリックでなければcastメソッドが使える() { | |
| try { | |
| Object obj = null; | |
| Integer[] ret = Integer[].class.cast(obj); | |
| return ret; | |
| } catch (ClassCastException cce) { | |
| throw cce; | |
| } | |
| } | |
| public List<Integer> アノテーションで警告を抑制() { | |
| Object obj = null; | |
| @SuppressWarnings("unchecked") | |
| List<Integer> ret = (List<Integer>) obj; | |
| return ret; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment