[Javaの]カテゴリの変遷

意気消沈し、アップキャストJavaクラスへの変換

 多形変換は、Javaで直接反射され、他方は書き換えとオーバーロードされたメソッドの現れであります

1.アップキャスト

目的は、これらのメソッドは、オブジェクトのみサブクラスまたはサブクラスへのアクセスを使用することができ、親クラスとサブクラスは中央アフリカ独自のメソッドをオーバーライドすることはできません呼び出す、親クラスのサブクラスが呼び出しを無効にすることができ、親への移行までのアプローチを作ることです(静的メソッド)

例:

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());
		
	}
}

結果:

 

分析:

这里将人の暁明=新しい学生( "暁明"、21);

上向きの遷移が使用され、次いで、変換は、親クラス・オブジェクト・サブクラスのオーバーライドは、それらの方法を言う使用して得ることができます

しかし、もはや例えば、ユニークな方法をサブクラス化しないことではありません

要約:

あなたは、オブジェクト名の親の名前=新しいサブクラス名(パラメータリスト)を使用することができます。

変換を完了します

2.ダウンキャスト

上向きの移行とは異なり、2つのステップにダウンの移行:

1.最初のステップは、親クラスのサブクラスの変換

2.第二に、サブクラスへの親クラス

例:

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());
	}
}

結果:

結果の分析:

移行の性質上行われ、その後、ダウン二回変革バック、あなたは結果xiaomingstuを呼び出すことができています

別のアップキャストでユニークな方法で学生クラス、

アップダウントランジション遷移リングのプロセスがあります

 

おすすめ

転載: blog.csdn.net/chenhanxuan1999/article/details/91525062