• 【获取class文件对象的三种方式】


    package test;
    
    /**
     * @author shusheng
     * @description 获取class文件对象的三种方式
     * @Email shusheng@yiji.com
     * @date 2018/12/23 23:14
     */
    public class ReflectDemo {
        /*
         *反射:就是通过class文件对象,去使用该文件中的成员变量,构造方法, 成员方法。
         *要想这样使用,首先你必须得到class文件对象,其实也就是得到Class类的对象。
         *Class类:
         *成员变量 Field
         *构造方法 Constructor
         *成员方法 Method
         *
         *获取class文件对象的方式:
         *A:Object类的getClass()方法
         *B:数据类型的静态属性class
         *C:Class类中的静态方法
         *public static Class forName(String className)
         *
         *一般我们到底使用谁呢?
         *A:自己玩    任选一种,第二种比较方便
         *B:开发 第三种
         *为什么呢?因为第三种是一个字符串,而不是一个具体的类名。这样我们就可以把这样的字符串配置到配置文件中。
         */
        public static void main(String[] args) throws ClassNotFoundException {
            // 方式1
            Person p = new Person();
            Class c = p.getClass();
            Person p2 = new Person();
            Class c2 = p2.getClass();
            System.out.println(p == p2);// fals
            System.out.println(c == c2);// true
    
            // 方式2
            Class c3 = Person.class;
            // int.class;
            // String.class;
            System.out.println(c == c3);
    
            // 方式3
            // ClassNotFoundException
            Class c4 = Class.forName("day27.Person");
            System.out.println(c == c4);
        }
    
    }
    
    
    
    package test;
    
    /**
     * @author shusheng
     * @description
     * @Email shusheng@yiji.com
     * @date 2018/12/23 23:49
     */
    public class Person {
        private String name;
        int age;
        public String address;
    
        public Person() {
        }
    
        private Person(String name) {
            this.name = name;
        }
    
        Person(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        public Person(String name, int age, String address) {
            this.name = name;
            this.age = age;
            this.address = address;
        }
    
        public void show() {
            System.out.println("show");
        }
    
        public void method(String s) {
            System.out.println("method " + s);
        }
    
        public String getString(String s, int i) {
            return s + "---" + i;
        }
    
        private void function() {
            System.out.println("function");
        }
    
        @Override
        public String toString() {
            return "Person [name=" + name + ", age=" + age + ", address="
                    + address
                    + "]";
        }
    
    }
    终身学习者
  • 相关阅读:
    Ubuntu 命令收集
    Step 4: Installing and Testing
    Step 2: Adding a Library
    【赵渝强】《大数据原理与实战》新书上市!!!
    【赵渝强】使用二进制包部署Kubernetes集群
    Asql 工具介绍
    PG访问外部数据postgres_fdw介绍及示例
    1.pg_basebackup 介绍及使用
    PG使用默认权限访问其它schema数据示例
    3.pg物理备份之快照备份使用和示例
  • 原文地址:https://www.cnblogs.com/zuixinxian/p/11275218.html
Copyright © 2020-2023  润新知