zoukankan      html  css  js  c++  java
  • Treflection01_Class对象_构造函数_创建对象

    1、

    package reflectionZ;
    
    import java.lang.reflect.Constructor;
    import java.util.List;
    
    public class Treflection01
    {
        public static void main(String[] args) throws Exception
        {
            // 第13课
            
            Class<?> clazz1 = Class.forName("reflectionZ.Cat");
            // 使用反射 生成对象
            // ZC: 创建出来的对象,使用的时候 需要处理 :
            // ZC:    (1)、强转成 某类的对象
            // ZC:    (2)、继续用反射来操作这个 对象
            
            // 使用的是 默认的 构造函数
            clazz1.newInstance(); // 强转
            
            // 通过Class对象来得到构造函数
            Constructor c1 = clazz1.getConstructor(Class.forName("java.lang.String"), int.class);
            Cat cat1 = (Cat)c1.newInstance("小猫", 6); // 强转
            cat1.Show();
            
            Constructor<?> c2 = clazz1.getConstructor(String[].class);
            String[] foods = {"鱼", "老鼠"};
            // Exception in thread "main" java.lang.IllegalArgumentException: wrong number of arguments
            //Cat cat2 = (Cat)c2.newInstance(foods); // 强转
            // 可变参数 ==> 拆散 --> String[] foods==>拆成foods[0],foods[1],...
            // ZC: 可是,普通的函数调用,没有这种情况的啊,为啥Constructor.newInstance(...)会这样?
            Cat cat2 = (Cat)c2.newInstance((Object)foods); // 强转
            cat2.Show();
        }
    }
    
    class Cat
    {
        public Cat()
        {}
        public Cat(String _strName, int _iAge)
        {
            FstrName = _strName;
            FiAge = _iAge;
        }
        public Cat(String[] _foods)
        {
            Ffoods = _foods;
        }
        
        public String FstrName;
        public void setFstrName(String fstrName)
        {
            System.out.println("setFstrName("+fstrName+")");
            FstrName = fstrName;
        }
        public int FiAge;
        public String[] Ffoods = null;
        private String Fstr1 = "WW";
        
        public void Show()
        {
            System.out.println("名字 : "+FstrName);
            if (Ffoods != null)
            for (int i=0; i<Ffoods.length; i++)
                System.out.println("Ffoods["+i+"] : "+Ffoods[i]);
        }
        public void Show(String _strName)
        {
            System.out.println("名字 : "+_strName);
        }
        public void Show(String _strName, int _iAge)
        {
            System.out.println("名字 : "+_strName+" , 年龄 : "+_iAge);
        }
        public void Show(List _list)
        {
            if (_list == null)
            {
                System.out.println("输入的_list == null .");
                return;
            }
            for (int i=0; i<_list.size(); i++)
                System.out.println("_list["+i+"] : "+_list.get(i));
        }
        private void Show(int _iAge)
        {
            System.out.println("年龄 : "+_iAge);
        }
    }

    2、

  • 相关阅读:
    maria-developers 开发者邮件
    Parallel Programming--perfbook
    面向对象设计模式中类与类关系
    binlog 轻松的找到没有及时提交的事物(infobin工具
    deeplearningbook-chinese
    Introduction to the Optimizer --cbo
    dell T420热插拔安装过程
    MySQL是如何利用索引的
    BTrace housemd TProfiler
    杨建荣的学习笔记
  • 原文地址:https://www.cnblogs.com/javaskill/p/5428272.html
Copyright © 2011-2022 走看看