A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 张先龙 中级黑马   /  2013-4-11 19:15  /  1891 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 张先龙 于 2013-4-11 21:36 编辑

public class Test {

        public static <Constructer> void main(String[] args) throws ClassNotFoundException, SecurityException, NoSuchMethodException {
        String TestClass = null;
        // TODO Auto-generated method stub

        Constructer con=(Constructer)Class.forName("TestClass").getConstructor(String.class);
               
        TestClass test=(TestClass)con.newInstance("");
        }

}

class TestClass{
        
        private String str;
        
        TestClass(String str){
               
                this.str=str;
        }
        
        public void printStr(){
                System.out.println(str);
        }
}
红色的那段代码出错 ,提示的是The method newInstancce(String) is undefined for the type Constructer ,我看了半天也没改过来  哪位同学指点下!

评分

参与人数 1技术分 +1 收起 理由
冯海霞 + 1

查看全部评分

6 个回复

倒序浏览
你看看这样行不?!
  1. public class Test {
  2.        
  3.         public static <Constructer> void main(String[] args) throws Exception {
  4.                
  5. //                String TestClass = null;
  6.                 Constructer con = (Constructer) Class.forName("TestClass").getConstructor(String.class).newInstance(new Object[]{""});
  7. //                TestClass test=(TestClass)con.newInstance("");
  8.         }

  9. }

  10. class TestClass {
  11.         private String str;

  12.         public TestClass(String str) {
  13.                 this.str = str;
  14.         }
  15.         public void printStr() {
  16.                 System.out.println(str);
  17.         }
  18. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
冯海霞 + 1

查看全部评分

回复 使用道具 举报
用你的代码试了下,还是不行啊,,,,
回复 使用道具 举报
  1. import java.lang.reflect.Constructor;

  2. public class ddddd {

  3.     public static void main(String[] args) throws Exception {

  4.     Class<TestClass> testclazz=TestClass.class;
  5.     Constructor<TestClass> con=testclazz.getConstructor(String.class);            
  6.     TestClass test=(TestClass)con.newInstance("xcv");
  7.    
  8.     test.printStr();
  9.     }

  10. }

  11. class TestClass{
  12.    
  13.     private String str;
  14.    
  15.     public TestClass(String str){
  16.             
  17.             this.str=str;
  18.     }
  19.    
  20.     public void printStr(){
  21.             System.out.println(str);
  22.     }
  23. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
冯海霞 + 1

查看全部评分

回复 使用道具 举报
public class Test {
           //不要修改主函数
        public static <Constructer> void main(String[] args) throws ClassNotFoundException, SecurityException, NoSuchMethodException {
        String TestClass = null;
        // TODO Auto-generated method stub
        //转换类型不对,你要得到的是TestClass的构造函数  干嘛要转成Constructer
        Constructer con=(Constructer)Class.forName("TestClass").getConstructor(String.class);
               
        TestClass test=(TestClass)con.newInstance("");
        }

}

class TestClass{
        
        private String str;
        
        TestClass(String str){//构造函数要是public的才能被其他类访问啊?????
               
                this.str=str;
        }
        
        public void printStr(){
                System.out.println(str);
        }
}
//上面给你提了一个正确的代码  你自己看看吧!你这个代码错误的地方我已经给你标示了一下,也许有不对的地方  你自己琢磨一下吧

评分

参与人数 1技术分 +1 收起 理由
冯海霞 + 1

查看全部评分

回复 使用道具 举报
刘印12 发表于 2013-4-11 20:28
public class Test {
           //不要修改主函数
        public static  void main(String[] args) thro ...

谢谢。。编译通过,,不过执行有异常。。不能正常执行。
回复 使用道具 举报
张先龙 发表于 2013-4-11 20:40
谢谢。。编译通过,,不过执行有异常。。不能正常执行。

我这边是可以运行的啊  你把你的源代码在发过来 然后把运行报错信息发一下
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马