Class:类的类(说明书的说明书,规定说明书该怎么写。)
注解结合反射小项目1:
Test程序入口类:
public class Test {
public static void main(String[] args) throws Exception{
Girl girl = new Girl();
Properties properties = new Properties();
properties.load(Test.class.getClassLoader().getResourceAsStream("conf/pet.properties"));
Animal pet = (Animal)Class.forName(properties.getProperty("animal")).newInstance();
GirlLike annotation = pet.getClass().getAnnotation(GirlLike.class);
if(annotation!=null){
girl.feed(pet);
}else{
System.out.println("she fucked up");
}
}
}
Animal接口及其实现类:
public interface Animal {
void eat();
}
public class Cat implements Animal {
@Override
public void eat() {
}
}
类上有注解
@GirlLike //自定义注解
public class Dog implements Animal{
private String name;
@Override
public void eat() {
}
}
它类Girl:
public class Girl {
public void feed(Animal animal){
System.out.println("i have fed this little ass");
}
}
自定义注解GirlLike:
@Target(ElementType.TYPE) //定义你的注解作用在类还是方法还是......
@Retention(RetentionPolicy.RUNTIME) //定义你的注解何时生效
public @interface GirlLike {
}
配置文件:
animal=com.itnls.animal.Dog
注解小项目:
注解A和Name:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface A {
}
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Name {
int length() default 3;
String lastName() default "w";
}
主类Animal:
public class Animal {
@Name(length = 5,lastName = "k")
private String Name;
private int age;
public void eat(){
System.out.println("chi");
}
public void eat(String food){
System.out.println(food);
}
public String eat(String food,int age){
return food+age;
}
public static void main(String[] args) {
//1.使用类获取类对象
Class clazz = Animal.class;
System.out.println(clazz);
//2 使用全类名获取
try {
Class clazz1= Class.forName("com.itnls.Animal");
System.out.println(clazz1);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//3 使用对象获取
Animal animal = new Animal();
Class clazz2 =animal.getClass();
System.out.println(clazz2);
}
@Test
public void optField(){
Animal animal = new Animal();
Class<Animal> animalClass = Animal.class;
//普通fields只能拿到public类型的字段
Field[] fields = animalClass.getFields();
for (Field field : fields) {
System.out.println(field.getName());
try {
field.set(animal,"wdnmd");
System.out.println(field.get(animal));
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
System.out.println("-----------------");
//declareFields拿到所有字段
Field[] declaredFields = animalClass.getDeclaredFields();
for (Field declaredField : declaredFields) {
System.out.println(declaredField.getName());
}
}
@Test
public void optMethod(){
Animal animal = new Animal();
Class<Animal> animalClass = Animal.class;
try {
Method eat = animalClass.getMethod("eat");
try {
eat.invoke(animal);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
try {
Method eat = animalClass.getMethod("eat",String.class);
try {
eat.invoke(animal,"hambuger");
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
try {
Method eat = animalClass.getMethod("eat",String.class,int.class);
try {
Object food = eat.invoke(animal, "hambuger", 12);
System.out.println(food);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
@Test
public void optConstructor() throws Exception{
Class aClass = Class.forName("com.itnls.Animal");
Animal animal = (Animal) aClass.newInstance();
// Constructor<Animal> constructor = aClass.getConstructor();
// Animal animal = constructor.newInstance();
// animal.eat();
}
@Test
public void optAnnotation() throws Exception{
// Class<Animal> animalClass = Animal.class;
// Annotation annotation = animalClass.getAnnotation(A.class);
// Field declaredField = animalClass.getDeclaredField("name");
// declaredField.setAccessible(true);
// Name nameAnnotation=declaredField.getAnnotation(Name.class);
// nameAnnotation.
}
}
## 外类other:
public class OtherClass {
// 字段操作
@Test
public void optField(){
Animal animal = new Animal();
Class<Animal> animalClass = Animal.class;
//普通fields只能拿到public类型的字段
Field[] fields = animalClass.getFields();
for (Field field : fields) {
System.out.println(field.getName());
try {
field.set(animal,"wdnmd");
System.out.println(field.get(animal));
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
System.out.println("-----------------");
//declareFields拿到所有字段
try {
Field declaredField = animalClass.getDeclaredField("age");
declaredField.setAccessible(true);
try {
//★★★
declaredField.set(animal,12);
System.out.println(declaredField.get(animal));
} catch (IllegalAccessException e) {
e.printStackTrace();
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
}