你好,请写一个Demo类,输出添加了注释Service的类的类名,目前有一个Cat类添加了注释Service,Dog类没有添加注释Service
时间: 2023-08-16 08:06:36 浏览: 37
好的,这是一个简单的Demo类,可以输出添加了注释Service的类的类名:
```java
public class Demo {
public static void main(String[] args) {
List<Class<?>> classes = Arrays.asList(Cat.class, Dog.class);
for (Class<?> clazz : classes) {
if (clazz.getAnnotation(Service.class) != null) {
System.out.println(clazz.getSimpleName() + " has the Service annotation!");
} else {
System.out.println(clazz.getSimpleName() + " does not have the Service annotation!");
}
}
}
}
@Service
class Cat {
// ...
}
class Dog {
// ...
}
```
在这个Demo类中,我们首先定义了一个包含了Cat和Dog两个类的列表。然后,我们遍历这个列表中的每一个类,判断它是否添加了@Service注释。如果添加了@Service注释,就输出类名和“has the Service annotation!”;如果没有添加@Service注释,就输出类名和“does not have the Service annotation!”。
在这个例子中,Cat类添加了@Service注释,所以输出为“Cat has the Service annotation!”;而Dog类没有添加@Service注释,所以输出为“Dog does not have the Service annotation!”。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)