[Java] category Transition

Transformation into Java classes downcast and upcast

 Polymorphic transformation is reflected directly in Java, and the other is a manifestation of rewriting and overloaded methods

1. upcast

The purpose is to make the transition up approach to parent the parent class subclass can override the call, then call the parent class and subclass can not override the Central African own methods, these methods can only use the object subclass or subclasses access (static method)

example:

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 class T2 {
	
	public static void main(String [] args) {
		Person xiaoming = new Student("xiaoming", 21);
//		Student xiaoming = new Student("xiaoming", 21);
		System.out.println(xiaoming.getCurrentObj().say());
		
	}
}

operation result:

 

analysis:

这里将Person xiaoming  = new Student("xiaoming", 21);

Upward transition is used, then transformation may be obtained using the parent class object subclass overrides say their method

But not to subclass a unique method, for example no longer

summary:

You can use the object name parent name = new subclass name (parameter list);

Complete the transformation up

2. downcast

Different from the upward transition, the transition down into two steps:

1. The first step, conversion of the subclass of a parent class

2. Second, the parent class into subclasses

Example:

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 xiaoming = new Student("xiaoming", 21);
		Student xiaomingstu = (Student)xiaoming;
//		Student xiaoming = new Student("xiaoming", 21);
		System.out.println(xiaomingstu.getCurrentObj().say());
		System.out.println(xiaomingstu.stuUnique());
	}
}

operation result:

Result analysis:

Due to the nature of the transition is done and then down twice Transformational back, so you can call the resulting xiaomingstu

Unique methods Student class, which is different upcast

Up-down transition is a process of transition ring

 

Guess you like

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