1.5 インスタンスの集まりとしてのプログラム内世界

 クラス,インスタンスはCの構造体と比較すると以下のようになります.
構造体 構造体定義 構造体型の変数
オブジェクト指向 クラス インスタンス

クラスとインスタンスの違い

 すなわち,Cの場合


struct GasStation {
    int unitPrice;
    int sales;
    int remain; 
};

.
.
.
.
struct GasStation *p;
struct GasStation a;

	a.unitPrice=100;
	p=&a;
	printf("%d\n", p->unitPrice);

とか書きますが,Javaでは,


public class GasStation {
.... 省略
}

.
.
.
.
あるクラスの中で,

GasStation gs;

	gs=new GasStation();
	gs.init(10, 100, 1000);

	ある処理をする

	gs.show();

というように書きます.その特徴は,


2003年9月25日 13:08 更新