JAVA使用反射机制实现动态性-----JAVA入门基础教程

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Date;

public class ReflectTest
{
    public static void main(String[] args)
    {
        Person person = getInstance("Person");
        System.out.println(person);
        Date date = getInstance("java.util.Date");
        System.out.println(date);
        String className = "Person";
        String methodName = "show";
        Object returnObj = invoke(className,methodName);
        System.out.println(returnObj);
    }
    public static Person getInstance()
    {
        return new Person();
    }
    public static T getInstance(String classname)
    {
        try
        {
            Class c = Class.forName(classname);
            Constructor constructor = c.getDeclaredConstructor();
            constructor.setAccessible(true);
            T t = (T)constructor.newInstance();
            return t;
        }
        catch (Exception e)
        {
            throw new RuntimeException(e);
        }
    }
    public static Object invoke(String className, String methodName)
    {
        try
        {
            Object obj = getInstance(className);
            Class c = Class.forName(className);
            Method method = c.getDeclaredMethod(methodName);
            method.setAccessible(true);
            return method.invoke(obj);
        }
        catch (Exception e)
        {
            throw new RuntimeException(e);
        }
    }
}

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Date;

public class ReflectTest
{
public static void main(String[] args)
{
Person person = getInstance("Person");
System.out.println(person);
Date date = getInstance("java.util.Date");
System.out.println(date);
String className = "Person";
String methodName = "show";
Object returnObj = invoke(className,methodName);
System.out.println(returnObj);
}
public static Person getInstance()
{
return new Person();
}
public static T getInstance(String classname)
{
try
{
Class c = Class.forName(classname);
Constructor constructor = c.getDeclaredConstructor();
constructor.setAccessible(true);
T t = (T)constructor.newInstance();
return t;
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
public static Object invoke(String className, String methodName)
{
try
{
Object obj = getInstance(className);
Class c = Class.forName(className);
Method method = c.getDeclaredMethod(methodName);
method.setAccessible(true);
return method.invoke(obj);
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
}

public class Person
{
    private String name;
    public int age;
    public Person()
    {
        System.out.println("Person()...");
    }
    public Person(int age)
    {
        this.age = age;
    }
    public Person(String name, int age)
    {
        this.name = name;
        this.age = age;
    }
    public void show()
    {
        System.out.println("你好");
    }
    private String showNation(String nation)
    {
        return nation;
    }
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

public class Person
{
private String name;
public int age;
public Person()
{
System.out.println("Person()...");
}
public Person(int age)
{
this.age = age;
}
public Person(String name, int age)
{
this.name = name;
this.age = age;
}
public void show()
{
System.out.println("你好");
}
private String showNation(String nation)
{
return nation;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}

你可能感兴趣的:(JAVA随手写,java,开发语言,jvm,算法,intellij-idea)