[Java] instanceof keyword

class Person {
	private String name;
	private int age;
	String sex = "secret";
	protected Person(String name, int age) {
		this(name);
		this.age = age;
		System.out.println("Father constructor 1th");
	}
	
	protected Person(String name) {
		this.name = name;
		System.out.println("Father constructor 2th");
	}
	
	protected String getName() {
		return name;
	}
	protected void setName(String name) {
		this.name = name;
	}
	protected int getAge() {
		return age;
	}
	protected void setAge(int age) {
		this.age = age;
	}
	protected String say () {
		return (this.name+" is saying sth");
	}
	
	public String getOldSex() {
		return sex;
	}
	
	public Person getCurrentObj () {
		return this;
	}
}

class Student extends Person {
	String sex = "male ";
	public Student(String name, int age) {
		super(name, age);
		System.out.println("child constructor 2th");
		// TODO Auto-generated constructor stub
	}
	
	public String getSex() {
		return sex;
	}
	
	public Student(String name) {
		super(name);
		System.out.println("child constructor 1th");
	}

	private String school;

	public String getSchool() {
		return school;
	}

	public void setSchool(String school) {
		this.school = school;
	}
	
	public String say() {
		return "super.say() = "+super.say()+"\n"+this.getName() + " said something"+"\n"+"super.sex = "+super.sex;
	}
	
	public String stuUnique () {
		return "unique method of Student";
	}

}
public class T2 {
	
	public static void main(String [] args) {
		Person p = new Person("Xiaoming", 21);
		Student stu = new Student("stu", 15);
		System.out.println("p instanceof Person "+(p instanceof Person));
		System.out.println("stu instanceof Student "+(stu instanceof Student));
		Person xiaohong = new Student("xiaohong", 19);
		System.out.println("xiaohong instanceof Student "+(xiaohong instanceof Student));
		System.out.println("xiaohong instanceof Person "+(xiaohong instanceof Person));
		Student xiaohongdown = (Student)xiaohong;
		System.out.println("xiaohongdown instanceof Student "+(xiaohongdown instanceof Student));
		System.out.println("xiaohongdown instanceof Student "+(xiaohongdown instanceof Person));		
		System.out.println("p instanceof Student "+(p instanceof Student));
		System.out.println("p instanceof Person "+(p instanceof Person));
		
	}
}

operation result:

Result analysis:

p is an instance of the Person class, belonging to the instance of the base class

stu is an instance of a subclass of the Student

xiaohong a Person object is a Student object to get up the transformation of

The output indicates that the object is both a Student object, and the object is to Person

xiaohongdown is a downward transition xiaohong Student object obtained

At this same time it is still Student and Person object

But if you use the Person object will find p, p only instance of the Person class

Examples not a subclass of Student's

Therefore subclass object can be upward transition, then downcast

But the object of a superclass can not be downcast

Guess you like

Origin blog.csdn.net/chenhanxuan1999/article/details/91878892