|
class A {
int a, b, c;
A(
) { ←クラスと同名
a = 0 ;
b = 0 ;
c = 0 ;
} ←コンストラクタ
}
class TestA {
:
:
A a =new A(
); ←オブジェクトの生成=コンストラクタの呼び出し
}
|
|
class B {
int s, t ;
B(int a, int b ) {
s = a ;
t = b ;
} ←コンストラクタ
}
class TestB {
:
:
B b =new B(3, 4 );
}
|
■サンプルプログラム
class Book {
int price;
int num = 0;
String title;
Book(String t, int p){
title = t;
price = p;
}
void print() {
System.out.println("タイトル:" + title);
System.out.println("定 価:" + price);
System.out.println("注文部数:" + num);
System.out.println("合計金額:" + price * num);
}
}
class Books {
public static void main(String[] args){
Book book = new Book("Cの絵本",1380);
book.num = 10;
book.print();
}
}