簡體   English   中英

用工廠模式實現反射

[英]Implementing reflection with factory pattern

我正在運行此代碼,但是卻收到此錯誤消息,但我無法弄清楚。 在反射的幫助下,要求使用工廠模式設計任何Java代碼。 在下面,我添加了在運行代碼時出現的錯誤消息,同時我的文件名和類名為TestReflectionFactoryDe​​sign。

錯誤信息:

Exception in thread "main" java.lang.ClassNotFoundException: com.test.TestReflectionFactoryDesign.Student
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at PersonFactory.getPersonWithFullQualifiedClassName(TestReflectionFactoryDesign.java:58)
    at TestReflectionFactoryDesign.main(TestReflectionFactoryDesign.java:6)

碼:

public class TestReflectionFactoryDesign {
    public static void main(String[] args) throws Exception {
        Person student = PersonFactory.getPersonWithFullQualifiedClassName("com.test.TestReflectionFactoryDesign.Student");    
        student.say();    
        Person teacher = PersonFactory.getPersonWithClass(Teacher.class);    
        teacher.say();   
        Person student2 = PersonFactory.getPersonWithName("student");    
        student2.say();   
    }    
}

class Student implements Person {    
    @Override    
    public void say() {    
        System.out.println("I am a student");    
    }    
}

class Teacher implements Person {    
    @Override    
    public void say() {   
        System.out.println("I am a teacher");    
    }    
}

interface Person {    
    void say();    
}

class PersonFactory {    
    // reflection, by full qualified class name    
    public static Person getPersonWithFullQualifiedClassName(String personType) throws Exception {    
        Class<?> personClass = Class.forName(personType);   
        return getPersonWithClass(personClass);    
    }

    // reflection, by passing class object   
    public static Person getPersonWithClass(Class personClass) throws Exception {  
        return (Person) personClass.newInstance();    
    }

    // no reflection, the ordinary way    
    public static Person getPersonWithName(String personType) {    
        if (personType.equalsIgnoreCase("STUDENT")) {    
            return new Student();    
        } else if (personType.equalsIgnoreCase("TEACHER")) {    
            return new Teacher();   
        }   
        return null;    
    }    
}

由於您(很可能)沒有在包的下面聲明您的類, package xxx.yyy.zzz在類的第一行中使用了package xxx.yyy.zzz ,因此Java認為它是未命名(默認)包的一部分,這已經非常詳細地解釋了這里

您可以通過將包設置為班級文件(最推薦)或更改完全合格的班級名稱以刪除無效的包來解決此問題(只需保留Student)。

如果您只是想學習反射的工廠模式,那么一個簡單的解決方案是使用您喜歡的文本編輯器將下面的代碼復制到TestReflectionFactoryDesign.java文件中,並轉到保存它的路徑,然后運行javac TestReflectionFactoryDesign.java和然后使用java TestReflectionFactoryDesign來查看結果。

public class TestReflectionFactoryDesign {
    public static void main(String[] args) throws Exception {
        Person student = PersonFactory.getPersonWithFullQualifiedClassName("Student");    
        student.say();    
        Person teacher = PersonFactory.getPersonWithClass(Teacher.class);    
        teacher.say();   
        Person student2 = PersonFactory.getPersonWithName("student");    
        student2.say();   
    }    
}

class Student implements Person {    
    @Override    
    public void say() {    
        System.out.println("I am a student");    
    }    
}

class Teacher implements Person {    
    @Override    
    public void say() {   
        System.out.println("I am a teacher");    
    }    
}

interface Person {    
    void say();    
}

class PersonFactory {    
    // reflection, by full qualified class name    
    public static Person getPersonWithFullQualifiedClassName(String personType) throws Exception {    
        Class<?> personClass = Class.forName(personType);   
        return getPersonWithClass(personClass);    
    }

    // reflection, by passing class object   
    public static Person getPersonWithClass(Class personClass) throws Exception {  
        return (Person) personClass.newInstance();    
    }

    // no reflection, the ordinary way    
    public static Person getPersonWithName(String personType) {    
        if (personType.equalsIgnoreCase("STUDENT")) {    
            return new Student();    
        } else if (personType.equalsIgnoreCase("TEACHER")) {    
            return new Teacher();   
        }   
        return null;    
    }    
}

如果您在IDE中,並且不想更改代碼,則應將package com.test.TestReflectionFactoryDesign放在代碼的開頭,這應該是創建java文件的路徑。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM