章节练习:枚举类&注解

枚举类

第1题

1、声明颜色枚举类:
7个常量对象:赤、橙、黄、绿、青、蓝、紫。
2、在测试类中,使用枚举类,获取绿色对象,并打印对象。

package test1;

/**
 * @author yhm
 * @create 2020-07-16 14:03
 */
public enum Color {
    RED,ORANGE,YELLOW,GREEN,CYAN,BLUE,PURPLE
}

package test1;

/**
 * @author yhm
 * @create 2020-07-16 14:05
 */
public class Test {
    public static void main(String[] args) {
        System.out.println(Color.GREEN);
    }
}

第2题

案例:
1、声明月份枚举类Month:
(1)创建:1-12月常量对象
(2)声明两个属性:value(月份值,例如:JANUARY的value为1),
description(描述,例如:JANUARY的description为1月份是一年的开始)。
(3)声明一个有参构造,创建12个对象
(4) 声明一个方法:public static Month getByValue(int value)
(5)重写toString():返回对象信息,例如:1->JANUARY->1月份是一年的开始。
2、在测试类中,从键盘输入1个1-12的月份值,获取对应的月份对象,并打印对象

package test2;

/**
 * @author yhm
 * @create 2020-07-16 14:08
 */
public enum  Month {
    JANUARY(1,"一月"),FEBRUARY(2,"二月"),MARCH(3,"三月"),
    APRIL(4,"四月"),MAY(5,"五月"),JUNE(6,"六月"),
    JULY(7,"七月"),AUGUST(8,"八月"),SEPTEMBER(9,"九月"),
    OCTOBER(10,"十月"),NOVEMBER(11,"十一月"),DECEMBER(12,"十二月");

    private int value;
    private String description;

    Month(int value, String description) {
        this.value = value;
        this.description = description;
    }
    public static Month getByValue(int value){
        Month[] values = Month.values();
        return values[value-1];
    }

    @Override
    public String toString() {
        return "Month{" +
                "value=" + value +
                ", description='" + description + '\'' +
                '}';
    }
}

package test2;

import java.util.Scanner;

/**
 * @author yhm
 * @create 2020-07-16 14:25
 */
public class Test {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();
        Month month = Month.getByValue(num);
        System.out.println(month);
    }
}

第3题

案例:
1、声明可支付接口Payable:
包含抽象方法:void pay();
2、声明支付枚举类Payment:
(1)创建常量对象:支付宝(ALIPAY),微信(WECHAT),信用卡(CREDIT_CARD),储蓄卡
(DEPOSIT_CARD)
(2)枚举类Payment实现接口Payable
①支付宝/微信:对接口的实现是打印“扫码支付”
②信用卡/储蓄卡:对接口的实现是打印“输入卡号支付”
3、在测试类中,获取所有支付对象,并调用它们的pay()

package test3;

/**
 * @author yhm
 * @create 2020-07-16 14:41
 */
public interface Payable {
    void pay();
}

package test3;

/**
 * @author yhm
 * @create 2020-07-16 14:42
 */
public enum Payment implements Payable{
    ALIPAY,WECHAT,CREDIT_CARD,DEPOSIT_CARD;

    @Override
    public void pay() {
        if (this == ALIPAY || this == WECHAT){
            System.out.println("扫码支付");
        }else {
            System.out.println("输入卡号支付");
        }
    }
}

package test3;

/**
 * @author yhm
 * @create 2020-07-16 14:46
 */
public class Test {
    public static void main(String[] args) {
        Payment.ALIPAY.pay();
        Payment.CREDIT_CARD.pay();
        Payment.DEPOSIT_CARD.pay();
        Payment.WECHAT.pay();
    }
}

注解编程题

第1题

案例:
1、编写图形工具类:ShapTools
(1)声明方法1:public static void printRectangle(),打印5行5列*组成的矩形图形
(2)声明方法2:public static void printRectangle(int line, int column, String sign),打印line行column列由sign
组成的矩形图形
(3)给这个类加上文档注释:包含@author,@param等
(4)给方法1标记已过时注解
2、编写测试类Test01
在测试类中调用上面的两个方法测试,如果有警告,就在main方法上抑制警告

package tsst4;

/**
 * @author yhm
 * @create 2020-07-16 14:56
 */
public class Test {
    public static void main(String[] args) {
        ShapTools.printRectangle();
        ShapTools.printRectangle(3, 10, "#");
    }
}

class ShapTools {
    @Deprecated
    public static void printRectangle() {
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }

    public static void printRectangle(int line, int column, String sign) {
        for (int i = 0; i < line; i++) {
            for (int j = 0; j < column; j++) {
                System.out.print(sign);
            }
            System.out.println();
        }
    }
}

第2题

1、声明自定义注解@Table
(1)加上String类型的配置参数value
(2)并限定@Table的使用位置为类上
(3)并指定生命周期为“运行时”
2、声明自定义注解@Column
(1)加上String类型的配置参数name,表示表格的列名
(2)加上String类型的配置参数type,表示表格的列数据类型
(3)并限定@Column的使用位置在属性上
(4)并指定生命周期为“运行时”
3、声明User类,
(1)属性:id, username, password, email
(2)在User类上,标记@Table注解,并为value赋值为"t_user"
(3)在User类的每一个属性上标记@Column,并为name和type赋值,例如:
id:name赋值为no,type赋值为int
username:name赋值为username,type赋值为varchar(20)
password:name赋值为pwd,type赋值为char(6)
email:name赋值为email,type赋值为varchar(50)

package test5;

/**
 * @author yhm
 * @create 2020-07-16 15:00
 */

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Column {
    String name();
    String type();
}

package test5;

/**
 * @author yhm
 * @create 2020-07-16 14:59
 */
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Table {
    String value();
}

package test5;

/**
 * @author yhm
 * @create 2020-07-16 15:00
 */

@Table("t_user")
public class User {
    @Column(name = "no", type = "int")
    private int id;
    @Column(name = "username", type = "varchar(20)")
    private String username;
    @Column(name = "pwd", type = "char(6)")
    private String password;
    @Column(name = "email", type = "varchar(50)")
    private String email;

    public User(int id, String username, String password, String email) {
        super();
        this.id = id;
        this.username = username;
        this.password = password;
        this.email = email;
    }

    public User() {
        super();
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @
            Override
    public String toString() {
        return "User [id=" + id + ", username=" + username + ", password=" + password
                + ", email=" + email + "]";
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值