问题与练习: 注解
问题
问题: 线面的接口有哪些错误:
123456public interface House {public void open();public void openFrontDoor();public void openBackDoor();}
问题: 考虑下面的
House
类,实现了问题1中的借口.12345public class MyHouse implements House {public void open() {}public void openFrontDoor() {}public void openBackDoor() {}}如果你编译这个程序,编译器会产生警告,因为
open
方法是反对的(在接口中),你应该做什么来避免警告?下面的代码在编译时是否会发生错误?为什么?
123456public Meal { ... }"breakfast", mainDish="cereal")("lunch", mainDish="pizza")("dinner", mainDish="salad")(public void evaluateDiet() { ... }
练习
- 练习: 定义一个增强请求的注解类型,包含元素
id
,synopsis
,engineer
, 以及date
. 设定engineer默认值为unassigned
,date默认值为unknown
.
问题与练习答案: 注解
问题答案
问题: 线面的接口有哪些错误:
123456public interface House {public void open();public void openFrontDoor();public void openBackDoor();}答案 文档应该反映出
open
为什么是反对的以及用什么来代替,例如:123456789101112public interface House {/*** @deprecated use of open* is discouraged, use* openFrontDoor or* openBackDoor instead.*/public void open();public void openFrontDoor();public void openBackDoor();}问题: 考虑下面的
House
类,实现了问题1中的借口.12345public class MyHouse implements House {public void open() {}public void openFrontDoor() {}public void openBackDoor() {}}如果你编译这个程序,编译器会产生警告,因为
open
方法是反对的(在接口中),你应该做什么来避免警告?答案: 你可以为
open
方法的实现添加deprecate注解:12345678public class MyHouse implements House {// The documentation is// inherited from the interface.public void open() {}public void openFrontDoor() {}public void openBackDoor() {}}同样,你也可以通过
@SuppressWarings
注解镇压这个警告:123456public class MyHouse implements House {"deprecation")(public void open() {}public void openFrontDoor() {}public void openBackDoor() {}}下面的代码在编译时是否会发生错误?为什么?
123456public Meal { ... }"breakfast", mainDish="cereal")("lunch", mainDish="pizza")("dinner", mainDish="salad")(public void evaluateDiet() { ... }答案:代码将会编译失败。JDK8之前,重复注解是不支持的。JDK8中,同样会编译失败,因为
Meal
注解类型并没有声明为可重复的。可以通过添加@Repeatable
元注解,并定义一个注解容器类型来修复这个问题:123456.lang.annotation.Repeatable(MealContainer.class)public Meal { ... }public MealContainer {Meal[] value();}
练习答案
练习: 定义一个增强请求的注解类型,包含元素
id
,synopsis
,engineer
, 以及date
. 设定engineer默认值为unassigned
,date默认值为unknown
.答案:
123456789/*** Describes the Request-for-Enhancement (RFE) annotation type.*/public RequestForEnhancement {int id();String synopsis();String engineer() default "[unassigned]";String date() default "[unknown]";}