搜尋此網誌

2009年12月2日 星期三

Java Annotation



  • 在Java 1.5 ,annotation data可以在runtime and compile-time處理




  • 紫色的字是Marker Annotation的例子





    public class Annotation {


    }
    class A{

    @Deprecated

    public void test(){

    }
    }




  • Annotation Type是annotations的藍圖,宣告Annotation Type類似宣告interface,只是在interface前面多加了@符號:


     

    public @interface TestAnnotationType {

    String printSomthing();
    }




  • Annotation Type的元素是用方法來定義,這個方法不可以傳入參數,也不可以丟出例外,而回傳值必須是基本型別,
    String, Class, enums, annotations, 以及以上型別的陣列,方法也可以有預設值.


     


    public @interface TestAnnotation {

    String sayHello();
    String defaultSayHello() default "hello";
    }





  • Annotation可以提供額外的資訊給程式使用,可以在compile的時候設定deprecated method展示出warning messages,或是說利用annotation
    讓一些tools產生程式碼,或是說在執行時期把值(Value)傳給method使用.





  • Annotation可以用在class,interface,method,還有field





  • Java5提供了metadata的能力,可以讓你自己定義annotation type.






  • 一個沒有元素的Annotation type被稱為Marker annotation type.




    public @interface TestAnnotation {


    }




  • Marker annotation type的使用上可以省略{},如下例:





    public @interface TestAnnotation {


    }
    @TestAnnotation
    class Test1 { }





  • 單一元素的annotation type可以直接給值,如下例:






    public @interface TestAnnotation {

    String value();
    }
    @TestAnnotation ("Test")
    class Test1 { }





  • annotation 是一種特別的修飾子,它可以放在任何修飾子(諸如public ,privte,protected,等等)可以放的地方,而annotations的值只可以是編譯時期常數.






Annotation的整理













import java.lang.annotation.*;
import java.lang.reflect.Method;


@Retention(RetentionPolicy.RUNTIME) //告訴Jvm可以在執行時期讀取
@Target(ElementType.METHOD)//這個annotation type只可以用在method
public @interface Test {
}

class Foo {
@Test public static void m1() { }
public static void m2() { }
@Test public static void m3() {
throw new RuntimeException("Boom");
}
public static void m4() { }
@Test public static void m5() { }
public static void m6() { }
@Test public static void m7() {
throw new RuntimeException("Crash");
}
public static void m8() { }
}

class RunTest {

public static void main(String[] args) throws Exception {
String[]ar={"Foo"};
int passed = 0, failed = 0;
for (Method m : Class.forName(ar[0]).getMethods()) {
if (m.isAnnotationPresent(Test.class)) {//依次取出annotation type為Test的method
try {
m.invoke(null);
passed++;
} catch (Throwable ex) {//如果有丟出exception就算失敗
System.out
.printf("Test %s failed: %s %n", m, ex.getCause());
failed++;
}
}
}
System.out.printf("Passed: %d, Failed %d%n", passed, failed);
}
}



參考連結:
Java Guide

Java存取修飾子


有兩種等級的存取控制:


類別等級(class level)包含了:public, or default (no modifier).


成員等級(member level)包含了:public, private, protected, or default (no modifier).


以下table展示了每一個修飾子可以存取的成員:


















































修飾子

Package內,類別本身

Package內,不同類別之間

不同Package內的子類別

所有類別




public


Y

Y

Y

Y




protected


Y

Y

Y

N



no modifier

Y

Y

N

N




private


Y

N

N

N












第一欄可解讀為:

Package內,類別本身可存取(可以看到)的成員為private,protected,default,public





package One;
public class SuperClassA {
public int a=1;
protected int b=2;
int c=3;
private int d=4;
public static void main(String[]args){
System.out.println(new SuperClassA().a);
System.out.println(new SuperClassA().b);
System.out.println(new SuperClassA().c);
System.out.println(new SuperClassA().d);

}
}




第二欄可解讀為:

Package內,不同類別之間可存取(可以看到)的成員為protected,default,public





package One;

public class SuperClassA {

public int a=1;
protected int b=2;
int c=3;
private int d=4;
public static void main(String[]args){

System.out.println(new B().a);
System.out.println(new B().b);
System.out.println(new B().c);
//compile-error:System.out.println(new B().d);

}
}
class B{


public int a=1;
protected int b=2;
int c=3;
private int d=4;


}


第三欄可解讀為:

不同Package內的子類別,可存取(可以看到)的成員為protected,public兩者





package Two;
import One.SuperClassA;

class SubOfA extends SuperClassA {

public SubOfA(){
System.out.println(super.a);
System.out.println(super.b);
//compile Error--->System.out.println(super.c);
//compile Error--->System.out.println(super.d);
}

}



第四欄可解讀為:

所有類別皆可存取(可以看到)的成員只有public





package Two;
import One.SuperClassA;

public class OtherClass {
public static void main(String[]args){
System.out.println(new SuperClassA().a);
//compile error--->System.out.println(new Alpha().b);
//compile error--->System.out.println(new Alpha().c);
//compile error--->System.out.println(new Alpha().d);

}

}



work

中国語も日本語と英語も、とともに勉強した方がいいと思います, やはり、international時代が来るんだ。.しかし業務なんかの仕事はやりたくない

2009年12月1日 星期二

Speaking用語

on the positive side

副詞單字筆記

fundamentally:基礎地

動詞片語

cope with=deal with ,to deal successfully with a difficult situation

exposure :暴露

utilize :善加利用

neglect:疏忽
to give not enough care or attention to people or things that are your responsibility

reference:http://dictionary.cambridge.org/define.asp

名詞筆記

clarify (v) /ˈklær.ɪ.faɪ/澄清

to make something clear or easier to understand by giving more details




pronoun (n) /ˈprəʊ.naʊn/ 代名詞

a word which is used instead of a noun or a noun phrase




diversity 差異

when many different types of things or people are included in something


variation
a change in amount or level



asset:有益


staff:工作人員
the group of people who work for an organization

stuff:物品


conformity:遵從
behaviour that follows the usual standards which are expected by a group or society

tension:緊張
a feeling of nervousness before an important or difficult event



applicant:申請人
a person who formally requests something, especially a job, or a place at college or university


reward:獎賞
an amount of money given to someone who helps the police or who helps to return stolen property to its owner

incentive:鼓勵
something which encourages a person to do something


recruit:新成員

strain:pressure:壓力

interpretation:解釋
scholar:學者
mystery:神祕
dozen:12個一組
alteration(衣服的修改)
alternative:選擇
magnificent:非常好





reference:http://dictionary.cambridge.org/define.asp

介係詞(Preposition)的用法

介係詞在關係子句的位置

1.通常放在關係子句的最後面

e.g :Do you know the girl (that) John is talking to?

2.在正式文章中也有放在句中

e.g :The tree under which they had their picnic was the largest and oldest in the park.

3.
介係詞通常放在名詞或代名詞前面

4.下列是常見的地方介係詞用法

aboard - She is aboard the boat.
above - The picture is above the sofa.
across - My house is across the street.
against - The desk is against the wall.
around - My house is around the block.
at - Is your house at the end of the street.
at the back of - We are going to sit at the back of the theater.

at the bottom of - The coins are at the bottom of the lake.

at the top of - The books are at the top of the shelves.

between - We sit between the two boys.

behind - The girls sit behind the two boys.

below - The desk is below the window

by - The books are by the door.

in - I live in the big green and white house.

inside - I live inside the big green house.

on the corner of - We live on the corner of 3rd avenue
in the middle of - We live in the middle of the street.

near - I don't live near the supermarket.

next to - I live next to my best friend.

to the left of - The blue box is to the left of the green box.

to the right of - The orange box is to the right of the yellow box.

on - The sun heater is on the top of the building.

on the side of - There is a big sign on the side of the house.

on top of - There is a man on the top of the roof.

on the other side of - Do you see what is going on over there on the other side of the roof?

opposite - The post office is on the opposite side of the street.

outside - The car is outside the garage.

under - The blanket is under the bed in a box.

underneath - The pen is underneath the box.

5.時間介係詞的用法

  • after - I will be there after work.
  • around - We will be there around 3 PM
  • before - I will be there before I go to school.
  • between -
  • by - I will be there by the time that you leave for work.
  • during - I will be there during your class.
  • for - I will be there for your birthday.
  • past - I wasn't there for the past 2 months.
  • since - I didn't see her since I was 10 years old.
  • until - I will not be home until 7:00 PM.
  • within - I will be there within 2 hours.

reference:http://www.edufind.com/english/grammar/relative_clauses_and_preposition.php#
reference:http://www.english-the-easy-way.com/Prepositions/Place_Prepositions.htm

2009年11月27日 星期五

雅思聽力準備技巧

通常Listening的一開始,都會先示範兩個人的對話,然後把答案寫出來,接下來就正式聽力測驗

這時候會重複把答案又說一次,所以可以好好利用這段說明的時間,把問題仔細的看一次,

看的時候要有文法概念.比如說 v+o (n),或 v+ adv, adj+n,當聽到這題的時候要特別注意

是否有出現相關字眼,還有當答案為名詞的時候,要注意單複數,和前面的冠詞.

2009年11月26日 星期四

形容詞單字筆記

get accustomed to : 習慣於
technicalities: 術語
offensive (a):攻擊性的
inappropriate(a):不合適的
well-informed(a):見多識廣的
multinational :跨國
neglected :忽視的,疏忽的
contemporary:當代的
clearly identifiable:清晰可辨
unintentional:並非故意的
disrespectful:缺少尊重的
intact:未受損傷的

2009年6月25日 星期四

2009年6月15日 星期一

我也進來了,
謝謝佳民兄

信宏

2009年5月20日 星期三

開版文

Hi all:

有來看的人可在這留言!!

記得到張貼選項更改日期和時間.

系統預設時間是錯的

佳民