本文目錄一覽:
- 1、java中的枚舉類型指的是什麼啊?
- 2、Java 什麼是註解及註解原理詳細介紹
- 3、java 枚舉類型怎麼添加註釋
- 4、Java語言中的枚舉類型如何使用?
- 5、java註解是怎麼實現的
- 6、枚舉類與註解9:類型註解(JDK8新特性)
java中的枚舉類型指的是什麼啊?
在Java中,枚舉類型本質上其實就是一個類,枚舉中的常量都是該枚舉類型的實例。雖然枚舉類型有一些限制,比如不能再派生出子枚舉類型,不能調用構造函數,不過我們仍然可以在枚舉類型中定義構造函數、欄位和方法
Java 什麼是註解及註解原理詳細介紹
1、註解是針對Java編譯器的說明。
可以給Java包、類型(類、介面、枚舉)、構造器、方法、域、參數和局部變數進行註解。Java編譯器可以根據指令來解釋註解和放棄註解,或者將註解放到編譯後的生成的class文件中,運行時可用。
2、註解和註解類型
註解類型是一種特殊的介面類型,註解是註解註解類型的一個實例。
註解類型也有名稱和成員,註解中包含的信息採用鍵值對形式,可以有0個或多個。
3、Java中定義的一些註解:
@Override 告訴編譯器這個方法要覆蓋一個超類方法,防止程序員覆蓋出錯。
@Deprecated 這個標識方法或類(介面等類型)過期,警告用戶不建議使用。
@SafeVarargs JDK7新增,避免可變參數在使用泛型化時候警告」執行時期無法具體確認參數類型「,當然,也可以用@SuppressWarnings來避免檢查,顯然後者的抑制的範圍更大。
@SuppressWarnings(value={“unchecked”}) 抑制編譯警告,應用於類型、構造器、方法、域、參數以及局部變數。 value是類型數組,有效取值為:
all, to suppress all warnings
boxing, to suppress warnings relative to boxing/unboxing operations
cast, to suppress warnings relative to cast operations
dep-ann, to suppress warnings relative to deprecated annotation
deprecation, to suppress warnings relative to deprecation
fallthrough, to suppress warnings relative to missing breaks in switch statements
finally, to suppress warnings relative to finally block that don’t return
hiding, to suppress warnings relative to locals that hide variable
incomplete-switch, to suppress warnings relative to missing entries in a switch statement (enum case)
javadoc, to suppress warnings relative to javadoc warnings
nls, to suppress warnings relative to non-nls string literals
null, to suppress warnings relative to null analysis
rawtypes, to suppress warnings relative to usage of raw types
restriction, to suppress warnings relative to usage of discouraged or forbidden references
serial, to suppress warnings relative to missing serialVersionUID field for a serializable class
static-access, to suppress warnings relative to incorrect static access
static-method, to suppress warnings relative to methods that could be declared as static
super, to suppress warnings relative to overriding a method without super invocations
synthetic-access, to suppress warnings relative to unoptimized access from inner classes
unchecked, to suppress warnings relative to unchecked operations
unqualified-field-access, to suppress warnings relative to field access unqualified
unused, to suppress warnings relative to unused code and dead code
4、註解的定義
使用 @interface 關鍵字聲明一個註解
public @interface MyAnnotation1
註解中可以定義屬性
String name default 「defval」;
value是註解中的特殊屬性
註解中定義的屬性如果名稱為 value, 此屬性在使用時可以省寫屬性名
例如,聲明一個註解:
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnno1 {
String msg();
int value();
}
java 枚舉類型怎麼添加註釋
枚舉類型添加註釋是什麼意思?
java 注釋 有
單行注釋 //
多行注釋 /* … */
JavaDoc /** … */
Java語言中的枚舉類型如何使用?
Java語言中的枚舉類型的使用方法如下:
用法一:常量;
public enum Color {
RED, GREEN, BLANK, YELLOW
}
用法二:switch;
enum Signal {
GREEN, YELLOW, RED
}
public class TrafficLight {
Signal color = Signal.RED;
public void change() {
switch (color) {
case RED:
color = Signal.GREEN;
break;
case YELLOW:
color = Signal.RED;
break;
case GREEN:
color = Signal.YELLOW;
break;
}
}
}
用法三:向枚舉中添加新方法;
public enum Color {
RED(“紅色”, 1), GREEN(“綠色”, 2), BLANK(“白色”, 3), YELLO(“黃色”, 4);
// 成員變數
private String name;
private int index;
// 構造方法
private Color(String name, int index) {
this.name = name;
this.index = index;
}
// 普通方法
public static String getName(int index) {
for (Color c : Color.values()) {
if (c.getIndex() == index) {
return c.name;
}
}
return null;
}
// get set 方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
}
用法四:覆蓋枚舉的方法;
public class Test {
public enum Color {
RED(“紅色”, 1), GREEN(“綠色”, 2), BLANK(“白色”, 3), YELLO(“黃色”, 4);
// 成員變數
private String name;
private int index;
// 構造方法
private Color(String name, int index) {
this.name = name;
this.index = index;
}
// 覆蓋方法
@Override
public String toString() {
return this.index + “_” + this.name;
}
}
public static void main(String[] args) {
System.out.println(Color.RED.toString());
}
}
用法五:實現介面;
public interface Behaviour {
void print();
String getInfo();
}
public enum Color implements Behaviour {
RED(“紅色”, 1), GREEN(“綠色”, 2), BLANK(“白色”, 3), YELLO(“黃色”, 4);
// 成員變數
private String name;
private int index;
// 構造方法
private Color(String name, int index) {
this.name = name;
this.index = index;
}
// 介面方法
@Override
public String getInfo() {
return this.name;
}
// 介面方法
@Override
public void print() {
System.out.println(this.index + “:” + this.name);
}
}
用法六:使用介面組織枚舉。
public interface Food {
enum Coffee implements Food {
BLACK_COFFEE, DECAF_COFFEE, LATTE, CAPPUCCINO
}
enum Dessert implements Food {
FRUIT, CAKE, GELATO
}
}
以上就是Java語言中枚舉類型的基本使用方法。
java註解是怎麼實現的
註解的使用一般是與java的反射一起使用,下面是一個例子
註解相當於一種標記,在程序中加了註解就等於為程序打上了某種標記,沒加,則等於沒有某種標記,以後,javac編譯器,開發工具和其他程序可以用反射來了解你的類及各種元素上有無何種標記,看你有什麼標記,就去干相應的事。標記可以加在包,類,欄位,方法,方法的參數以及局部變數上。
自定義註解及其應用
1)、定義一個最簡單的註解
public @interface MyAnnotation {
//……
}
2)、把註解加在某個類上:
@MyAnnotation
public class AnnotationTest{
//……
}
以下為模擬案例
自定義註解@MyAnnotation
1 package com.ljq.test;
2
3 import java.lang.annotation.ElementType;
4 import java.lang.annotation.Retention;
5 import java.lang.annotation.RetentionPolicy;
6 import java.lang.annotation.Target;
7
8 /**
9 * 定義一個註解
10 *
11 *
12 * @author jiqinlin
13 *
14 */
15 //Java中提供了四種元註解,專門負責註解其他的註解,分別如下
16
17 //@Retention元註解,表示需要在什麼級別保存該注釋信息(生命周期)。可選的RetentionPoicy參數包括:
18 //RetentionPolicy.SOURCE: 停留在java源文件,編譯器被丟掉
19 //RetentionPolicy.CLASS:停留在class文件中,但會被VM丟棄(默認)
20 //RetentionPolicy.RUNTIME:內存中的位元組碼,VM將在運行時也保留註解,因此可以通過反射機制讀取註解的信息
21
22 //@Target元註解,默認值為任何元素,表示該註解用於什麼地方。可用的ElementType參數包括
23 //ElementType.CONSTRUCTOR: 構造器聲明
24 //ElementType.FIELD: 成員變數、對象、屬性(包括enum實例)
25 //ElementType.LOCAL_VARIABLE: 局部變數聲明
26 //ElementType.METHOD: 方法聲明
27 //ElementType.PACKAGE: 包聲明
28 //ElementType.PARAMETER: 參數聲明
29 //ElementType.TYPE: 類、介面(包括註解類型)或enum聲明
30
31 //@Documented將註解包含在JavaDoc中
32
33 //@Inheried允許子類繼承父類中的註解
34
35
36 @Retention(RetentionPolicy.RUNTIME)
37 @Target({ElementType.METHOD, ElementType.TYPE})
38 public @interface MyAnnotation {
39 //為註解添加屬性
40 String color();
41 String value() default “我是林計欽”; //為屬性提供默認值
42 int[] array() default {1, 2, 3};
43 Gender gender() default Gender.MAN; //添加一個枚舉
44 MetaAnnotation metaAnnotation() default @MetaAnnotation(birthday=”我的出身日期為1988-2-18″);
45 //添加枚舉屬性
46
47 }
註解測試類AnnotationTest
1 package com.ljq.test;
2
3 /**
4 * 註解測試類
5 *
6 *
7 * @author jiqinlin
8 *
9 */
10 //調用註解並賦值
11 @MyAnnotation(metaAnnotation=@MetaAnnotation(birthday = “我的出身日期為1988-2-18″),color=”red”, array={23, 26})
12 public class AnnotationTest {
13
14 public static void main(String[] args) {
15 //檢查類AnnotationTest是否含有@MyAnnotation註解
16 if(AnnotationTest.class.isAnnotationPresent(MyAnnotation.class)){
17 //若存在就獲取註解
18 MyAnnotation annotation=(MyAnnotation)AnnotationTest.class.getAnnotation(MyAnnotation.class);
19 System.out.println(annotation);
20 //獲取註解屬性
21 System.out.println(annotation.color());
22 System.out.println(annotation.value());
23 //數組
24 int[] arrs=annotation.array();
25 for(int arr:arrs){
26 System.out.println(arr);
27 }
28 //枚舉
29 Gender gender=annotation.gender();
30 System.out.println(“性別為:”+gender);
31 //獲取註解屬性
32 MetaAnnotation meta=annotation.metaAnnotation();
33 System.out.println(meta.birthday());
34 }
35 }
36 }
枚舉類Gender,模擬註解中添加枚舉屬性
1 package com.ljq.test;
2 /**
3 * 枚舉,模擬註解中添加枚舉屬性
4 *
5 * @author jiqinlin
6 *
7 */
8 public enum Gender {
9 MAN{
10 public String getName(){return “男”;}
11 },
12 WOMEN{
13 public String getName(){return “女”;}
14 }; //記得有「;」
15 public abstract String getName();
16 }
註解類MetaAnnotation,模擬註解中添加註解屬性
1 package com.ljq.test;
2
3 /**
4 * 定義一個註解,模擬註解中添加註解屬性
5 *
6 * @author jiqinlin
7 *
8 */
9 public @interface MetaAnnotation {
10 String birthday();
11 }
枚舉類與註解9:類型註解(JDK8新特性)
類型註解:
關於元註解@Target的參數類型ElementType枚舉值多了兩個:TYPE_PARAMETER,TYPE_USE
ElementType.TYPE_PARAMETER:表示該註解能寫在類型變數的聲明語句中(如,泛型聲明)
ElementType.TYPE_USE:表示該註解能寫在使用類型的任何語句中。
具體舉例:
MyAnnotation.java
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/241309.html