Skip to content

Commit 2cde907

Browse files
author
xiachaochao
committed
123
1 parent c67a684 commit 2cde907

18 files changed

Lines changed: 354 additions & 3 deletions

File tree

JavaBase/.classpath

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<classpath>
33
<classpathentry kind="src" path="src"/>
44
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
5-
<classpathentry kind="lib" path="D:/eclipse/log4j-1.2.17.jar"/>
6-
<classpathentry kind="lib" path="D:/eclipse/lombok.jar"/>
5+
<classpathentry kind="lib" path="D:/Program Files/eclipse/log4j-1.2.17.jar"/>
6+
<classpathentry kind="lib" path="D:/Program Files/eclipse/lombok.jar"/>
77
<classpathentry kind="output" path="bin"/>
88
</classpath>

JavaBase/src/com/_520it/reflect/GetConstructorDemo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static void main(String[] args) throws Exception {
2727
}
2828

2929
public static void getOne() throws Exception {
30-
Class<?> clazz=User.class;//第一种方式
30+
Class<?> clazz=Person.class;//第一种方式
3131
//Class<?> clazz1=Class.forName("com._520it.reflect.User");//第二种方式
3232
//Class<?> clazz2=(new User().getClass());//第三种方式
3333
System.out.println(clazz);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com._520it.reflect;
2+
3+
class Person {
4+
public Person() {
5+
System.out.println("无参构造器");
6+
}
7+
8+
public Person(String name) {
9+
System.out.println("含有name的构造器");
10+
}
11+
12+
private Person(String name, int age) {
13+
System.out.println("有参构造器" + name + age);
14+
}
15+
16+
public void doWork() {
17+
System.out.println("doWork方法");
18+
}
19+
}
20+
21+
public class RefectDemo01 {
22+
public static void main(String[] args) throws Exception {
23+
// 三种方式获取clazz对象
24+
//Class<?> c1 = Class.forName("com._520it.reflect.Person");
25+
//Class<?> c2=Person.class;
26+
Class<?> c3=(new Person().getClass());
27+
28+
}
29+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.ddb.javacore.annotation;
2+
3+
import java.lang.reflect.Field;
4+
5+
public class AnnotationProcessor {
6+
7+
@SuppressWarnings("deprecation")
8+
public static void main(String[] args) throws Exception {
9+
// 得到Class实例
10+
Class<?> clazz = Person.class;
11+
12+
// 得到被反射类的一个实例
13+
Person person = (Person) clazz.newInstance();
14+
System.out.println("Person 默认值:");
15+
person.showInfo();
16+
17+
// 获得所有的字段
18+
Field[] fields = clazz.getDeclaredFields(); // 不能获取到继承的属性
19+
// Field[] fields = clazz.getFields(); //可以获取到公开的继承的属性
20+
/*
21+
Field[] fields3 =new Field[fields.length+fields2.length];
22+
System.arraycopy(fields, 0, fields3, 0, fields.length);
23+
System.arraycopy(fields2, 0, fields3,fields.length, fields2.length);
24+
如果是需要获取抽象类中的属性,则需要通过获得一个新的数组,将所有的属性值放置在同一个数组中,然后继续遍历
25+
*/
26+
for (Field field : fields) {
27+
// 判断该属性上是否存在HelloWorld注解
28+
if (field.isAnnotationPresent(HelloWorld.class)) {
29+
// 获取指定的注解
30+
HelloWorld annotation = field.getAnnotation(HelloWorld.class);
31+
System.out.println("HelloWorld注解的值是:" + annotation.value());
32+
33+
// 打破封装
34+
field.setAccessible(true);
35+
// 将注解的值赋给属性值
36+
field.set(person, annotation.value());
37+
38+
} /*else if (field.isAnnotationPresent(Default.class)) {
39+
Default default1 = field.getAnnotation(Default.class);
40+
System.out.println("Default注解的值是:" + default1.value());
41+
field.setAccessible(true); // 获取字段的数据类型
42+
System.out.println(field.getType().getSimpleName().toString());
43+
44+
switch (field.getType().getSimpleName().toString()) {
45+
case "int":
46+
field.set(person, Integer.valueOf(default1.value()));
47+
break;
48+
case "double":
49+
field.set(person, Double.valueOf(default1.value()));
50+
break;
51+
case "String":
52+
field.set(person, default1.value());
53+
break;
54+
default:
55+
break;
56+
}
57+
}*/
58+
}
59+
60+
System.out.println("注解后的值:");
61+
person.showInfo();
62+
63+
}
64+
65+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.ddb.javacore.annotation;
2+
3+
import static java.lang.annotation.ElementType.FIELD;
4+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
5+
6+
import java.lang.annotation.Retention;
7+
import java.lang.annotation.Target;
8+
9+
@Retention(RUNTIME)
10+
@Target(FIELD)
11+
public @interface Default {
12+
public String value() default "";
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.ddb.javacore.annotation;
2+
3+
import static java.lang.annotation.ElementType.FIELD;
4+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
5+
6+
import java.lang.annotation.Retention;
7+
import java.lang.annotation.Target;
8+
9+
@Retention(RUNTIME)
10+
@Target(FIELD)
11+
//@Inherited
12+
public @interface HelloWorld {
13+
public String value() default "ZhangSan";
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.ddb.javacore.annotation;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
6+
@Getter
7+
@Setter
8+
public abstract class Human {
9+
private String name;
10+
private int age;
11+
12+
public String toString() {
13+
return "Human [name=" + name + ", age=" + age + "]";
14+
}
15+
16+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.ddb.javacore.annotation;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
6+
@Getter
7+
@Setter
8+
public class Person extends Human {
9+
@Default("12.5")
10+
private double score = 36.0;
11+
@HelloWorld("Beijing")
12+
private String address = "HeFei";
13+
14+
@Deprecated
15+
public void showInfo() {
16+
System.out.println("Person [score=" + score + ",address=" + address + ", toString()=" + super.toString()
17+
+ ", getName()=" + getName() + ", getAge()=" + getAge() + "]");
18+
}
19+
20+
@Override
21+
public String toString() {
22+
return "Person [score=" + score + ", toString()=" + super.toString() + ", getName()=" + getName()
23+
+ ", getAge()=" + getAge() + ", getClass()=" + getClass() + ", hashCode()=" + hashCode() + "]";
24+
}
25+
26+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.ddb.javacore.annotation;
2+
3+
public class TestDemo {
4+
5+
@SuppressWarnings("deprecation")
6+
public static void main(String[] args) {
7+
Person person = new Person();
8+
person.setName("Lisi");
9+
person.setAge(26);
10+
person.setScore(60);
11+
12+
person.showInfo();
13+
14+
}
15+
16+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.xinan.annotation01;
2+
3+
public class Fruit {
4+
5+
public void displayName(){
6+
System.out.println("水果的名字是:*****");
7+
}
8+
}
9+
10+
class Orange extends Fruit {
11+
@Override
12+
public void displayName(){
13+
System.out.println("水果的名字是:桔子");
14+
}
15+
}
16+
17+
class Apple extends Fruit {
18+
@Override
19+
public void displayName(){
20+
System.out.println("水果的名字是:苹果");
21+
}
22+
}

0 commit comments

Comments
 (0)