Documentation

The Java™ Tutorials
Hide TOC
Questions and Exercises问题和练习
Trail: Learning the Java Language
Lesson: Annotations

Questions and Exercises: Annotations问题和练习:批注

Questions问题

  1. What is wrong with the following interface?以下界面有什么问题?

    public interface House {
        @Deprecated
        void open();
        void openFrontDoor();
        void openBackDoor();
    }
  2. Consider this implementation of the House interface, shown in Question 1.考虑到在1中所示的House接口的实现。

    public class MyHouse implements House {
        public void open() {}
        public void openFrontDoor() {}
        public void openBackDoor() {}
    }

    If you compile this program, the compiler produces a warning because open was deprecated (in the interface).如果编译此程序,编译器将生成警告,因为open已弃用(在接口中)。What can you do to get rid of that warning?你能做些什么来摆脱这个警告?

  3. Will the following code compile without error? Why or why not?下面的代码编译时不会出错吗?为什么?

    public @interface Meal { ... }
    
    @Meal("breakfast", mainDish="cereal")
    @Meal("lunch", mainDish="pizza")
    @Meal("dinner", mainDish="salad")
    public void evaluateDiet() { ... }

Exercises练习

  1. Define an annotation type for an enhancement request with elements id, synopsis, engineer, and date.使用元素idsynopsisengineerdate为增强请求定义批注类型。Specify the default value as unassigned for engineer and unknown for date.将默认值指定为工程师unknown和日期unknown

Check your answers.检查你的答案。


Previous page: Repeating Annotations
Next page: Interfaces and Inheritance