无声specialweek

从零开始学Java-Day19

反射

获取私有的成员属性,并赋值

package cn.tedu.reflection;
//本类用于测试反射而准备的物料类
public class Student {
    public String name;
    public int age;
    private static String skill;
    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void learn(){
        System.out.println("学习!");
    }
    public void play(String gameName){
        System.out.println("冲冲冲!!!" + gameName);
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
package cn.tedu.reflection;

import org.junit.Test;

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

public class TestReflection {
    Student student = new Student();
    public static void main(String[] args) throws ClassNotFoundException {
        Class<?> s1 = Class.forName("cn.tedu.reflection.Student");

        Constructor<?>[] cs = s1.getConstructors();
        for (Constructor<?> c : cs) {
            Class[] cp = c.getParameterTypes();
            System.out.print(Arrays.toString(cp) + "\t");
        }
    }

    @Test
    public void getFunction() {
        Class<?> s = Student.class;
        Method[] methods = s.getMethods();
        for (Method method : methods) {
            //System.out.println(method.getName());
            Class<?>[] mp = method.getParameterTypes();
            System.out.print(Arrays.toString(mp) + "\t");
        }
    }

    @Test
    public void getFields(){
        /*只有被public修饰的成员变量才能获取*/
        Class<? extends Student> s = student.getClass();
        Field[] fields = s.getFields();
        for (Field field : fields) {
            System.out.println(field.getName());
            System.out.println(field.getType());
        }
    }
    /*
    * 方式一:通过字节码对象直接调用newInstance(),触发无参构造来创建对象*/
    @Test
    public void getObject() throws Exception {
        Class<? extends Student> s = student.getClass();
        Object o = s.newInstance();//此时相当于获取了无参构造方法
        System.out.println(o);

    /*方式二:字节码对象获取指定的构造方法,再通过这个构造方法进行创建对象*/
        Constructor<?> c = s.getConstructor(String.class, int.class);
        Object o2 = c.newInstance("东海帝王", 3);
        System.out.println(o2);
        /*此处强转了o2的类型,目的是为了使用子类的独有的功能*/
        Student s2 = (Student) o2;
        s2.play("lol");

    }

}

获取私有的方法并调用

package cn.tedu.reflection;
//本类作为测试暴力反射的物料类
public class Person {
    private String name;
    private int age;

    private void kill(String name, int age){
        System.out.println("要杀的目标名字:" + name + ",今年" + 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;
    }
}
package cn.tedu.reflection;

import org.junit.Test;

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

//本类用于测试暴力反射
public class TestReflect2 {
    /*暴力反射获取与设置私有属性值*/
    @Test
    public void getFields() throws Exception {
        Class<?> p = Person.class;
        Field f = p.getDeclaredField("name");
        System.out.println(f.getType().getName());
        System.out.println(f.getType());
        //多态的方式创建对象
        Object o = p.newInstance();
        f.setAccessible(true);
        f.set(o,"东海帝皇");
        System.out.println(f.get(o));

    }
    /*通过暴力反射获取与使用方法*/
    @Test
    public void getFunction() throws Exception {
        Class<?> p = Person.class;
        Method kill = p.getDeclaredMethod("kill", String.class, int.class);
        kill.setAccessible(true);
        Object o = p.newInstance();
        kill.invoke(o,"江户川柯南",18);
    }
}

内部类

  • 成员内部类
package cn.tedu.innerclass;
//本类用于测试内部类
public class TestInner1 {
    public static void main(String[] args) {
        Outer outer = new Outer();
        outer.find();
        Outer.Inner inner = new Outer().new Inner();
        inner.delete();
        inner.n = "明哥";
        System.out.println(inner.n);
    }
}


class Outer{
    String name;
    private int age;

    class Inner{
        String n;
        int age;
        public void delete(){
            System.out.println("Inner xxxx delete()");
            System.out.println(name);
        }
    }
    Inner inner = new Inner();

    /*外部类需要创建内部类对象才能使用内部类的资源*/
    public void find(){
        System.out.println("Outer xxxx find()");
        inner.delete();
    }
}
  • 局部内部类
package cn.tedu.innerclass;
//本类用于测试内部类
public class TestInner2 {
    public static void main(String[] args) {
        Outer2 outer2 = new Outer2();
        outer2.inn();
    }
}


class Outer2{
    String name;
    private int age;

    private class Inner2{
        String n;
        int age;
        public void delete(){
            System.out.println("Inner2 xxxx delete()");
        }
    }
    private Inner2 inner2 = null;

    /*外部类需要创建内部类对象才能使用内部类的资源*/
    public void find(){
        System.out.println("Outer2 xxxx find()");
    }
    public Inner2 inn(){
        inner2 = new Inner2();
        return inner2;
    }
}
  • 静态内部类
package cn.tedu.innerclass;

public class TestInner3 {
    public static void main(String[] args) {
       // Outer3.Inner3 oi3 = new Outer3().new Inner3();
       // oi3.show();
        //匿名对象访问内部类
       // new Outer3().new Inner3().show();
        Outer3.Inner3 oi = new Outer3.Inner3();
        oi.show();
        Outer3.Inner3.show2();

    }
}

class Outer3{

    static class Inner3{
        public void show(){
            System.out.println("圆环之理");
        }
        static public void show2(){
            System.out.println("圆环之理");
        }
    }
}
  • 匿名内部类
package cn.tedu.innerclass;

public class TestInner4 {
    public static void main(String[] args) {
        Outer4 oi = new Outer4();
        oi.show();
    }
}

class Outer4{

    public void show(){

        class Inner4{
            String name;
            int age;
            public void find(){
                System.out.println("Inner4 xxxx xxxx");
            }
        }
        Inner4 inn = new Inner4();
        inn.find();
    }

    /*如何使用局部内部类的资源
    * 在房内创建内部类的对象
    * 再调用*/

}
package cn.tedu.innerclass;

public class TestInner5 {
    /*
    本类用于测试匿名内部类
     */
    public static void main(String[] args) {

        new Inner1() {
            @Override
            public void save() {
                System.out.println("帝皇和麦昆关系友人之上");
            }
        }.save();

        new Inner2() {
            @Override
            void eat() {
                System.out.println("帝皇带着麦昆去吃甜品");
            }
        }.eat();



        Inner1 inner1 = null;
        inner1 = () -> {
            for (int i = 0; i < 3; i++) {
                System.out.println("帝皇麦昆贴贴");
            }
        };

        inner1.save();


    }
}


interface Inner1{
    void save();
}

abstract class Inner2{
    abstract void eat();
}

class Inner3{
    public void power(){
        System.out.println("帝皇-娇小的身体里充斥着强大的能量");
    }
    public void running(){
        System.out.println("没有伤病的她,无马能敌");
    }
}

posted on 2021-06-25 20:11  无声specialweek  阅读(36)  评论(0编辑  收藏  举报

导航