注解
(1)注解也被称为元数据(Metdata),用于修饰解释 包、类、方法、属性、构造器、局部变量等数据信息
(2)和注释一样,注解不影响程序逻辑,但注解可以被编译或运行,相当于嵌入在代码中的补充信息。
(3)在javaSE中,注解的使用目的比较简单,例如标记过时的功能,忽略警告等。在javaEE中注解占据了更重要的角色,例如用来配置应用程序的任何切面,代替javaEE旧版中所遗留的繁冗代码和xml配置等
-
三种基本的注解(Annotation)
(1)@Override:限定某个方法,重写父类方法,(是JDK5.0后加入的)
(2)@Deprecated:用于表示某个程序元素已过时(过时不代表不能用)
(3)@SuppressWarnings:抑制编译器警告(抑制范围和你写的位置有关)
-
四种元注解:
(1)@Retention //指定注解的作用范围
(2)@Target //指定注解可以在那些地方使用
(3)@Docunmented //指定该注解是否会在javadoc体现
(4)@Inherited //子类会继承父类注解
本章作业
(1)创建一个枚举类Color
(2)有RED,BLUE,BLACK,YELLOW,GREEN这五个枚举值/对象
(3)Color有三个属性
(4)创建构造方法,参数包含这三个属性
(5)每个枚举类都要给这三个属性赋值
(6)定义接口,里面有方法show,Color实现该接口
(7)show方法中显示三属性的值
(8)将枚举对象在switch中匹配使用
public class Test {
public static void main(String[] args) {
Color[] colors = Color.values();
for(Color color : colors){
System.out.print(color+" ");
color.show();
}
Color black = Color.RED;
switch(black){
case RED:
System.out.println("匹配到了红色");
black.show();
break;
case BLUE:
System.out.println("匹配到了蓝色");
black.show();
break;
case BLACK:
System.out.println("匹配到了黑色");
black.show();
break;
case YELLOW:
System.out.println("匹配到了黄色");
black.show();
break;
case GREEN:
System.out.println("匹配到了绿色");
black.show();
break;
}
}
}
enum Color implements colorshow{
RED(255,0,0),BLUE(0,0,255),BLACK(0,0,0),YELLOW(255,255,0),GREEN(0,255,0);
private int redValue;
private int greenValue;
private int blueValue;
Color(int redValue, int greenValue, int blueValue) {
this.redValue = redValue;
this.greenValue = greenValue;
this.blueValue = blueValue;
}
public int getRedValue() {
return redValue;
}
public int getGreenValue() {
return greenValue;
}
public int getBlueValue() {
return blueValue;
}
public void show() {
System.out.println("属性值为 " + redValue + " " + greenValue + " " + blueValue);
}
}
interface colorshow{
public void show();
}