java(5)-反射之调用构造方法

Person p = new Person("jkaf");
        //p = Person.class.newInstance(); // 只能调用public的无参构造函数
        //通过反射创建新的实例
        Constructor c = Person.class.getConstructor(String.class);
        p = (Person) c.newInstance("2333");

    }

}

class Person
{
    Person(String s)
    {

    }
}
发布了89 篇原创文章 · 获赞 0 · 访问量 1634

猜你喜欢

转载自blog.csdn.net/qq_43410618/article/details/104410165
今日推荐