annotation。
柯橋ssl適用于網站、小程序/APP、API接口等需要進行數據傳輸應用場景,ssl證書未來市場廣闊!成為創新互聯的ssl證書銷售渠道,可以享受市場價格4-6折優惠!如果有意向歡迎電話聯系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!
Annotation,是Java5的新特性,下面是Sun的Tutorial的描述,因為是英文,這里我翻譯下,希望能夠比較清晰的描述一下Annotation的語法以及思想。Annotation:Release 5.0 of the JDK introduced a metadata facility called annotations. Annotations provide data about a program that is not part of the program, such as naming the author of a piece of code or instructing the compiler to suppress specific errors. An annotation has no effect on how the code performs. Annotations use the form @annotation and may be applied to a program's declarations: its classes, fields, methods, and so on. The annotation appears first and often (by convention) on its own line, and may include optional arguments: JDK5引入了Metedata(元數據)很容易的就能夠調用Annotations.Annotations提供一些本來不屬于程序的數據,比如:一段代碼的作者或者告訴編譯器禁止一些特殊的錯誤。An annotation 對代碼的執行沒有什么影響。Annotations使用@annotation的形勢應用于代碼:類(class),屬性(field),方法(method)等等。一個Annotation出現在上面提到的開始位置,而且一般只有一行,也可以包含有任意的參數。@Author("MyName")class myClass() { }
or @SuppressWarnings("unchecked")void MyMethod() { }
Defining your own annotation is an advanced technique that won't be described here, but there are three built-in annotations that every Java programmer should know: @Deprecated, @Override, and @SuppressWarnings. The following example illustrates all three annotation types, applied to methods:
定義自己的Annotation是一個比較高級的技巧,這里我們不做討論,這里我們僅僅討論每一個Java programer都應該知道的內置的annotations:@Deprecated, @Override, and @SuppressWarnings。下面的程序闡述了這三種annotation如何應用于methods。import java.util.List;
class Food {}
class Hay extends Food {}
class Animal {
Food getPreferredFood() {
return null; } /** * @deprecated document why the method was deprecated */
@Deprecated
static void deprecatedMethod() { }
}
class Horse extends Animal {
Horse() {
return;
}
@Override
Hay getPreferredFood() {
return new Hay();
}
@SuppressWarnings("deprecation")
void useDeprecatedMethod() {
Animal.deprecateMethod(); //deprecation warning - suppressed }}
}
}
@DeprecatedThe @Deprecated annotation indicates that the marked method should no longer be used. The compiler generates a warning whenever a program uses a deprecated method, class, or variable. When an element is deprecated, it should be documented using the corresponding @deprecated tag, as shown in the preceding example. Notice that the tag starts with a lowercase "d" and the annotation starts with an uppercase "D". In general, you should avoid using deprecated methods — consult the documentation to see what to use instead.
@Deprecated@Deprecated annotation標注一個method不再被使用。編譯器在一個program(程序?)使用了不贊成的方法,類,變量的時候會產生警告(warning)。如果一個元素(element:method, class, or variable)不贊成被使用,應該像前面的例子里使用相應的@deprecated 標簽,并且注意標簽的首字母是小寫的"d",而annotation時大寫的"D"。一般情況下,我們應該避免使用不贊成使用的方法(deprecated methods),而應該考慮替代的方法。
@OverrideThe @Override annotation informs the compiler that the element is meant to override an element declared in a superclass. In the preceding example, the override annotation is used to indicate that the getPreferredFood method in the Horse class overrides the same method in the Animal class. If a method marked with @Override fails to override a method in one of its superclasses, the compiler generates an error. While it's not required to use this annotation when overriding a method, it can be useful to call the fact out explicitly, especially when the method returns a subtype of the return type of the overridden method. This practice, called covariant return types, is used in the previous example: Animal.getPreferredFood returns a Food instance. Horse.getPreferredFood (Horse is a subclass of Animal) returns an instance of Hay (a subclass of Food). For more information, see Overriding and Hiding Methods.
@Override@Override annotation 告訴編譯器當前元素是重寫(override)自父類的一個元素。在前面的例子中,override annotation用來說明Horse類中的getPreferredFood這個方法重寫(override)自Animal類中相同的方法。如果一個方法被標注了@Override,但是其父類中沒有這個方法時,編譯器將會報錯。但是并不是說我們一定要使用這個annotation,但是它能夠很明顯的給出實際行為,尤其是在方法返回一個被重寫的方法返回類型的子類型的時候。上面的例子中,Animal.getPreferredFood 返回一個 Food實例,Horse.getPreferredFood 返回一個Hay實例,這里Horse是Animal的子類,Hay是Food的子類。
@SuppressWarningsThe @SuppressWarnings annotation tells the compiler to suppress specific warnings that it would otherwise generate. In the previous example, the useDeprecatedMethod calls a deprecated method of Animal. Normally, the compiler generates a warning but, in this case, it is suppressed. Every compiler warning belongs to a category. The Java Language Specification lists two categories: "deprecation" and "unchecked". The "unchecked" warning can occur when interfacing with legacy code written before the advent of generics. To suppress more than one category of warnings, use the following syntax: @SuppressWarnings@SuppressWarnings annotation 告訴編譯器禁止別的元素產生的特殊的警告(warnings),在前面的例子里,useDeprecatedMethod調用了Animal的不贊成使用的一個方法。一般情況下,編譯器會給出一個警告(warning),但是在這種情況下,不會產生這個警告,也就是說被suppress。每個編譯器的警告都屬于一個類型。Java Language Specification列出了兩種類型:"deprecation" 和 "unchecked"。"unchecked" warning 發生在使用非generic的舊代碼交互的generic collection類時。為了禁止不止一種的警告時,使用下面的語法:@SuppressWarnings({"unchecked", "deprecation"})
你需要導入util包,可以直接寫import java.util.*,這樣就不用每個util下的包都每次導入了
深圳遠標(ITJOB)幫你:
Java代碼規范之一 ——標識符命名規范
轉載 2015-08-19 12:06:22
1. 標識符命名規范
1.1 概述
標識符的命名力求做到統一、達意和簡潔。
1.1.1
統一
統一是指,對于同一個概念,在程序中用同一種表示方法,比如對于供應商,既可以用supplier,也可以用provider,但是我們只能選定一個使用,至少在一個Java項目中保持統一。統一是作為重要的,如果對同一概念有不同的表示方法,會使代碼混亂難以理解。即使不能取得好的名稱,但是只要統一,閱讀起來也不會太困難,因為閱讀者只要理解一次。
1.1.2
達意
達意是指,標識符能準確的表達出它所代表的意義,比如: newSupplier,
OrderPaymentGatewayService等;而 supplier1,
service2,idtts等則不是好的命名方式。準確有兩成含義,一是正確,而是豐富。如果給一個代表供應商的變量起名是
order,顯然沒有正確表達。同樣的,supplier1, 遠沒有targetSupplier意義豐富。
1.1.3
簡潔
簡潔是指,在統一和達意的前提下,用盡量少的標識符。如果不能達意,寧愿不要簡潔。比如:theOrderNameOfTheTargetSupplierWhichIsTransfered 太長,
transferedTargetSupplierOrderName則較好,但是transTgtSplOrdNm就不好了。省略元音的縮寫方式不要使用,我們的英語往往還沒有好到看得懂奇怪的縮寫。
1.1.4
駱駝法則
Java中,除了包名,靜態常量等特殊情況,大部分情況下標識符使用駱駝法則,即單詞之間不使用特殊符號分割,而是通過首字母大寫來分割。比如:
SupplierName, addNewContract,而不是 supplier_name,
add_new_contract。
稍微深入一點的分析會認為該程序應該打印16,因為兩個Unicode轉義字符每一個在源文件中都需要用6個字符來表示,但是它們只表示字符串中的一個字符。因此這個字符串應該比它的外表看其來要短10個字符。 如果你運行這個程序,就會發現事情遠不是這么回事。它打印的既不是26也不是16,而是2。 理解這個謎題的關鍵是要知道:Java對在字符串字面常量中的Unicode轉義字符沒有提供任何特殊處理。編譯器在將程序解析成各種符號之前,先將Unicode轉義字符轉換成為它們所表示的字符[JLS 3.2]。因此,程序中的第一個Unicode轉義字符將作為一個單字符字符串字面常量("a")的結束引號,而第二個Unicode轉義字符將作為另一個單字符字符串字面常量("b")的開始引號。程序打印的是表達式"a".length()+"b".length(),即2。
JAVA中轉義字符:
1.八進制轉義序列:\ + 1到3位5數字;范圍'\000'~'\377'
\0:空字符
2.Unicode轉義字符:\u + 四個十六進制數字;0~65535
\u0000:空字符
3.特殊字符:就3個
\":雙引號
\':單引號
\\:反斜線
4.控制字符:5個
\' 單引號字符
\\ 反斜杠字符
\r 回車
\n 換行
\f 走紙換頁
\t 橫向跳格
\b 退格
點的轉義:. == u002E
美元符號的轉義:$ == u0024
乘方符號的轉義:^ == u005E
左大括號的轉義:{ == u007B
左方括號的轉義:[ == u005B
左圓括號的轉義:( == u0028
豎線的轉義:| == u007C
右圓括號的轉義:) == u0029
星號的轉義:* == u002A
加號的轉義:+ == u002B
問號的轉義:? == u003F
反斜杠的轉義: == u005C
網站標題:Java符號化代碼,java中的符號
本文網址:http://m.kartarina.com/article8/heipip.html
成都網站建設公司_創新互聯,為您提供網站收錄、網站內鏈、企業建站、服務器托管、外貿建站、搜索引擎優化
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯