Created
May 13, 2018 09:38
-
-
Save PandyYang/b4c5a8ffe640160d2b80aa4c0e2545ad to your computer and use it in GitHub Desktop.
This file contains 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
package jichubiancheng; | |
import java.util.ArrayList; | |
/** | |
* @author Pandy | |
* @date 2018/5/13 16:18 | |
*/ | |
public class ApplesAndOrangesWithGenerics { | |
public static void main(String[] args){ | |
//这块使用了泛型 <>之间是类型参数 指定了这个容器实例可以保存的类型 | |
ArrayList<Apple> apples = new ArrayList<Apple>(); | |
for (int i = 0;i<3;i++) | |
apples.add(new Apple()); | |
//apples.add(new Orange()); 由于增加了泛型 规定了容器保存的类型 所以这个语句在编译时期就会出错 | |
for (int i = 0;i<apples.size();i++) | |
System.out.println(apples.get(i).id()); | |
for (Apple c : apples) | |
System.out.println(c); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment