● 可以把多個class寫在同一個檔案中,但檔名只能有一個,compiler時會自動產生不同class的*.class檔
ex:student.java中有student class/a class/b class,compiler後會自動產生student.class/a.class/b.class三個檔案
 
1.instanceof 
=>查詢is-a(查詢物件是何類型)=>true/false
=>ex:查a是不是A物件=>a instanceof A(true or false)
studtent.java
class student{ }
class A extends student{ }
class A_1 extends A{ }
class B extends student{ }
 
add.java
A a = new A();
System.out.println(a instanceof A);
System.out.println(a instanceof student);
System.out.println(a instanceof A_1);
=>instanceof只會往上找不會往下找,所以找A_1會變成flase
 
System.out.println(a instanceof B);
=>instanceof尋找時,限定組織圖中的同一條線上,如果是另外一條線compiler會出現問題(B和A無關)
 
 
2.java.lang.object;
a) 所有物件的共同父類別
*因為class是為了new物件用的,所以object是所有class的共同父類別也會是所有物件的共同父類別
 
b) 新增class時會自動繼承
*只要是寫class雖然都沒有寫extends但都會自動繼承object
*如果日後在使用object的method時,也可以用override修改
 
c) equals 方法
*不論是物件還是變數或陣列,只要"="的左右兩邊都是位址,就會把左邊砍掉
add.java
A a=new A();
A b=new A(); 
a.x=50;
a.show();   
b.show();  
b=a;  
b.x=200;
b.show();
a.show(); System.out.println(a.equals(b));
=> 假設不小心把位址砍掉了,show()的時候才發現兩個結果一樣,檢查的方法除了直接查兩個物件的記憶體外,還可以用equals方法,看a和b兩個物件的記憶體是否相同,相同就是true,不同就是false
 
3.final
a)field=>常數,須給初始值,如果沒有給初始值,compiler會出錯,如下,一定要給初始值或是用建構式給予初始值
studtent.java
class student{
final double pi=3.14;
}
 
class A extends student{
int x;
void show(){
System.out.println("x="+(x+pi));
}
 
add.java
A a = new A();
A b = new A();
a.x=50;
a.show();
b.show();
b=a;
b.x=200;
a.show();
b.show();
 
**final常數給了以後不能再修改
studtent.java
int x;
void show(){
pi=5;
System.out.println("x="+(x+pi));
}
=>final變數不能指定
 
b)method=>不允許override
如果有個程式(名稱)不想被override,就在前面類型加上final,就無法被override,要改就要另外取名字寫
studtent.java
class A extends student{
int x;
final void show(){
System.out.println("x="+(x+pi));
}
}
class A_1 extends A{
     void show(){  }
}
 
c)class=>不允許繼承
如果有寫一個class不想被繼承,又不想加private(因為加private在文件中會被隱藏),就在class前面加上final
studtent.java
final class A extends student{
int x;
final void show(){
System.out.println("x="+(x+pi));
}
}
 
class A_1 extends A{
/*void show(){
}*/
}
=>A因為有加final所以不允許繼承
 
d)空final變數(沒給初始值的final變數),需透過建構式給予初始值(要在new的時候給初始值,所以是建構式給)
通常會用在輸入帳號時,因為帳號一開始空的給使用者輸入,輸入以後就要變成常數不能再修改
member.java
class member{
final String name;
member(String name){
this.name=name;
}
}
 
add.java
member m=new member("iamaccount");
System.out.println(m.name);
=>輸出iamaccount
 
=>如果之後用m.name="change";去修改,會出現錯誤訊息,name是final variable不能修改
 
4.參數列表
a) 即方法的引數 
b) 與陣列類似 
ex:
abc(int [ ] x):void; 或 abc(int... x):void;
studtent.java
class B extends student{
  int sum1(int x, int y, int z){  
       return x+y+z;
  }
  int sum2(int[]x){
      //用陣列的時候沒有數量的限制
       return x[0]+x[1]+x[2]; 
  }
 int sum3(int...x){   //一定只能有三個點
       return x[0]+x[1]+x[2];
  }
}
add.java
int[] x={10,20,30};  
System.out.println(new B().sum1(10,20,30));
System.out.println(new B().sum2(x));  
=>要先宣告陣列 x,且這邊sum()內放的是陣列x
System.out.println(new B().sum3(x));
//注意引數的類型要一致
System.out.println(new B().sum3(20,30,40)); 
 
=>參數列表比陣列有彈性在於,這邊輸出時可以當作陣列用也可以直接輸入3個引數(自動幫你轉成陣列),但如果在 System.out.println(new B().sum2(10,20,30));放入引數,就會錯誤,而且在 sum3(20,30,40) 這裡放入的引數也沒有數量的限制
 
**如果student.java中,把method的名稱改為一致就不會是overloading,因為兩個方法的引數都是陣列
int sum(int[]x){
     return x[0]+x[1]+x[2];  
  }
int sum(int...x){   
     return x[0]+x[1]+x[2];
  }
**參數列表只能設定一個,不能一次設定多個
void abc(int[] x,double[] y){  }
void abc2(int... x, double... y){  }
**當引數還有混和其他變數時,參數列表一定要放在最後
void abc(int[] x,double[] y){}
void abc2(int[] x,int y, int z){}
void abc3(int... x,int y, int z){}
 
=>正確的用法
void abc3(int y, int z,int... x){}
 
● 靜態class也可以放入main程式,可以同時是靜態也是動態,而且因為有main所以可以執行
=>這樣做可以用來自己測試自己寫好的class,檔案命名時以main為主
class ABC
{
      public static void main(String args[]){
            new ABC(100).show();
      }
 int x;
 ABC(int x)
 {
       this.x=x;
 } 
 void show()
 {
  System.out.println("hello "+x);
 } 
}
 
 
 
 
 
 
 
 
 
文章標籤
全站熱搜
創作者介紹
創作者 muchone 的頭像
muchone

簡單。生活。享受

muchone 發表在 痞客邦 留言(0) 人氣(72)