Next: 4 Objectクラス
Up: ソフトウェア第三 講義資料 Java入門,宣言,インタフェース,パッケージ,スレッド
Previous: 2.5 アクセス修飾子(access modifier)
抽象メソッド(abstract method)をもつクラスを
抽象クラス(abstract class)という.
抽象クラスはインスタンスを生成できない不完全なクラスのようなもので,
いくつかのサブクラスに共通の性質を定義するために使う.
抽象メソッドは,手続きの本体がないメソッドで,サブクラスのメソッドに
おいて,その手続き本体を定義する.
abstract class Benchmark {
abstract void benchmark();
public final long repeat(int count) {
long start = System.currentTimeMillis();
for (int i=0; i < count; i++)
benchmark();
return(System.currentTimeMillis() - start);
}
}
class LoopBenchmark extends Benchmark {
long count=0;
LoopBenchmark(int c) { count = c; }
void benchmark() {
for (int i=0; i < count; i++);
}
}
class FactBenchmark extends Benchmark {
int count=0;
FactBenchmark(int c) { count = c; }
int Fact(int n) {
return((n <= 1)? 1 : Fact(n-1)*n);
}
void benchmark() {
int f = Fact(count);
}
}
class MethodBenchmark extends Benchmark {
void benchmark() {
}
public static void main(String[] args) {
int count = Integer.parseInt(args[0]);
long time = new MethodBenchmark().repeat(count);
System.out.println(count +
" methods in " +
time +
" milliseconds");
time = new LoopBenchmark(1000000).repeat(count);
System.out.println("Loop(1000000) " +
count +
" methods in " +
time +
" milliseconds");
time = new FactBenchmark(10000).repeat(count);
System.out.println("Fact(10000) " +
count +
" methods in " +
time + " milliseconds");
}
}
generated through LaTeX2HTML. M.Inaba 平成18年5月7日