【java】javaSE知识梳理-注释+枚举

注释:对程序的解析

注释:代码解析

/** 文档注释:快捷键:Alt+control+J,设置:Window --> Preferences --> Java --> Code Style  --> Code Templates --> Comments --> types --> Edit... 
* 项目名称:javaSE   
* 类名称:Annotation   
* 类描述:   
* 创建人:chendan   
* 创建时间:2021年3月11日 下午8:16:22   
* @version        
*/
public class Annotation {
    public static void main(String[] args) {
//         单行注释: 快捷键:control+/ 取消:control+/;快捷键的设置:Windows-->Preference-->General-->Keys
        /* 多行注释:快捷键:control+shift+/ 取消:control+shift+\ */ 
        /*
         * System.out.println("Hello"); System.out.println("World");
         */
        System.out.println("!");
    }
}
View Code

标识符:代码解析

public class Identifier {
    /*
     * 注释符:类名+变量名+方法名等 规则:A-Z a-z $ _ 开始,首字符之后:~+数字, 区分大小写、不能用关键字 命名规则
     * 包名:全小写;类/接口名:首字母大写;变量/方法名:第二个单词开始首字母大写;常量名:所有字母大写,字母之间用下划线连接
     */

    public static final double PI_LIST = 3.14;

    public static void main(String[] args) {
        // identifier
        String hello = "Hello World!"; // 小写字母开头
        String Hello = "Hello World!"; // 大写字母开头
        String $hello = "Hello World!"; // $字母开头
        String _hello = "Hello World!"; // _开头
        String hello123 = "Hello World!"; // 首字母之后用数字
    }

    public void methodTest() {

    }
}
View Code

 测试类

package javaLang.annotation.methods;

import org.junit.Test;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Date;

/**
 * @author ChenDan
 * @create 2021-05-19
 * @version 1.0
 * @see Annotations.java
 * @since jdk5.0
 * param null
 * return null
 * exception null
 * @desc 文档注解的描述
 */
public class Methods {

    /**
     * 编译时注解
     * public @interface SuppressWarnings:一直编译警告器
     * public @interface Deprecated:提示对应方法已过时
     * public @interface Override:重写父类/接口的方法
     */
    public static void main(String[] args) {
        @SuppressWarnings("unused")
        int i;
        @SuppressWarnings({"unused","raw"})
        ArrayList list = new ArrayList();

        Date date = new Date(2020,10,11);  // @Deprecated
    }

    @Test
    public void getAnnotation1() {
        Class clazz = Student.class;
        Annotation[] annotations = clazz.getAnnotations();
        for (int i = 0; i < annotations.length; i++) {
            System.out.println(annotations[i]);
        }
//        for (Annotation annotation:annotations) {
//            System.out.println(annotation);
//        }
    }
}

/**
 * Override重写父类/接口的方法
 * jdk1.8之后的重复注解
 */
@MyAnnotation(value = "one")
@MyAnnotation(value = "two")
//@MyAnnotations({@MyAnnotation(value = "one"), @MyAnnotation(value = "two")})
class Person{
    private String name;
    private String age;

    public Person() {
    }
    public String getName() {
        return name;
    }
    public String getAge() {
        return age;
    }
    public void walk() {
        System.out.println("人走了");
    }
    public void eat() {
        System.out.println("人吃饭");
    }
}

interface Info{
    void show();
}

class Student extends Person implements Info{
    @Override
    public void walk() {
        System.out.println("学生走路");
    }

    @Override
    public void show() {
        System.out.println("重写接口方法");
    }
}
/**
 * jdk1.8之后的类型注解
 */
class Genric <@MyAnnotation T> {
    public void show() throws @MyAnnotation RuntimeException{
        ArrayList<@MyAnnotation String> list = new ArrayList<>();
        int num = (@MyAnnotation int) 10L;
    }

}
View Code

自定义注释1

package javaLang.annotation.methods;

import java.lang.annotation.*;

import static java.lang.annotation.ElementType.*;

@Repeatable(MyAnnotations.class)
@Documented
@Inherited
@Target({PACKAGE,TYPE,CONSTRUCTOR,METHOD,LOCAL_VARIABLE,FIELD,PARAMETER,TYPE_PARAMETER,TYPE_USE})
@Retention(value = RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
    String value() default "hello";
}
View Code

自定义注释2

package javaLang.annotation.methods;

import java.lang.annotation.*;

import static java.lang.annotation.ElementType.*;

/**
 * @author ChenDan
 * @create 2021-05-22
 * @desc
 */
@Documented
@Inherited
@Target({PACKAGE,TYPE,CONSTRUCTOR,METHOD,LOCAL_VARIABLE,FIELD,PARAMETER,TYPE_PARAMETER})
@Retention(value = RetentionPolicy.RUNTIME)
public @interface MyAnnotations {
    MyAnnotation[] value();
}
View Code

 枚举:对象可有数据且数据可全部举例

自定义枚举类

package javaLang.enum1.methods;

public class SeasonTest {
    public static void main(String[] args) {
        Season spring = Season.SPRING;
        System.out.println(spring);
    }
}
/**
 * 自定义枚举类<pre>
 *     构造器:private
 *     变量私有化:private final+在构造器中初始化
 *     对象:public static final
 * </pre>
 * @author ChenDan
 * @create 2021-05-19
 */
class Season {
    private final String seasonName;
    private final String seasonDesc;

    private Season(String seasonName, String seasonDesc) {
        this.seasonName = seasonName;
        this.seasonDesc = seasonDesc;
    }

    public static final Season SPRING = new Season("春天","春暖花开");
    public static final Season SUMMER = new Season(" 夏天","夏日炎炎");
    public static final Season AUTUNMN = new Season("秋天","秋高气爽");
    public static final Season WINTER = new Season("冬天","白雪皑皑");

    public String getSeasonName() {
        return seasonName;
    }

    public String getSeasonDesc() {
        return seasonDesc;
    }

    @Override
    public String toString() {
        return "Season{" +
                "seasonName='" + seasonName + '\'' +
                ", seasonDesc='" + seasonDesc + '\'' +
                '}';
    }
}
View Code

enum自定义枚举

package javaLang.enum1.methods;

import org.junit.Test;

public class SeasonTest1{

    class TempClass implements Comparable{

        @Override  // 重写方法
        public int compareTo(Object o) {
            return 0;
        }

        @Deprecated  // 不鼓励使用这样的元素
        int num;

        @SuppressWarnings("unused") // 该类的警告取消,不过警告是个什么东西??
        Season1 winter = Season1.valueOf("hj");
    }

    @Test
    public void test() {
        Season1 summer = Season1.SUMMER;
        System.out.println(summer.toString());  // 返回对象的名称

        Season1[] values = Season1.values();  // 遍历枚举数组
        for (int i = 0; i < values.length; i++) {
            System.out.println(values[i]);
            values[i].show();
        }
        @SuppressWarnings("unused") // 该类的警告取消
        Season1 winter = Season1.valueOf("hj");
//        Season1 winter = Season1.valueOf("SUMMER");
//        System.out.println(winter);
    }
}

interface Info{
    void show();
}

/**
 * 自定义枚举类<pre>
 *     构造器:private
 *     变量私有化:private final+在构造器中初始化
 *     对象:public static final
 * </pre>
 * @author ChenDan
 * @create 2021-05-19
 */
enum Season1 implements Info{
    SPRING("春天","春暖花开"){
        @Override
        public void show(){
            System.out.println("春天在哪里?");
        }
    },
    SUMMER("夏天","夏日炎炎"){
        @Override
        public void show(){
            System.out.println("宁夏");
        }
    },
    AUTUMN("秋天","秋高气爽"){
        @Override
        public void show(){
            System.out.println("秋天不回来");
        }
    },
    WINRTER("冬天","白雪皑皑"){
        @Override
        public void show(){
            System.out.println("大约在冬季");
        }
    };

    private final String seasonName;
    private final String seasonDesc;

    Season1(String seasonName, String seasonDesc) {
        this.seasonName = seasonName;
        this.seasonDesc = seasonDesc;
    }

    public String getSeasonName() {
        return seasonName;
    }

    public String getSeasonDesc() {
        return seasonDesc;
    }

//    @Override
//    public void show() {
//        System.out.println("这是一个季节!");
//    }
}
View Code

 

posted @ 2021-03-11 23:14  cdan134  阅读(80)  评论(0)    收藏  举报