Discuss / Java / 作业打卡

作业打卡

Topic source
public class Main {

	public static void main(String[] args) throws Exception {
		String name = "Xiao Ming";
		int age = 20;
		Person p = new Person();
		// TODO: 利用反射给name和age字段赋值:
		String s[] = { "name", "age" };

		Class c = p.getClass();
		Field[] fs = c.getDeclaredFields();
		for (Field f : fs) {
			f.setAccessible(true);
			if (f.getName() == "name") {
				f.set(p, name);
			} else if (f.getName() == "age") {
				f.set(p, age);
			}
			System.out.println(f.getName());

		}

		System.out.println(p.getName()); // "Xiao Ming"
		System.out.println(p.getAge()); // 20
	}
}

🌙

#2 Created at ... [Delete] [Delete and Lock User]

可以可以,适合干这一行

🌙

#3 Created at ... [Delete] [Delete and Lock User]

在次看,好像也没啥秀的,就是,api记得熟,这里感觉没必要用循环,+ 判断0


  • 1

Reply