Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通过反射创建对象_Java_Eclipse - Fatal编程技术网

Java 通过反射创建对象

Java 通过反射创建对象,java,eclipse,Java,Eclipse,我正试图用这样的反射来创建一个对象 (也许这不是一个好的架构,但我只是在做一些测试) package.com.interfaces; 公共接口雇员{ 列出getEmployees(); } 然后 package.com.personal; 公共类Employee实现了IEEmployee{ 公共雇员(){} 公开名单{ ... } } 然后在另一节课上 package com.factory; import java.lang.reflect.Constructor; public fi

我正试图用这样的反射来创建一个对象 (也许这不是一个好的架构,但我只是在做一些测试)

package.com.interfaces;
公共接口雇员{
列出getEmployees();
}
然后

package.com.personal;
公共类Employee实现了IEEmployee{
公共雇员(){}
公开名单{
...
}
}
然后在另一节课上

package com.factory;

import java.lang.reflect.Constructor;

public final class EmployeeFactory {

    private static String path = "com.personnel";

    public EmployeeFactory() {}

    public static interfaces.IEmployee CreateEmployee(){
        String className = path + ".Employee";
        try {
            Class<?> cls = Class.forName(className);
            Object object = cls.newInstance();
            return (interfaces.IEmployee)object;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }

    }

}
public final class PersonnelService {

    private static interfaces.IEmployee employeeFactory = com.factory.EmployeeFactory.CreateEmployee();

    public List<entities.Employee> getEmployees(){
        return employeeFactory.getEmployees();
    }

}
package.com.factory;
导入java.lang.reflect.Constructor;
公共末级雇员工厂{
私有静态字符串path=“com.personal”;
公共雇员工厂(){}
public static interfaces.ieemployee CreateEmployee(){
字符串className=path+“.Employee”;
试一试{
Class cls=Class.forName(className);
Object Object=cls.newInstance();
返回(interfaces.IEmployee)对象;
}捕获(例外e){
System.out.println(e.getMessage());
返回null;
}
}
}
然后在另一节课上

package com.factory;

import java.lang.reflect.Constructor;

public final class EmployeeFactory {

    private static String path = "com.personnel";

    public EmployeeFactory() {}

    public static interfaces.IEmployee CreateEmployee(){
        String className = path + ".Employee";
        try {
            Class<?> cls = Class.forName(className);
            Object object = cls.newInstance();
            return (interfaces.IEmployee)object;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }

    }

}
public final class PersonnelService {

    private static interfaces.IEmployee employeeFactory = com.factory.EmployeeFactory.CreateEmployee();

    public List<entities.Employee> getEmployees(){
        return employeeFactory.getEmployees();
    }

}
公共末级人员服务{
私有静态接口.ieemployee employeeFactory=com.factory.employeeFactory.CreateEmployee();
公开名单{
返回employeeFactory.getEmployees();
}
}
最后在我的主要方法

public static void main(String[] args) {
    List<entities.Employee> employees = new com.services.PersonnelService().getEmployees();
    for(entities.Employee employee : employees){
        System.out.println(employee.getEmployeeName());
    }
publicstaticvoidmain(字符串[]args){
List employees=new com.services.PersonnelService().getEmployees();
对于(实体。员工:员工){
System.out.println(employee.getEmployeeName());
}
当代码到达类cls=Class.forName(className)时,它抛出异常消息“com.personal.Employee.(java.lang.String)”和“NoSuchMethodException”


更新


我发现了问题。事实上,这是一个糟糕的命名约定。我有一个可序列化的Employee类(我的pojo)还有一个Employee类,它实现了我的接口IEEmployee,用于执行DB操作。我将最后一个重命名为EmployeeDAL,它工作了。即使我定义了完整的类名,正如您在示例中所看到的,它看起来像是我试图实例化我的可序列化pojo(我仍然不明白,但是…)。谢谢您的时间。

我尝试复制您的代码,但未发现此代码中存在任何错误。我仅在使用字符串参数更改Employee构造函数时收到NoSuchMethodException消息

package com.agit.example.test;

import java.util.ArrayList;
import java.util.List;

public class TestClass {
    public static void main(String[] args) {
        List<Employee> employees = new PersonnelService().getEmployees();
        for(Employee employee : employees){
            System.out.println(employee);
        }
    }
}

interface IEmployee {
    List<Employee> getEmployees(); 
}

class Employee implements IEmployee{
    public Employee(){}

    public List<Employee> getEmployees(){
        List<Employee> x = new ArrayList<>();
        x.add(this);
        return x;
    }
}

final class EmployeeFactory {

    private static String path = "com.agit.example.test";

    public EmployeeFactory() {}

    public static IEmployee CreateEmployee(){
        String className = path + ".Employee";
        try {
            Class<?> cls = Class.forName(className);
            Object object = cls.newInstance();
            return (IEmployee)object;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }

    }

}

final class PersonnelService {

    private static IEmployee employeeFactory = EmployeeFactory.CreateEmployee();

    public List<Employee> getEmployees(){
        return employeeFactory.getEmployees();
    }

}
package com.agit.example.test;
导入java.util.ArrayList;
导入java.util.List;
公共类TestClass{
公共静态void main(字符串[]args){
列出员工=new PersonnelService().getEmployees();
用于(员工:员工){
系统输出打印项次(员工);
}
}
}
介面雇员{
列出getEmployees();
}
类Employee实现IEEmployee{
公共雇员(){}
公开名单{
列表x=新的ArrayList();
x、 加上(这个);
返回x;
}
}
末级雇员工厂{
私有静态字符串path=“com.agit.example.test”;
公共雇员工厂(){}
公共静态IEEmployee CreateEmployee(){
字符串className=path+“.Employee”;
试一试{
Class cls=Class.forName(className);
Object Object=cls.newInstance();
返回(IEEmployee)对象;
}捕获(例外e){
System.out.println(e.getMessage());
返回null;
}
}
}
末级人事服务{
私有静态IEEmployee employeeFactory=employeeFactory.CreateEmployee();
公开名单{
返回employeeFactory.getEmployees();
}
}

根据错误消息,它似乎有点像。(java.lang.String)正在添加到className,但我不知道如何添加。这是您所有的相关代码吗?是的,是的。基本上className变量包含异常返回的字符串:«com.personal.Employee»。这三个句点是db connect以获取我的员工,但我无法到达。这里缺少一些内容。
NoSuchMethodException
是一个已检查的异常,但您尚未显示任何可能引发该异常的方法。当我实例化我的service PersonnelService时,它应立即调用静态方法CreateEmployee,该方法会给出错误。除此之外,没有更多代码。只有作为db连接执行选择的三个时段(与问题无关).那么
员工
构造函数呢?它真的只是
公共员工(){}