高级java实验指导书(1)

1、了解注解的基本概念和为什么使用注解 2、掌握常见的注解使用方法以及如何自定义注解 3、了解注解实体映射与如何解析注解实体 4、学会如何利用注解解决实际编程中的问题

二、实验内容 1、程序验证题

(1)输入如下程序,看编译器报什么警告。

public class Person {

private String name;

public void speak(String message){

List list = new ArrayList();

System.out.println(\\

}

}

(2)输入如下程序,看程序结果是什么? class A { private String id; A(String id){ this.id = id; } @Deprecated public void execute(){ System.out.println(id); } public static void main(String[] args) { A a = new A(\

a.execute();

}

}

2、程序填空题

(1)要定义一个作用在属性上、信息保留到程序运行时的注解ID。请按要求填完程序。

——————————

——————————

public @interface ID { public String value() default \} 13

(2)如下程序,要为注解ID赋一个值“personID”,请根据题目要求填完程序。 @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface ID { public String value(); public String description() default \ }

public class Person {

————————

}

三、根据下面要求编写程序

(1)创建Person类,Person的属性有:

Strng name 姓名 String sex 性别 Integer age 年龄, String idNo 身份证号

Boolean isMerried 是否已婚

请生成相应的getter、setter方法。请编写注解@Label,表示所注解对象的中文名称,请把@Label注解标注在Person类和Person的每个属性上面。请编写PersonInput类,负责提示录入人员的相关属性,提示必须是注解@Label所标注的中文名称。请编写PersonDisplay,负责显示人员信息,显示时的属性名称必须为注解@Label所标注的中文名称,PersonInput类与PersonDisplay类实现了共同的接口PersonAction,接口PersonAction有方法process,方法process的签名为:public Person process(Person person);

(2) 在第一题目的基础上,编写注解@Column,属性有Label 表示类的属性的显示名称,

Nullable 表示是否允许属性值为空,MaxLength 表示文本属性的最大长度,MinLength表示文本属性的最小长度,MaxValue表示最大值,MinValue表示最小值,把注解@Column加在P

>>展开全文<<
12@gma联系客服:779662525#qq.com(#替换为@)