JAVA使用反射调用类的属性方法以及构造器-----JAVA入门基础教程

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

public class ReflectionTest
{
    public static void main(String[] args) throws Exception
    {
        Class c = Class.forName("Person");
        Person person = (Person)c.newInstance();
        Field a = c.getField("age");
        a.set(person,2);
        System.out.println(a.get(person));
        a = c.getDeclaredField("name");
        a.setAccessible(true);
        a.set(person,"Tomcat");
        System.out.println(a.get(person));
        Field info = c.getDeclaredField("info");
        info.setAccessible(true);
        System.out.println(info.get(Person.class));
        info.set(Person.class,"Person");
        System.out.println(info.get(Person.class));
        info.set(null,"Person");
        System.out.println(info.get(null));
        Method method = c.getDeclaredMethod("showNation",String.class,int.class);
        method.setAccessible(true);
        //调用方法如果返回值类型为VOID则返回为null
        System.out.println(method.invoke(person, "china", 1));
        Method m = c.getDeclaredMethod("showInfo");
        m.setAccessible(true);
        m.invoke(null);
        c = Person.class;
        Constructor constructor = c.getDeclaredConstructor(String.class,int.class);
        constructor.setAccessible(true);
        Person p = (Person)constructor.newInstance("TOMCAT",12);
        System.out.println(p);
        constructor = c.getConstructor();
        constructor.setAccessible(true);
        p = (Person)constructor.newInstance();
        System.out.println(p);
    }
}

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

public class ReflectionTest
{
public static void main(String[] args) throws Exception
{
Class c = Class.forName("Person");
Person person = (Person)c.newInstance();
Field a = c.getField("age");
a.set(person,2);
System.out.println(a.get(person));
a = c.getDeclaredField("name");
a.setAccessible(true);
a.set(person,"Tomcat");
System.out.println(a.get(person));
Field info = c.getDeclaredField("info");
info.setAccessible(true);
System.out.println(info.get(Person.class));
info.set(Person.class,"Person");
System.out.println(info.get(Person.class));
info.set(null,"Person");
System.out.println(info.get(null));
Method method = c.getDeclaredMethod("showNation",String.class,int.class);
method.setAccessible(true);
//调用方法如果返回值类型为VOID则返回为null
System.out.println(method.invoke(person, "china", 1));
Method m = c.getDeclaredMethod("showInfo");
m.setAccessible(true);
m.invoke(null);
c = Person.class;
Constructor constructor = c.getDeclaredConstructor(String.class,int.class);
constructor.setAccessible(true);
Person p = (Person)constructor.newInstance("TOMCAT",12);
System.out.println(p);
constructor = c.getConstructor();
constructor.setAccessible(true);
p = (Person)constructor.newInstance();
System.out.println(p);
}
}

public class Creature
{
    protected boolean gender;
    public int id;
    public void breath()
    {
        System.out.println("Breath");
    }
    private void info()
    {
        System.out.println("info");
    }
}

 

public class Creature
{
protected boolean gender;
public int id;
public void breath()
{
System.out.println("Breath");
}
private void info()
{
System.out.println("info");
}
}

public interface MyInterface
{
    void method();
}

public interface MyInterface
{
void method();
}

public class Person extends Creature implements Comparable,MyInterface
{
    private String name;
    public int age = 1;
    private static String info;
    public Person()
    {
        System.out.println("Person");
    }
    protected Person(int age)
    {
        this.age = age;
    }
    private Person(String name,int age)
    {
        this.age = age;
        this.name = name;
    }
    public void show() throws RuntimeException,ClassNotFoundException
    {
        System.out.println("是个人");
    }
    private String showNation(String nation,int age)
    {
        System.out.println("nation");
        return nation + age;
    }
    @Override
    public String toString()
    {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
    public int compareTo(Person person)
    {
        return 0;
    }
    public static void showInfo()
    {
        System.out.println("人");
    }

    @Override
    public void method() {

    }
}

public class Person extends Creature implements Comparable,MyInterface
{
private String name;
public int age = 1;
private static String info;
public Person()
{
System.out.println("Person");
}
protected Person(int age)
{
this.age = age;
}
private Person(String name,int age)
{
this.age = age;
this.name = name;
}
public void show() throws RuntimeException,ClassNotFoundException
{
System.out.println("是个人");
}
private String showNation(String nation,int age)
{
System.out.println("nation");
return nation + age;
}
@Override
public String toString()
{
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public int compareTo(Person person)
{
return 0;
}
public static void showInfo()
{
System.out.println("人");
}

@Override
public void method() {

}
}

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