javaSE(23)(反射)

Person类:
package zz.itheima.reflect;

public class Person {
    private String name;
    private int age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Person(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    public Person() {
        super();
        // TODO Auto-generated constructor stub
    }
    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + "]";
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + age;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Person other = (Person) obj;
        if (age != other.age)
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }
    public void eat(){
        System.out.println("吃饭!");
    }
}
TestClass类:
package zz.itheima.reflect;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class TestClass {

    public static void main(String[] args)throws Exception {
        //1.第一种方式
        /*Person p = new Person(); Class c = p.getClass();*/
        //2.第二种方式
        Class c = Person.class;
        //3.第三种方式:不通过new来创建对象
        Class p = Class.forName("zz.itheima.reflect.Person");//得到类型
        Object Person = p.newInstance();//得到对象
        Method eat  = p.getMethod("eat", null);//得到方法
        eat.invoke(Person, null);//调用方法



        //得到类名
        System.out.println(c.getName());
        //得到所有的属性名
        Field[] f = c.getDeclaredFields();
        for (int i = 0; i < f.length; i++) {
            System.out.println(f[i].getName());
        }
        //得到所有的方法名
        Method[] m  = c.getDeclaredMethods();
        for (int i = 0; i < m.length; i++) {
            System.out.println(m[i].getName());
        }

    }

}
运行结果:
吃饭!
zz.itheima.reflect.Person
name
age
equals
toString
hashCode
getName
setName
eat
setAge
getAge
ZhiFu接口:
package zz.itheima.reflect;
//支付接口
public interface ZhiFu {
    public void zhifu();//支付
}
 Bao实现类:
 package zz.itheima.reflect;

public class Bao implements ZhiFu{

    @Override
    public void zhifu() {
        System.out.println("在使用支付宝充话费");
    }

}
WangYin实现类:
package zz.itheima.reflect;

public class WangYin implements ZhiFu{

    @Override
    public void zhifu() {
        System.out.println("在使用网银充话费");
    }

}
PingTai类:
package zz.itheima.reflect;

public class PingTai {
    public void chong(ZhiFu zf){//接口作为参数。只要是接口的实现类都可以传递进来
        zf.zhifu();
    }
}
平台测试类:
package zz.itheima.reflect;

import java.io.FileReader;
import java.lang.reflect.Method;
import java.util.Properties;

public class TestPingTai {

    public static void main(String[] args) throws Exception{
        PingTai pt = new PingTai();
        //第一种实现方法:
        System.out.println("第一种实现方法:");
        pt.chong(new Bao());
        pt.chong(new WangYin());
        //第2种实现方法:反射方式实现
        System.out.println("第2种实现方法:");
        Class c = Class.forName("zz.itheima.reflect.Bao");
        Object bao = c.newInstance();
        Method m = c.getMethod("zhifu", null);
        m.invoke(bao, null);
        //第三种:利用配置文件+IO流+反射
        System.out.println("第3种实现方法");
        Properties prop = new Properties();
        FileReader fr = new FileReader("config.properties");
        prop.load(fr);
        String lei = prop.getProperty("lei");
        String mt = prop.getProperty("method");
        Class c1 = Class.forName(lei);
        Object bao1 = c1.newInstance();
        Method m1 = c1.getMethod(mt, null);
        m1.invoke(bao, null);
    }

}
运行结果:
第一种实现方法:
在使用支付宝充话费
在使用网银充话费
第2种实现方法:
在使用支付宝充话费
第3种实现方法
在使用支付宝充话费

你可能感兴趣的:(反射)