ITeye博客

父子类的加载顺序和父类参数

dmwdmc · 2013-06-01 09:53 · core JAVA jvm · 编程语言 · Markdown

public class Father {
		static {
			System.out.println("static Father");
		}
		Father(){
			System.out.println("construction Father");
		}
	
}
public class Son extends Father{
		static {
			System.out.println("static Son");
		}
		Son(){
			System.out.println("construction Son");
		}
}
public class Test {
	public static void main(String[] args) {
		Father fa=new Son();
		System.out.println(Test.getName(fa));
	}

	 static String getName(Father fa){
		 if(fa instanceof Son){
			 return "Variable is Son Type.";
		 }
		return "Variable is Father Type.";
	}
}

最终会打印出: static Father static Son construction Father construction Son Variable is Son Type.

最后一句说明传的参数就是完全子类对象了。

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。