インスタンス変数とインスタンスメソッド



コード

class Pt3DCls{

	//インスタンス変数
	double 	x, y, z;
	String		Name;
	boolean	Flag;
	
	//コンストラクタ
	Pt3DCls(){
		/*
		 * 各インスタンス変数が、規定値に初期化される。
		 * x, y, zが0、Nameが"null"、Flagがfalseになる。
		 */
	}
	
	Pt3DCls(double x){
		this(x, x * 10, x * 20);
	}
	
	Pt3DCls(double x, double y){
		this(x, y, x * 2);
	}
	
	Pt3DCls(double x, double y, double z)
	{
		Name = new String();
		Name = "temp";
		
		Flag = true;
		
		this.x = x;
		this.y = y;
		this.z = z;
	}
	
	
	//インスタンスメソッド
	void Move(double dDt){
		
		this.x += dDt;
		this.y += dDt;
		this.z += dDt;
		
		Name += "_Move";
	}
}

public class Test_INS {

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

		//
		System.out.println("------------------------");
		
		
		//
		System.out.println("//////   p1   /////");
		Pt3DCls p1 = new Pt3DCls();
		
		System.out.println("Name = " + p1.Name + "//");
		System.out.println("Flag = " + p1.Flag);
		System.out.println("x = " + p1.x);
		System.out.println("y = " + p1.y);
		System.out.println("z = " + p1.z);	
		System.out.println("");	

		//
		System.out.println("//////   p1 Move   /////");
		p1.Move(15.0);
		
		System.out.println("Name = " + p1.Name + "//");
		System.out.println("Flag = " + p1.Flag);
		System.out.println("x = " + p1.x);
		System.out.println("y = " + p1.y);
		System.out.println("z = " + p1.z);	
		System.out.println("");
		

		
		//
		System.out.println("//////   p2   /////");
		Pt3DCls p2 = new Pt3DCls(1.0);
		
		System.out.println("Name = " + p2.Name + "//");
		System.out.println("Flag = " + p2.Flag);
		System.out.println("x = " + p2.x);
		System.out.println("y = " + p2.y);
		System.out.println("z = " + p2.z);	
		System.out.println("");

		//
		System.out.println("//////   p2 Move   /////");
		p2.Move(15.0);
		
		System.out.println("Name = " + p2.Name + "//");
		System.out.println("Flag = " + p2.Flag);
		System.out.println("x = " + p2.x);
		System.out.println("y = " + p2.y);
		System.out.println("z = " + p2.z);	
		System.out.println("");
		
		
		
		//
		System.out.println("//////   p3   /////");
		Pt3DCls p3 = new Pt3DCls();
		p3.Name = new String();	//Name変数を初期化
		
		System.out.println("Name = " + p3.Name + "//");
		System.out.println("Flag = " + p3.Flag);
		System.out.println("x = " + p3.x);
		System.out.println("y = " + p3.y);
		System.out.println("z = " + p3.z);	
		System.out.println("");

		//
		System.out.println("//////   p3 Move   /////");
		p3.Move(15.0);
		
		System.out.println("Name = " + p3.Name + "//");
		System.out.println("Flag = " + p3.Flag);
		System.out.println("x = " + p3.x);
		System.out.println("y = " + p3.y);
		System.out.println("z = " + p3.z);	
		System.out.println("");
		
		
		//
		System.out.println("------------------------");
		
		System.out.println("End");
		
	}
}
			


結果

Start
------------------------
//////   p1   /////
Name = null//
Flag = false
x = 0.0
y = 0.0
z = 0.0

//////   p1 Move   /////
Name = null_Move//
Flag = false
x = 15.0
y = 15.0
z = 15.0

//////   p2   /////
Name = temp//
Flag = true
x = 1.0
y = 10.0
z = 20.0

//////   p2 Move   /////
Name = temp_Move//
Flag = true
x = 16.0
y = 25.0
z = 35.0

//////   p3   /////
Name = //
Flag = false
x = 0.0
y = 0.0
z = 0.0

//////   p3 Move   /////
Name = _Move//
Flag = false
x = 15.0
y = 15.0
z = 15.0

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

ビジネスPC&PCサーバ


<戻る>

PC用眼鏡【管理人も使ってますがマジで疲れません】 解約手数料0円【あしたでんき】 Yahoo 楽天 NTT-X Store

無料ホームページ 無料のクレジットカード 海外格安航空券 ふるさと納税 海外旅行保険が無料! 海外ホテル