課題詳細
Report#6INDEXへ戻ります?
static変数を用いたプログラム
class Sum { int total; Sum() { total = 0; } void add(int x) { total += x; } } class SumSample { public static void main(String args[]) { Sum obj1 = new Sum(); Sum obj2 = new Sum(); obj1.add(10); obj1.add(20); obj2.add(30); obj2.add(40); System.out.println("obj1.total = " + obj1.total); System.out.println("obj2.total = " + obj2.total); } } |
[super:report06/kougi/1] J04011% java SumSample obj1.total = 30 obj2.total = 70 |
Sum obj1 = new Sum(); Sum obj2 = new Sum(); |
INDEXへ戻ります?
static変数を共有させるプログラム
class Sum { static int total; Sum() { total = 0; } void add(int x) { total += x; } } class SumSample { public static void main(String args[]) { Sum obj1 = new Sum(); Sum obj2 = new Sum(); obj1.add(10); obj1.add(20); obj2.add(30); System.out.println("obj1.total = " + obj1.total); System.out.println("obj2.total = " + obj2.total); obj2.add(40); System.out.println("obj1.total = " + obj1.total); System.out.println("obj2.total = " + obj2.total); } } |
[nw0411:report06/kougi/2] J04011% java SumSample obj1.total = 60 obj2.total = 60 obj1.total = 100 obj2.total = 100 |
static int total; |
INDEXへ戻ります?
コンストラクタを用いた合計プログラム
class Sum { int total; Sum() { total = 0; } Sum(int x) { total = x; } void add(int x) { total += x; } } class ConstrcSample { public static void main(String args[]) { Sum obj1 = new Sum(); obj1.add(10); obj1.add(20); System.out.println("sum1 = " + obj1.total); Sum obj2 = new Sum(100); obj2.add(10); obj2.add(20); System.out.println("sum2 = " + obj2.total); } } |
[nw0411:report06/kougi/3] J04011% java ConstrcSample sum1 = 30 sum2 = 130 |
INDEXへ戻ります?
staticイニシャライズを用いたプログラム
class Disp { static { System.out.println("initialize"); } Disp() { System.out.println("construct"); } } class StaticSample { public static void main(String args[]) { Disp obj1 = new Disp(); Disp obj2 = new Disp(); } } |
[nw0411:report06/kougi/4] J04011% java StaticSample initialize construct construct |
Disp obj1 = new Disp(); |
static { System.out.println("initialize"); } |
INDEXへ戻ります?
メソッドのオーバーロードを用いたプログラム
class Display { void Disp() { System.out.println("Nothing"); } void Disp(int x) { System.out.println(x); } void Disp(int x, int y) { System.out.println(x + y); } } class OvldSample { public static void main(String args[]) { Display obj = new Display(); obj.Disp(); //引数無しでクラスDisplay内のメソッドDisp()を実行。 obj.Disp(1); //1を引数としてクラスDisplay内のメソッドDisp(int x)を実行。 obj.Disp(1,2); //1、2を引数としてクラスDispla内のメソッドDisp(int x, int y)を実行。 } } |
[nw0411:report06/kougi/5] J04011% java OvldSample Nothing 1 3 |
INDEXへ戻ります?
アクセス制御を用いたプログラム
class keisan { static int tanka; private float rate; keisan() { tanka = 1000; rate = 0.05f; } int keisan(int kazu) { int kingaku; kingaku = (int)(tanka * kazu * (1.0 + rate)); return kingaku; } } class PrvSample { public static void main(String args[]) { keisan obj1 = new keisan(); int kazu = 3; int kingaku = obj1.keisan(kazu); System.out.println("tanka = " + obj1.tanka); System.out.println("kazu = " + kazu); System.out.println("kingaku = " + kingaku); //System.out.println("rate = " + obj1.rate); /*rateはprivate変数のため参照不可。*/ } } |
[nw0411:report06/kougi/6] J04011% java PrvSample tanka = 1000 kazu = 3 kingaku = 3150 |
public | 全てのクラス、全てのメソッドからアクセス可能。 |
private | 定義した同じクラスでのみアクセス可能。 |
protected | 定義した同じクラスでのアクセス、そのクラスから派生したサブクラス、同じパッケージ内のクラスからアクセス可能。 |
//System.out.println("rate = " + obj1.rate); |
[nw0411:report06/kougi/6] J04011% javac PrvSample.java PrvSample.java:25: rate は keisan で private アクセスされます。 System.out.println("rate = " + obj1.rate); ^ エラー 1 個 |
INDEXへ戻ります?
クラスの継承
class Display { void Disp() { System.out.println("Hello! (^o^)/ "); } } class Succeed extends Display { public static void main(String args[]) { Succeed obj = new Succeed(); obj.Disp(); } } | class Display { //スーパークラス void Disp() { System.out.println("Hello! (^o^)/"); } } class Override extends Display { //サブクラス void Disp() { //メソッドのオーバーライド System.out.println("Hello! Java!! (^o^)/"); } public static void main(String args[]) { Override obj = new Override(); obj.Disp(); } } |
[nw0411:report06/kougi/7] J04011% java Succeed Hello! (^o^)/ | [nw0411:report06/kougi/8] J04011% java Override Hello! Java!! (^o^)/ |
INDEXへ戻ります?
メソッドのオーバーライドを用いたプログラム
class Display { //スーパークラスDisplay void Disp() { System.out.println("Hello!"); } } class SuperSample extends Display { //サブクラスSuperSample void Disp() { //メソッドのオーバーライド super.Disp(); //スーパークラスのDispを実行。 System.out.println("Hello! Java!! (^o^)/"); } public static void main(String args[]) { SuperSample obj = new SuperSample(); obj.Disp(); //サブクラスのDispを実行。 } } |
[nw0411:report06/kougi/9] J04011% java SuperSample Hello! Hello! Java!! (^o^)/ |
INDEXへ戻ります?
superを用いたクラスの継承
class Keisan { //スーパークラスKeisan int x = 100; void add(int x, int y) { int w1 = x + y; int w2 = this.x + y; System.out.println("w1 = " + w1 + " w2 = " + w2); } } class ThisSample extends Keisan { //サブクラスThisSample public static void main(String args[]) { ThisSample obj = new ThisSample(); obj.add(10, 20); } } | class Keisann { void Keisann(int x, int y) { System.out.println("Keisann = " + (x * y)); } void Keisann(int x) { this(x,1); } } class ThisSample1 extends Keisann { public static void main(String args[]) { Keisann obj1 = new Keisann(200,5); Keisann obj2 = new Keisann(200); } } |
[nw0411:report06/kougi/10] J04011% java ThisSample w1 = 30 w2 = 120 | [nw0411:report06/kougi/10] J04011% javac ThisSample1.java ThisSample1.java:6: this の呼び出しはコンストラクタの先頭文でなければなりません。 this(x,1); ^ ThisSample1.java:12: シンボルを解決できません。 シンボル: コンストラクタ Keisann (int,int) 場所 : Keisann の クラス Keisann obj1 = new Keisann(200,5); ^ ThisSample1.java:13: シンボルを解決できません。 シンボル: コンストラクタ Keisann (int) 場所 : Keisann の クラス Keisann obj2 = new Keisann(200); ^ エラー 3 個 |
INDEXへ戻ります?
abstractを用いた抽象クラスと継承
abstract class PreDifine { public abstract int add(int x, int y); public int sub(int x, int y) { return x - y; } } class AbstractSample extends PreDifine { public int add(int x, int y) { return x + y; } public static void main(String args[]) { AbstractSample obj = new AbstractSample(); System.out.println("add = " + obj.add(100, 80)); System.out.println("sub = " + obj.sub(100, 80)); } } |
[nw0411:report06/kougi/11] J04011% java AbstractSample add = 180 sub = 20 |
INDEXへ戻ります?
interfaceとimplementsを用いたクラスの継承
こちらが正しいソース。interface PreDefine { int add(int x, int y); int sub(int x, int y); } class InterfaceSample implements PreDefine { public int add(int x, int y) { return x + y; } public int sub(int x, int y) { return x - y; } public static void main(String args[]) { InterfaceSample obj = new InterfaceSample(); System.out.println("add = " + obj.add(100,80)); System.out.println("sub = " + obj.sub(100,80)); } } |
[nw0411:report06/kougi/12] J04011% java InterfaceSample add = 180 sub = 20 |
INDEXへ戻ります?
感想
今回は課題内容が多いということが大きな問題となり、かなりの時間を費やしてしまいました。INDEXへ戻ります?
参考文献など
独習Java 第2版INDEXへ戻ります?