LX【468x60】


クラス継承階層



コード

//
class W
{
	
	W(){
		System.out.println("W Constructor");
	}
	
	float f;
	
	int XX = 100;
	
	void SayHello(){
		System.out.println("Hello!!. I am W");
	}
	
	void SayKonnichiha(String s){
		System.out.println("こんちちは!! " + s + "さん。私はWです。");
	}
}

//
class X extends W
{
	
	X(){
		System.out.println("X Constructor");
	}
	
	StringBuffer sb;
	
	int XX = 200;
	
	int PutW_XX(){
		return super.XX;	//superキーワード
	}
	
	void SayHello(){
		System.out.println("Hello!!. I am X");
	}
	
	void SayKonnichiha(String s){
		System.out.println("こんちちは!! " + s + "さん。私はXです。");
		super.SayKonnichiha(s);	//superキーワード
	}
}

//
class Y extends X
{
	
	Y(){
		System.out.println("Y Constructor");
	}
	
	String s;
	
	int XX = 300;

	int PutX_XX(){
		return super.XX;	//superキーワード
	}
	
	void SayHello(){
		System.out.println("Hello!!. I am Y");
	}

	void SayKonnichiha(String s){
		System.out.println("こんちちは!! " + s + "さん。私はYです。");
		super.SayKonnichiha(s);	//superキーワード
	}
}

//
class Z extends Y
{
	
	Z(){
		System.out.println("Z Constructor");
	}
	
	Integer i;
	
	int XX = 400;
	
	int PutY_XX(){
		return super.XX;	//superキーワード
	}
	
	void SayHello(){
		System.out.println("Hello!!. I am Z");
	}

	void SayKonnichiha(String s){
		System.out.println("こんちちは!! " + s + "さん。私はZです。");
		super.SayKonnichiha(s);	//superキーワード
	}
}

//
public class Test_Extends {

	public static void main(String[] args) {
		
		System.out.println("Start");

		//
		System.out.println("------------------------");
		
		Z Obj;
		
		Obj = new Z();
		
		//継承と変数
		Obj.f	= 0.1f;
		Obj.sb	= new StringBuffer("abcde");
		Obj.s	= "fghij";
		Obj.i	= new Integer(100);
		
		System.out.println("f -> "	+ Obj.f);
		System.out.println("sb -> "	+ Obj.sb);
		System.out.println("s -> "	+ Obj.s);
		System.out.println("i -> "	+ Obj.i);
		System.out.println("");	

		//superキーワード(変数)
		System.out.println("W XX -> "	+ Obj.PutW_XX());
		System.out.println("X XX -> "	+ Obj.PutX_XX());
		System.out.println("Y XX -> "	+ Obj.PutY_XX());
		System.out.println("Z XX -> "	+ Obj.XX);
		System.out.println("");	
		
		
		//クラス継承階層
		W Obj1 = new W();
		W Obj2 = new X();
		W Obj3 = new Y();
		W Obj4 = new Z();
		
		Obj1.SayHello();
		Obj2.SayHello();
		Obj3.SayHello();
		Obj4.SayHello();
		System.out.println("");	
		
		
		//superキーワード(メソッド)
		Obj1.SayKonnichiha("鈴木さん");
		Obj2.SayKonnichiha("山田さん");
		Obj3.SayKonnichiha("川田さん");
		Obj4.SayKonnichiha("大木さん");
		System.out.println("");	
		
		//
		System.out.println("------------------------");
		
		System.out.println("End");
		
	}
}
			


結果

Start
------------------------
W Constructor
X Constructor
Y Constructor
Z Constructor
f -> 0.1
sb -> abcde
s -> fghij
i -> 100

W XX -> 100
X XX -> 200
Y XX -> 300
Z XX -> 400

W Constructor
W Constructor
X Constructor
W Constructor
X Constructor
Y Constructor
W Constructor
X Constructor
Y Constructor
Z Constructor
Hello!!. I am W
Hello!!. I am X
Hello!!. I am Y
Hello!!. I am Z

こんちちは!! 鈴木さんさん。私はWです。
こんちちは!! 山田さんさん。私はXです。
こんちちは!! 山田さんさん。私はWです。
こんちちは!! 川田さんさん。私はYです。
こんちちは!! 川田さんさん。私はXです。
こんちちは!! 川田さんさん。私はWです。
こんちちは!! 大木さんさん。私はZです。
こんちちは!! 大木さんさん。私はYです。
こんちちは!! 大木さんさん。私はXです。
こんちちは!! 大木さんさん。私はWです。

------------------------
End
			

 


<戻る>