标签:java 基础 初始化
在测试初始化先后顺序时,加入了泛型,得到的结果颠覆认知。
我明明先执行的Qiu a = new Qiu();怎么会先执行static HuiYi ?!
-----------输出结果--------------
static block Qiu
static HuiYi
block Qiu
Qiu
block Qiu
Qiu
block HuiYi
HuiYi
package fanxing01; import java.util.ArrayList; import java.util.Collection; import java.util.List; //回忆泛型知识点 class Qiu{ static{ System.out.println("static block Qiu"); } { System.out.println("block Qiu"); } Qiu(){ System.out.println("Qiu"); } protected int set(Integer a){ return 1; } protected void set(Double a){ // return 1; } } //class Qiu2 extends Qiu{ // private void set(Integer a){ // // } //} public class HuiYi<T> extends Qiu{ static{ System.out.println("static HuiYi"); } HuiYi(){ System.out.println("HuiYi"); } { System.out.println("block HuiYi"); } public void set(T a){ System.out.println(a); } public static<T> void setFromA2B(Collection<T> arr1,Collection<? super T> arr2){ for(T o : arr1){ arr2.add(o); } System.out.println(arr2); } public static void main(String[] args) { Qiu a = new Qiu(); HuiYi<Integer> b = new HuiYi<>(); } }
标签:java 基础 初始化
原文地址:http://3290753.blog.51cto.com/3280753/1970676