java - java的接口(interface)问题

标签 java interface casting jaxb

我在使用 java 中的接口(interface)时遇到问题。我将向您展示代码,它将更加明确:

我使用 jaxb 从 XML 配置文件中提取数据:

public class LoadFilePollerConfiguration implements IConfig{

    File configFile = new File("config.xml");

    @Override
    public void loadConfiguration() throws Exception  {
        // TODO Auto-generated method stub
        loadFilePollerConfiguration();
    }

    private void loadFilePollerConfiguration() throws Exception{
        // TODO Auto-generated method stub
        SchemaFactory sf = SchemaFactory.newInstance
                (XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = sf.newSchema(new File("config.xsd"));
        JAXBContext jc = JAXBContext.newInstance(FilePollerConfiguration.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        unmarshaller.setSchema(schema);
        unmarshaller.setEventHandler(new MyValidationEventHandler());
        FilePollerConfiguration f = (FilePollerConfiguration)
                unmarshaller.unmarshal(configFile);

        Marshaller mar = jc.createMarshaller();
        mar.marshal(f, new File("test.xml"));

    }

}

我将向您展示 IConfig 界面:

public interface IConfig {

    public void loadConfiguration() throws Exception;

}

我有一个用于轮询存储库的类,并且我在主函数中使用这个函数,是什么让我遇到了一些麻烦:

public class WatchSer {    

    private final WatchService watcher;     
    private final Map<WatchKey,Path> keys;          
    private boolean trace = false;  
    private FilePollerConfiguration configuration;

    WatchSer(IConfig conf) throws IOException {    

        this.watcher = FileSystems.getDefault().newWatchService();         
        this.keys = new HashMap<WatchKey,Path>();   
        configuration = (FilePollerConfiguration) conf;

    }

    public ArrayList<IConfig> getAction(File file, String Event) {

        Pattern p;

        for(int i = 0; i < configuration.directoriesList.size(); i++){
            p =  Pattern.compile(configuration
                              .directoriesList.get(i).toString());
            System.out.println(p);

        }

        return null;

    }

}

最后主要实例化loadFilePollerConfiguration类,使用loadConfiguration()。到这里为止,还可以,但是当我想创建一个 WatchSer 时,我遇到了转换问题:

>

public class Main {

    /**
     * @param args
     * @throws Exception 
     */

    public static void main(String[] args) throws Exception{
        // TODO Auto-generated method stub
        LoadFilePollerConfiguration l = new LoadFilePollerConfiguration();
        l.loadConfiguration();
        WatchSer w = new WatchSer(l);
        w.getAction(new File("C://Users//jmoreau040612
                    //Desktop//New//yop.xml"),  "create");
    }

}

线程“main”java.lang.ClassCastException中出现异常:LoadFilePollerConfiguration无法转换为FilePollerConfiguration

最佳答案

仅当 LoadFilePollerConfiguration 扩展 FilePollerConfiguration 时,

LoadFilePollerConfiguration 才能转换为 FilePollerConfiguration

从你的问题来看,你似乎误解了接口(interface)的概念。要进一步解释,请查看以下代码。

Inteface iSample {
  void doThing();
  //more code
}

class Parent implements iSample {
  void doThing() {
     System.out.println("Parent");
  }
}

class AnotherParent implements iSample {
  void doThing() {
     System.out.println("Another Parent");
  }
}

class Child extends Parent implements iSample{
   //child specific code
}

class Test {
   public static void main(String[] args) {
       iSample i = new Parent();
       iSample j = new AnotherParent();
       iSample k = new Child();
       Parent p = j; //error
   }
}

仅仅因为 ParentAnotherParent 实现了 iSample,并不意味着 Parent 对象可以保存 AnotherParent 的对象。但是接口(interface)iSample的引用可以保存ParentAnotherParent,因为它们都实现了iSample,并且它也可以保存Child的实例,因为它的父类(super class)已经完成了接口(interface)契约

关于java - java的接口(interface)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19977065/

相关文章:

ios - 创建一个 NSObject 来存储数据

swift - 无法将 CGFloat 类型的值分配给 CGFloat 类型的值

C++ - 转换变量以及它如何处理它们?

java - Netbeans JDK 校准 8.1

java - 我可以使用 RESTeasy 获取 application.wadl 文件吗?

java - 是我导师的界面有问题吗?

java - 接口(interface)中的静态初始化

iphone - 如何以 CGFloat 形式获取该值?

java - Java中的多线程服务器程序

java - IntelliJ 不使用本地存储库进行 Maven 构建