面向对象编程的基本特点:封装、继承和多态。因此下面的例子就用到了接口、抽象类和子类等。
Animal接口:
package com.taobao.demo;
public interface Animal {
void eat();
void say();
}
AbstractAnimal抽象类:
package com.taobao.demo;
public abstract class AbstractAnimal implements Animal {
public String name;
private Gender gender;
public AbstractAnimal() {}
public AbstractAnimal(String name, Gender gender) {
this.name = name;
this.gender = gender;
}
@Override
public void eat() {
System.out.println("eat...");
}
@Override
public void say() {
System.out.println("say...");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
public enum Gender {
MALE, FEMALE;
}
}
Cat类:
package com.taobao.demo;
public class Cat extends AbstractAnimal implements Animal {
//给猫添加一个私有属性
private Color eyeColor;
public Cat() {}
public Cat(String name, Gender gender, Color eyeColor) {
super(name, gender);
this.eyeColor = eyeColor;
}
@Override
public void eat() {
System.out.println("I like to eat fish.");
}
@Override
public void say() {
System.out.println("miao...miao...miao...");
}
public Color getEyeColor() {
return eyeColor;
}
public void setEyeColor(Color eyeColor) {
this.eyeColor = eyeColor;
}
public enum Color {
WHITE,BLUE,YELLOW,BLACK;
}
}
Dog类:
package com.taobao.demo;
public class Dog extends AbstractAnimal implements Animal {
//给狗添加一个私有属性
private int age;
public Dog() {}
public Dog(String name, Gender gender, int age) {
super(name, gender);
this.age = age;
}
@Override
public void eat() {
System.out.println("I like to eat bone.");
}
@Override
public void say() {
System.out.println("wang...wang...wang...");
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
}
上面这些类都是基础类,为了做实验用的基本类。也是为了实践而专门写的无意义的类。
AnimalFactory类:
package com.taobao.demo.factory;
import com.taobao.demo.Animal;
public class AnimalFactory {
public static Animal getInstance(String className)
throws InstantiationException, IllegalAccessException, ClassNotFoundException {
return (Animal) Class.forName(className).newInstance();
}
}
测试反射实践:
package com.taobao.demo.reflect;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import com.taobao.demo.AbstractAnimal;
import com.taobao.demo.Animal;
import com.taobao.demo.Cat;
import com.taobao.demo.factory.AnimalFactory;
public class ReflectPractice {
public static void main(String[] args) throws Exception {
//所有类的对象都是Class的实例
Class<?> clazz1 = Class.forName("com.taobao.demo.Cat");
Class<?> clazz2 = new Cat().getClass();
Class<?> clazz3 = Cat.class;
//判断以上三个是不是同一个类
System.out.println(clazz1.equals(clazz2) && clazz1.equals(clazz3));
//输出类名
System.out.println(clazz1.getName());
//通过多种方式获得一个类的实例对象
Cat cat0 = (Cat) clazz1.newInstance();
Cat cat1 = new Cat();
//通过class调用Cat类的构造方法,默认获得的构造方法的顺序比较奇怪
Cat cat2 = (Cat) clazz1.getConstructors()[1].newInstance("miaomiao", AbstractAnimal.Gender.MALE, Cat.Color.YELLOW);
//通过指定参数类型来获取对应的构造器
Constructor<?> csr = clazz1.getConstructor(String.class, AbstractAnimal.Gender.class, Cat.Color.class);
//打印该构造器的方法定义,返回值、参数类型等
System.out.println(csr.toGenericString());
//通过该构造器构造出实例对象
Cat cat3 = (Cat) csr.newInstance("miaomiao", AbstractAnimal.Gender.MALE, Cat.Color.YELLOW);
//获取父类和接口信息
Class<?> superClass = clazz2.getSuperclass();
System.out.println("父类名为:"+superClass.getName());
Class<?> clazz4 = Class.forName(clazz2.getSuperclass().getName());
System.out.println(clazz4.getName());
Class<?>[] interfaces = clazz3.getInterfaces();
for(Class<?> interf: interfaces) {
System.out.println(interf.getName());
}
//获取成员属性
Field[] fields = clazz3.getDeclaredFields();
for(Field field : fields) {
System.out.println(field.getName());
}
//获取父类的属性,private和protected类型的获取不到
Field[] fields2 = clazz3.getFields();
System.out.println("父类属性"+fields2.length);
for(Field field : fields2) {
System.out.println(field);
}
//获取成员方法
Method[] meths = clazz3.getMethods();
for(Method method : meths) {
System.out.println(method);
}
//调用其它类的方法
Method method = clazz1.getMethod("say");
Cat catt = (Cat) clazz1.newInstance();
method.invoke(catt);
method = clazz1.getMethod("setEyeColor", Cat.Color.class);
method.invoke(catt, Cat.Color.YELLOW);
System.out.println(catt.getEyeColor());
//通过反射操作属性
Field field = clazz1.getField("name");
field.set(cat0, "xiaomao");
System.out.println(field.get(cat0));
//测试工厂方法
Animal animal = AnimalFactory.getInstance("com.taobao.demo.Cat");
animal.getClass().newInstance().say();
}
}