Report6


課題
11/15付講義資料と今回の講義資料中のサンプルプログラムについて考察せよ。
講義資料サンプルプログラム考察についてはstatic変数以降とする。

static変数
プログラム
class Sum{
    static int total;

    Sum() {
        total = 0;
    }

    void add(int x) {
        total += x;
    }

}
public class Repo6test02 {

   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);
    }
}
   
結果
obj1.total = 100
obj2.total = 100
    
考察
static変数は静的変数と言われ、そのクラスのすべてのオブジェクトに共有され る変数。
static変数以外だと、obj1の変数totalとobj2の変数totalは別々の変数として見 られるが、static変数だと同一のものとして扱われる。

コンストラクタ
プログラム
class Sum {
    int total;

    Sum() {
        total = 0;
    }

    Sum(int x){
        total = x;
    }

    void add(int x){
        total += x;
    }
    void sub(int y){
        total -= y;
    }
}

public class Repo6test03 {

    public static void main(String[] args) {
        Sum obj1 = new Sum();
	obj1.add(10);
	obj1.sub(20);
	System.out.println("sum1 = " + obj1.total);
	Sum obj2 = new Sum(100);
	obj2.sub(10);
	obj2.sub(20);
	System.out.println("sum2 = " + obj2.total);
    }
}
   
結果
sum1 = -10
sum2 = 70
    
考察
コンストラクタはオブジェクトの作成と同時に初期化を行うものである。
コンストラクタとクラス名は同じでなくてはならない。
コンストラクタは戻り値を持たない。

staticイニシャライズ
プログラム
class Disp {
    static {
        System.out.println("initialize Disp");
    }
	     
    Disp() {
         System.out.println("construct Disp");
    }
    static void Print() {
       System.out.println("Print Disp");
   }
}
class Body {
      static {
           System.out.println("initialize Body");
     }
     Body() {
         System.out.println("construct Body");
    }
}
public class Repo6test04 {

       public static void main(String[] args) {
       	   Disp obj1 = new Disp();
      	   Disp obj2 = new Disp();
           Body obj3 = new Body();
	   Body obj4 = new Body();
           Disp.Print();
       }
}
   
結果
initialize Disp
construct Disp
construct Disp
initialize Body
construct Body
construct Body
Print Disp
   
考察
staticイニシャライズはあるクラスが呼び出されたときだけ実行される。
結果を見ると、クラスDispとクラスBobyをインスタンス化すると、一回だけ staticイニシャライズが実行されていることが分かる。そのため、クラスDispのPrintメソッドを実行 してもstaticイニシャライズはされていないのが確認できる。

メソッドのオーバーロード
プログラム
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);
    }
    void Disp (int x, int y, int z) {
        System.out.println(x + y + z);
    }
    void Disp (double x) {
        System.out.println(x);
    }
}
public class Repo6test05 {

    public static void main(String[] args) {
        Display obj = new Display();
        obj.Disp();
        obj.Disp(1);
        obj.Disp(1,2);
        obj.Disp(1,2,3);
        obj.Disp(5.2);
    }
}
    
結果
Nothing
1
3
6
5.2
    
考察
メソッドのオーバーロードとは一つのクラスの中に同じ名前のメソッドを複数作る ことである。
ただし、メソッドの引数の数や型はそれぞれ違ったものにする必要がある。

アクセス制御
プログラム
class A {
    static void a() {
        System.out.println("a");
    }
    static public void b() {
        System.out.println("b");
    }
    static private void c() {
        System.out.println("c");
    }
    static protected void d() {
        System.out.println("d");
    }
}
public class Repo6test062 {

    public static void main(String[] args) {
        A obj = new A();
	A.a();
	A.b();
      //A.c();
	A.d();
    }
}
    
結果
a
b
d
    
考察
変数、コンストラクタ、メソッドの修飾子にあるキーワードを使うことで、こ れらのものに対するアクセスを制御することが出来る。
キーワード 説明
public すべてのクラスにアクセスを許可する
protected 同じパッケージ内のコード、または別のパッケージ内のサブクラスに のみアクセスを許可する
private 同じクラス内のコードにのみアクセスを許可する
A.c()はクラスAのメソッドc()にprevateがついているため、実行するとコンパ イルエラーになる。

継承
プログラム
class Disply {
    int x =10;
    void Disp() {
        System.out.println("Hello!");
    }
}

1:public class Repo6test07 extends Disply {

    public static void main(String[] args) {
2:        Repo6test07 obj = new Repo6test07();
        obj.Disp();
        int hensu = obj.x;
        System.out.println(hensu);
    }
}
    
結果
Hello!
10
    
考察
継承とはあるクラスをスーパークラスとし、スーパークラスのメソッドや変数を サブクラスで使用できるようにしたものである。
1:extends DisplyでクラスDisplyを継承している。
2:普通、インスタンス化する時にはインスタンス化するクラスの名前を書くが、 この場合はサブクラスをインスタンス化し、スーパークラスのメソッドを使用す ることが出来る。

super
プログラム
class Disply {
    void Disp() {
        System.out.println("スーパークラス");
    }
}
public class Repo6test09 extends Disply {
    void Disp() {
        super.Disp();
        System.out.println("サブクラス");
    }
    public static void main(String[] args) {
        Repo6test09 obj = new Repo6test09();
        obj.Disp();
    }
}
    
結果
スーパークラス
サブクラス
    
考察
スーパークラスとサブクラスに同じ名前のメソッドや変数があるとサブクラス の方が優先されて、スーパークラスのものは隠れてしまう。これをオーバーラ イドと言う。
オーバーライドしてしまったサブクラスからスーパークラスを参照したい時は superを使うと、スーパークラスのメソッドや変数を参照することが出来る。

this
プログラム
class Keisan {
    int x = 100;
    void add(int x, int y) {
        int w1 = x + y;
        int w2 = this.x + y;
        System.out.println("w1 = " + w1);
        System.out.println("w2 = " + w2);
    }
}
public class Repo6test10 {

    public static void main(String[] args) {
        Keisan obj = new Keisan();
        obj.add(10,20);
    }
}
    
結果
w1 = 30
w2 = 120
    
プログラム
class Keisan {
    void Keisan(int x, int y) {
        System.out.println("Kakezan = " + (x * y));
    }
    void Keisan(int x) {
        this.Keisan(x,1);
    }
}

public class Repo6test11{
    public static void main(String[] args) {
        Keisan obj1 = new Keisan();
        obj1.Keisan(200,5);
        Keisan obj2 = new Keisan();
        obj2.Keisan(200);
    }
}
    
結果
Kakezan = 1000
Kakezan = 200
    
考察
メソッドで引数から与えられた値の他にクラス変数を使用したい場合や、同じ クラス内のメソッドを使用したい場合がある。
この時に使用するのがthisである。
最初のプログラムではメソッドaddの中でクラス変数xを使いたいが、引数とし てのxをすでに受け取っている。この時にthisを使うとクラス変数xを使用する ことが出来る。

abstract
プログラム
abstract class Predifine {
    abstract int add(int x,int y);
    int sub(int x, int y) {
        return x - y;
    }
}
class Repo6test12 extends Predifine {
    public int add(int x, int y) {
        return x+y;
    }

    public static void main(String[] args) {
        Repo6test12 obj = new Repo6test12();
        System.out.println("add = " + obj.add(100,80));
        System.out.println("sub = " + obj.sub(100,80));
    }
}
    
結果
add = 180
sub = 20
    
考察
abstractは抽象メソッドや抽象クラスを作る時に使う。abstractメソッドを持 つクラスは必ずabstractクラスでなくてはならない。
抽象メソッドには入出力だけが決められていて、具体的な処理は書かれていな い。
抽象クラスを継承し、サブクラスでオーバーライドすることにより使用できる ようになる。

interface
プログラム
interface PreDefine {
    int add (int x, int y);
    int sub (int x, int y);
}
class Repo6test13 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) {
        Repo6test13 obj = new Repo6test13();
        System.out.println("add = " + obj.add(100,80));
        System.out.println("sub = " + obj.sub(100,80));
    }
}
    
結果
add = 180
sub = 20
    
考察
interfaceはメソッド宣言を集めたもので、処理が書かれたメソッドは持たな い。
実際の処理が書かれたメソッドがあるクラスにはimplemntsキーワードがつい ていて、interfaceを実装できるようにしている。

感想・反省
今回も早めに終わらせることができず、時間に追われながらやっていた。次こ そは早めに・・・
abstractくらいからよく分からなくなってしまった。もう一度勉強し直します。

独学Java第二版
ジョセフ・オニール著
浅煎り珈琲Javaアプリケーション入門