Java基礎

1.new一個子類對象時,會先new出父類的對象

2.Arrays.sort(Object[] a);

先排序數字,再排序字母  如1,1c,3b

3.The finalize() method for a given object is called no more than once by the garbage collector.

4.Cannot use super in a static context

  1. public static int multiply(int a, int b) {  
  2.     int c = super.multiply(a, b);  
  3.     return c;  

 5.屬性的話,根據聲明對象的類,方法則看new的是哪個類,考題舉例:

  1.  class Foo {  
  2.  public int a = 3;  
  3.  public void addFive() { a += 5; System.out.print("f "); }  
  4.  }  
  5.  class Bar extends Foo {  
  6.  public int a = 8;  
  7.  public void addFive() { this.a += 5; System.out.print("b " ); }  
  8.  }  
  9. Invoked with: Foo f = new Bar(); f.addFive(); System.out.println(f.a); 

輸出b   3

6.異或
真^假=真
假^真=真
假^假=假
真^真=假

7.變量:only public, static & final are permitted in InterFace

  方法:only public & abstract are permitted in InterFace

構造器是不能用synchronized來修飾的(一句話:不能用abstract,final,static,synchronized及native來修飾只能是public ,protected,private

8.Cannot make a static reference to the non-static field counter

 9.

  1. Inner i = new Inner();  
  2. Outer o = new Outer();  
  3. int n = 10;  
  4. i.setX(n);  
  5. o.setY(i);  
  6. i.setX(100);  
  7. System.out.println(o.getY().getX()); 
輸出爲100,修改對象時對其地址進行修改,故不用重複set

10.

  1. Dogs myDog = Dogs.collie;  
  2. switch (myDog) {  
  3.     case collie:  
  4.         System.out.print("collie ");  
  5.     case harrier:  
  6.         System.out.print("harrier ");  
如果各個case後沒有break,則符合第一個case後,忽略下面各個case的判斷,直接執行結果

 11.

  1. public static void main(String[] args) {  
  2.     Integer i = new Integer(1) + new Integer(2);  
  3.     switch (i) {  
  4.         case 3:  
  5.             System.out.println("three");  
  6.             break;  
  7.         default:  
  8.             System.out.println("other");  
  9.             break;  
  10.     }  

swith可接受Integer,以上輸入爲three

12.抽象類繼承接口時可不實現接口內方法

 13.因爲接口定義的方法默認是public的,意思就是你沒有任何訪問修飾符的情況下,系統給接口的方法加上了一個public的訪問修飾符,實現接口的方法不能降低其訪問權限,故一定要爲public

14.TreeSet的API中有如下一句話:基於 TreeMap 的 NavigableSet 實現。使用元素的自然順序對元素進行排序,或者根據創建 set 時提供的 Comparator 進行排序,具體取決於使用的構造方法。也就是說TreeSet中的元素的相同與否有裏面對象的Comparator決定,由於你的Comparator中的cmpareTo()方法恆返回的是0也就是恆等所以在用迭代器遍歷你的TreeSet的時候會認爲裏面的兩個對象是同一個對象所以只訪問了第一個就輸出了Coffee.

  1. public class TestA implements Comparable {  
  2.     public String name;  
  3.  
  4.     public int compareTo(Object o) {  
  5.         return 0;  
  6.     }  
  7.  
  8.     public static void main(String[] args) {  
  9.         TestA one = new TestA();  
  10.         TestA two = new TestA();  
  11.         one.name = "Coffee";  
  12.         two.name = "Tea";  
  13.         TreeSet set = new TreeSet();  
  14.         set.add(one);  
  15.         set.add(two);  
  16.         System.out.println(set.size());  
  17.         for (Iterator iterator = set.iterator(); iterator.hasNext();) {  
  18.             TestA name = (TestA) iterator.next();  
  19.             System.out.println(name.name);  
  20.         }  
  21.     }  

 15

  1. ((TestA)new TestB()).start(); //這個理解成   
  2.  
  3. TestA t = new TestB();   
  4. t.start();  

 16.default修飾符只允許同包的的類訪問,若不同包則不能覆蓋。

17.

  1. public static void main(String...a)  
  2. public static void main(String[]... a)  

can be used

18.hibernate中,不可2個相同對象同時存在於session中

 

19.

Which three are valid? (Choose three.)

  1. class ClassA {} 
  2. class ClassB extends ClassA {} 
  3. class ClassC extends ClassA {} 
  4.  
  5. ClassA p0 = new ClassA(); 
  6. ClassB p1 = new ClassB(); 
  7. ClassC p2 = new ClassC(); 
  8. ClassA p3 = new ClassB(); 
  9. ClassA p4 = new ClassC();  
20.
ClassA是父類,ClassB,ClassC是兩個子類,

子類對象可以直接賦給父類變量,父類對象賦值給子類對象需要強制轉換,所以:
A. p0 = p1;正確
C. p2 = p4;錯誤,應該是:p2 = (ClassC)p4;
E. p1 = (ClassB)p3;正確
F. p2 = (ClassC)p4;正確


父類的兩個子類對象不可以互相賦值:所以:

B. p1 =p2;錯誤
D. p2 = (ClassC)p1;錯誤

答案:A E F 
21.
JavaBean Listener Naming Rules
Listener method names used to "register" a listener with an event source must use the prefix add, followed by the listener type. For example,addActionListener() is a valid name for a method that an event source will have to allow others to register for Action events.
Listener method names used to remove ("unregister") a listener must use the prefix remove, followed by the listener type (using the same rules as the registration add method).
The type of listener to be added or removed must be passed as the argument
Listener method names must end with the word "Listener".

所以listener的規則是添加是add 刪除是remove並且要跟上listener的具體類型
所以是兩個
addMouseListener和removeMouseListener 
22.this的用法:http://www.examda.com/JAVA/Instructs/060626/14071782.html
23.assert的用法:http://tiantian0521.blog.163.com/blog/static/41720883201073025939130/

 

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