casting.java

来自「JAVA培训用实用代码」· Java 代码 · 共 26 行

JAVA
26
字号
class Person{
	String name;
}

class Student extends Person{
	String school = "pku";
	public String getSchool(){
		return school;
	}
}

public class Casting{	    
	public void method(Person e) {
		//System.out.println(e.getSchool());   //非法
		if(e instanceof Student){
			Student me = (Student)e;
		    System.out.println(me.getSchool());
		}	    
	}
	public static  void main(String args[]){
	    Casting t = new Casting();
	    Student m = new Student();
	    t.method(m);
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?