给下面代码添加注释public class Demo1 { public static void main(String[] args) { TicketThread1 ticketThread=new TicketThread1(); Thread t1=new Thread(ticketThread,"窗口1"); Thread t2=new Thread(ticketThread,"窗口2"); Thread t3=new Thread(ticketThread,"窗口3"); // t2.setPriority(10); t3.setPriority(1); t1.start(); t2.start(); t3.start(); } }
时间: 2024-04-26 08:22:57 浏览: 43
// 定义一个名为Demo1的公共类
public class Demo1 {
// 程序入口main方法
public static void main(String[] args) {
// 创建一个TicketThread1对象,该对象实现了Runnable接口
TicketThread1 ticketThread=new TicketThread1();
// 创建三个线程对象t1、t2、t3,它们都使用同一个TicketThread1对象
Thread t1=new Thread(ticketThread,"窗口1");
Thread t2=new Thread(ticketThread,"窗口2");
Thread t3=new Thread(ticketThread,"窗口3");
// 设置线程t2的优先级为最高,即10,t3的优先级为最低,即1
t2.setPriority(10);
t3.setPriority(1);
// 启动三个线程
t1.start();
t2.start();
t3.start();
}
}
相关问题
添加注释 public class Demo2 { public static void main(String[] args) { File f1 = new File("/Users/nxy/Desktop/笔记/File"); List<String> list = getFile(new ArrayList<>(),f1); for (String s:list ) { System.out.println(s); } } public static List<String> getFile(List<String> list, File file) { if (file.isFile()) { System.out.println(file.getName()); } else { File[] files = file.listFiles(); for (File f : files ) { getFile(list, f); } } return list; } }
/**
* Demo2类
*/
public class Demo2 {
/**
* 主函数
* @param args 参数列表
*/
public static void main(String[] args) {
// 创建File对象f1表示指定目录
File f1 = new File("/Users/nxy/Desktop/笔记/File");
// 调用getFile方法获取该目录下所有文件的路径
List<String> list = getFile(new ArrayList<>(), f1);
// 遍历输出所有文件的路径
for (String s : list) {
System.out.println(s);
}
}
/**
* 获取指定目录下所有文件的路径
* @param list 存放文件路径的List集合
* @param file 指定目录
* @return 返回所有文件的路径
*/
public static List<String> getFile(List<String> list, File file) {
// 如果file是一个文件,则输出文件名
if (file.isFile()) {
System.out.println(file.getName());
} else {
// 如果file是一个目录,则获取该目录下所有文件
File[] files = file.listFiles();
for (File f : files) {
// 递归调用getFile方法,获取f目录下所有文件的路径
getFile(list, f);
}
}
return list;
}
}
给下列代码添加详细注释public class Demo1 { public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException { List<Class> arrayLists = new ArrayList<>(Arrays.asList(Class.forName("com.zb.pro0531.anno.Cat"), Class.forName("com.zb.pro0531.anno.Dog"))); for (Class clazz:arrayLists ) { // Annotation annot = clazz.getAnnotation(clazz); if(clazz.getAnnotation(Service.class)!=null){ System.out.println(clazz.getName()); Method[] methods = clazz.getDeclaredMethods(); for (Method method : methods) { Annotation autoAnnotation = method.getAnnotation(Auto.class); if (autoAnnotation != null) { method.invoke(clazz.newInstance()); } } } } } }
/**
* 这是一个演示如何使用注解的例子
*/
public class Demo1 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
// 创建一个包含 Cat 和 Dog 类的 Class 列表
List<Class> arrayLists = new ArrayList<>(Arrays.asList(Class.forName("com.zb.pro0531.anno.Cat"), Class.forName("com.zb.pro0531.anno.Dog")));
// 遍历 Class 列表中的每一个类
for (Class clazz : arrayLists) {
// 如果这个类上有 Service 注解
if (clazz.getAnnotation(Service.class) != null) {
// 打印这个类的名称
System.out.println(clazz.getName());
// 获取这个类中所有的方法
Method[] methods = clazz.getDeclaredMethods();
// 遍历这个类中的每一个方法
for (Method method : methods) {
// 如果这个方法上有 Auto 注解
Annotation autoAnnotation = method.getAnnotation(Auto.class);
if (autoAnnotation != null) {
// 实例化这个类并调用这个方法
method.invoke(clazz.newInstance());
}
}
}
}
}
}
注释中解释了代码的主要功能和各个部分的作用。注解是 Java 语言中一种重要的元编程机制,通过为类、方法、字段等元素添加注解,可以为程序提供更多的语义信息,从而实现更灵活的编程。本例中使用了两个自定义注解,分别是 Service 和 Auto,用来标记某个类是否是服务类以及某个方法是否需要自动注入依赖。
阅读全文
相关推荐
















