JAVA-零碎知識點

JAVA-零碎知識點

你若要喜愛你自己的價值,你就得給世界創造價值。——歌德

函數的重載

  • 什麼時候用重載?
    當定義的功能相同,但參與運算的未知內容不同。
    那麼,這時就定義一個函數名稱以表示起功能,方便閱讀,而通過參數列表的不同來區分多個同名函數。
/*

void show(int a,char b,double c){}

a.
void show(int x,char y,double z){}//沒有,因爲和原函數一樣。

b.
int show(int a,double c,char b){}//重載,因爲參數類型不同。注意:重載和返回值類型沒關係。
c.

void show(int a,double c,char b){}//重載,因爲參數類型不同。注意:重載和返回值類型沒關係。

d.
boolean show(int c,char b){}//重載了,因爲參數個數不同。

e.
void show(double c){}//重載了,因爲參數個數不同。

f.
double show(int x,char y,double z){}//沒有,這個函數不可以和給定函數同時存在與一個類中。


*/

創建數組的三種方式

       int vec[] = new int[]{1, 5, 3};  // 第一種方法          
        int  vec[]  =  { 37 ,  47 ,  23 } ;   // 第二種方法  



       int vec[] = new int [3];
       for(int i=0;i<3;i++)

       vec[i]=i+1;           //第三種方法

關於選擇排序和冒泡排序

選擇排序法

class ArrayTest2 
{

    /*
    選擇排序。
    內循環結束一次,最值出現頭角標位置上。
    */
    public static void selectSort(int[] arr)
    {
        for (int x=0; x<arr.length-1 ; x++)
        {
            for(int y=x+1; y<arr.length; y++)
            {
                if(arr[x]>arr[y])
                {
                    /*
                    int temp = arr[x];
                    arr[x] = arr[y];
                    arr[y]= temp;
                    */
                    swap(arr,x,y);
                }
            }
        }
    }

冒泡排序法

    public static void bubbleSort(int[] arr)
    {
        for(int x=0; x<arr.length-1; x++)
        {                                   
            for(int y=0; y<arr.length-x-1; y++)//-x:讓每一次比較的元素減少,-1:避免角標越界。
            {
                if(arr[y]<arr[y+1])
                {
                    /*
                    int temp = arr[y];
                    arr[y] = arr[y+1];
                    arr[y+1] = temp;
                    */
                    swap(arr,y,y+1);
                }
            }
        }
    }

選擇排序法實際上是保證了每次循環的第一個是最小,而冒泡排序法則保證了每次循環的最後一個是最大。

二維數組

/*
int[] x; int x[];
int[][] y; int y[][]; int[] y[];


int[] x,y[];//x一維,y二維。
int[] x;
int[] y[];

a.
x[0] = y;//error

b.
y[0] = x;//yes

c.
y[0][0] = x;//error

d.
x[0][0] = y;//error

e.
y[0][0] = x[0];//yes

f.
x=y;//error
*/

構造函數當中this 的應用

/*
this語句 :用於構造函數之間進行互相調用。

this語句只能定義在構造函數的第一行。因爲初始化要先執行。
*/
class Person
{
    private String name;
    private int age;

    {

        System.out.println("code run");
    }

    Person()
    {
        //this("hah");
        System.out.println("person run");
    }
    Person(String name)
    {
        //this();
        this.name =name;
    }
    Person(String name,int age)
    {
        //this(name);
        //this.name = name;
        this.age = age;

    }

}

class  PersonDemo4
{
    public static void main(String[] args) 
    {
        new Person();
        //Person p = new Person("lisi",30);
        //Person p1 = new Person("lisi2",36);

    }
}

關於子父類的構造函數

/*
子父類中的構造函數。

在對子類對象進行初始化時,父類的構造函數也會運行,
那是因爲子類的構造函數默認第一行有一條隱式的語句 super();
super():會訪問父類中空參數的構造函數。而且子類中所有的構造函數默認第一行都是super();

爲什麼子類一定要訪問父類中的構造函數。

因爲父類中的數據子類可以直接獲取。所以子類對象在建立時,需要先查看父類是如何對這些數據進行初始化的。
所以子類在對象初始化時,要先訪問一下父類中的構造函數。
如果要訪問父類中指定的構造函數,可以通過手動定義super語句的方式來指定。

注意:super語句一定定義在子類構造函數的第一行。



子類的實例化過程。

結論:
子類的所有的構造函數,默認都會訪問父類中空參數的構造函數。
因爲子類每一個構造函數內的第一行都有一句隱式super();

當父類中沒有空參數的構造函數時,子類必須手動通過super語句形式來指定要訪問父類中的構造函數。

當然:子類的構造函數第一行也可以手動指定this語句來訪問本類中的構造函數。
子類中至少會有一個構造函數會訪問父類中的構造函數。
*/
class Fu //extends Object
{
    int num ;
    Fu()
    {
        //super();
        num= 60;
        System.out.println("fu run");
    }
    Fu(int  x)
    {
        System.out.println("fu ...."+x);
    }

}

class Zi extends Fu
{
    Zi()
    {

        super();  
        //super(4);
        System.out.println("zi run");
    }
    Zi(int x)
    {
        this();
        //super();
        //super(3);
        System.out.println("zi..."+x);
    }
}

class  ExtendsDemo4
{
    public static void main(String[] args) 
    {
        Zi z = new Zi(0);
        System.out.println(z.num);
    }
}

關於多態

關於多態中的成員變量引用的規則

class Fu
{
    static int num = 5;
    void method1()
    {
        System.out.println("fu method_1");
    }
    void method2()
    {
        System.out.println("fu method_2");
    }
    static void method4()
    {
        System.out.println("fu method_4");
    }
}


class Zi extends Fu
{
    static int num = 8;
    void method1()
    {
        System.out.println("zi method_1");
    }
    void method3()
    {
        System.out.println("zi method_3");
    }

    static void method4()
    {
        System.out.println("zi method_4");
    }
}
class  DuoTaiDemo4
{
    public static void main(String[] args) 
    {

//      Fu f = new Zi();
//
//      System.out.println(f.num);
//
//      Zi z = new Zi();
//      System.out.println(z.num);

        //f.method1();
        //f.method2();
        //f.method3();

        Fu f = new Zi();
        System.out.println(f.num);
        f.method4();

        Zi z = new Zi();
        z.method4();



/*
在多態中成員函數的特點:
在編譯時期:參閱引用型變量所屬的類中是否有調用的方法。如果有,編譯通過,如果沒有編譯失敗。
在運行時期:參閱對象所屬的類中是否有調用的方法。
簡單總結就是:成員函數在多態調用時,編譯看左邊,運行看右邊。


在多態中,成員變量的特點:
無論編譯和運行,都參考左邊(引用型變量所屬的類)。


在多態中,靜態成員函數的特點:
無論編譯和運行,都參考做左邊。


*/


//      Zi z = new Zi();
//      z.method1();
//      z.method2();
//      z.method3();
    }
}
5
fu method_4
zi method_4

藉口行引用指向自己的子類對象

interface PCI
{
    public void open();
    public void close();
}

class MainBoard
{
    public void run()
    {
        System.out.println("mainboard run ");
    }
    public void usePCI(PCI p)//PCI p = new NetCard()//接口型引用指向自己的子類對象。
    {
        if(p!=null)
        {
            p.open();
            p.close();

        }
    }
}


class NetCard implements PCI
{
    public void open()
    {
        System.out.println("netcard open");
    }
    public void close()
    {
        System.out.println("netcard close");
        method();
    }

}
class SoundCard implements PCI
{
    public void open()
    {
        System.out.println("SoundCard open");
    }
    public void close()
    {
        System.out.println("SoundCard close");
    }
}
/*
class MainBoard
{
    public void run()
    {
        System.out.println("mainboard run");
    }
    public void useNetCard(NetCard c)
    {
        c.open();
        c.close();
    }
}

class NetCard
{
    public void open()
    {
        System.out.println("netcard open");
    }
    public void close()
    {
        System.out.println("netcard close");
    }
}
*/

class DuoTaiDemo5 
{
    public static void main(String[] args) 
    {
        MainBoard mb = new MainBoard();
        mb.run();
        mb.usePCI(null);
        mb.usePCI(new NetCard());
        mb.usePCI(new SoundCard());

    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章