0
0

More than 3 years have passed since last update.

java Shilberの復習

Posted at

import java.util.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;


import java.time.*;


import java.time.format.*;

class A{

    int A=100;
    public void test(){
        System.out.println("testA");
    }

    public void test2(){
        System.out.println("test2");
    }
}

class B extends A{ 
    int A=1000;
    public void test(){
        System.out.println("testB");
    }

    public void test3(){
        System.out.println("test3");
    }

}

class c extends A{ 
    int A=1000;
    public void test(){
        System.out.println("testc");
    }

    public void test4(){
        System.out.println("testc");
    }

}


//スーパークラスにコンストラクタを準備した場合、派生クラスでは必ずスーパーで呼ぶ、ただし引数無しの場合には、明示的に呼ばれるので
//必要ない、またthisなどで他のコンストラクタに移動した場合にスーパークラスのコンストラクターを呼んでもOK
class spclass { 
    int A=1000;

    spclass(String s){
        System.out.println("スーパークラスのコンストラクタ");
    }

}

class konsutoTest extends spclass{ 
    int A=1000;

    konsutoTest(){
        this("s");
        System.out.println("派生クラスのコンストラクタ");
    }

     konsutoTest(String s){
        super("s");
        System.out.println("派生クラスのコンストラクタ");
    }
}

interface intaTest{
    public void process(); //修飾子になにもつけないとパブリック
}

class Sample implements intaTest{
    public void process(){}; //piblicか強くしなければ駄目
}




public class Main {

    protected static int st;
    protected static boolean bl;
    protected static int[] bll;
    protected static String stt;
    protected static ArrayList ALL;
    protected static ArrayList ALL2;
    protected static StringBuilder stb=new StringBuilder(5);


    public static void main(String[] args) throws Exception {

        //デフォルトコンストラクタの復習
        new konsutoTest();

        //ポリフォーリズム
        //BのインスタンスをAにいれるけど、結局BのインスタンスはAの差分にすぎない
        //Aの箱に入れたらAの物しか見れないけど、オーバーライドされたメソッドはAで反映される
        //フィールドは箱に合わせた値が出力されるので、ぽりふぇーリズムの問題が出たらオーバーライドされているかを気を付ける。
        A a=new B();

        a.test();
        a.test2();
        System.out.println(a.A);

        B b=(B)a;
        b.test3();

        System.out.println(b.A);
        // Your code here!

        //日付の問題
        //LocalDateTime date = LocalDateTime.of(2015,9,15,1,1);
        LocalDate date = LocalDate.of(2015,9,15);
        System.out.println(date);
        System.out.println(date.format(DateTimeFormatter.ISO_DATE));

        double ddd=10.1d;
        float fff=10f;
        int iii=100;
        String sss;
        long lll=10;

        ddd=fff;//小さいから大きいはOK
        fff=(float)ddd;//大きいから小さいへはキャスト

        ddd=iii;
        iii=(int)ddd;//キャストすると桁落ちする

        double v=10.0+10;//intはダブルに入るから

        long l=10+10;//int型は暗黙的にLONGに変えてくれるから

        lll=lll+iii;//longもint入るから大丈夫



        System.out.println("StringBuilder<StringBuilder> =初期値" + stb.toString() +"StringBuilder<StringBuilder> =数" +stb.length());//参照型なのでNULL


        try{
        RuntimeExceptionTest();
        }
        catch(RuntimeException e){
            System.out.println(e);
        }


        try{

        bll=new int[3];

        ArrayList<Integer> ll = new ArrayList<Integer>();
        ArrayList<String> list = new ArrayList<>();//型指定ありだから駄目
        ArrayList list2 = new ArrayList();//ジェネリックで型指定なしだからなんでもOK

        System.out.println("ArrayList<Integer> =" + ALL2);//参照型なのでNULL
        System.out.println(list);//参照型なのでNULLtただしインスタンスを生成すると【】が入る。

        ll.add(10);
        list.add("10");
        list2.add('B');

        System.out.println(ALL2);//参照型なのでNULL
        System.out.println("ArrayList<Integer> =" + ll);//参照型なのでNULL
        System.out.println("ArrayList<String>=" + list);//参照型なのでNULLtただしインスタンスを生成すると【】が入る。

        System.out.println("ストリング=" + stt);//参照型なのでNULL
        System.out.println("アレイ=" + ALL);//参照型なのでNULLtただしインスタンスを生成すると【】が入る。

        System.out.println(bl);

        System.out.println(bll[0]);
        System.out.println(bll.length + "←配列の数 中身" + bll[0]);//配列のインスタンスだけでも数は出せる 中身は0で初期化される
        System.out.println(args);//argsは何も入れないとint[0]を作る数は0個でハッシュコードを作る


        //例外の練習 

        Main.test();
        System.out.println("XXXXXXXX");

        }
        catch(RuntimeException e){
            System.out.println(e);

        }
        catch(Exception e){
            System.out.println("到達しない");

        }


    }

    public static void RuntimeExceptionTest()
    {
        //非検査例外 スローしなくてもいい、呼び出し元でキャッチする
        //必ず記載する必要はない。

        if(2==1)
        {
            throw new RuntimeException();
        }
        else{
            throw new NullPointerException();
        }

    }

    public static void test() throws Exception
    {
        //throws エラーを呼び出し元に投げて、呼び出し元で処理させる
        st=100;
        if(1==1)
        {
            throw new RuntimeException();
        }
        else
        {
            throw new Exception();
        }

    }

}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0