做了一个如下的interfacepublic interface OnlineSystem_Interface { void storeRecord(String XMLSource, int recId); String retrieveRecord(int recId); void setDate(String date, int recId); void updateRecord(int recId); String setUpdateDate(String date);}然后又做了一个类实现这个interfacepublic class Adapter implements OnlineSystem_Interface{

。。
}主类里面生成了如下的interface对象public class Client { private OnlineSystem_Interface document; public Client() { this.document = new Adapter(); }
}但是, 请问, 为何this.document = new Adapter();这句报错?
报Type mismatch: cannot convert from Adapter to OnlineSystem_Interface请指点, 谢谢

解决方案 »

  1.   

      public class Adapter implements OnlineSystem_Interface 去掉public ,或者在另一个文件定义
      

  2.   

    要不你的Adapter就没有implements接口OnlineSystem_Interface
    看看是不是有拼写错误,clean一下,rebuild一下再看
      

  3.   

    interface OnlineSystem_Interface {  void storeRecord(String XMLSource, int recId);  String retrieveRecord(int recId);  void setDate(String date, int recId);  void updateRecord(int recId);  String setUpdateDate(String date);  } 
    //
      class Adapter implements OnlineSystem_Interface{ 
    public void storeRecord(String XMLSource, int recId){

    } public String retrieveRecord(int recId){
    return null;
    } public void setDate(String date, int recId){

    } public void updateRecord(int recId){

    } public String setUpdateDate(String date){
    return null;
    }
    } // 主类里面生成了如下的interface对象  public class Client {  private OnlineSystem_Interface document;  public Client() {  this.document = new Adapter();  } 
    } // 但是, 请问, 为何this.document = new Adapter();这句报错? 
    // 报Type mismatch: cannot convert from Adapter to OnlineSystem_Interface 
    没错啊?????,再把接口前的public 去掉
      

  4.   

    能去吗?interface,client,adapter是分别在三个文件里的。
      

  5.   

    使用的是windows系统不?用资源管理器(win+E)打开你的包结构,展开所有包,,,把类的结构分部情况粘到这里。让大家看下你的包里边的类的分部情况,另外把public class Client这个类的所有代码全部粘在这里来。你给的信息量太少了,不能找到问题。你给的代码如果包引用正确的是,是没错的。
      

  6.   

    好的, 谢谢各位高手, 粘贴如下package assignment5;/* This class is the Online System Client.
     It reads a source xml file as input and it invokes methods defined in the
     OnlineSystem_Interface Interface on an class that implements that interface */import java.io.*;
    import java.util.Scanner;import com.sun.xml.internal.bind.v2.model.core.Adapter;public class Client { // Define a document object of type OnlineSystem_Interface
    private OnlineSystem_Interface document; // The Client constructor
    public Client() { // Instanciate the document object with a class that implements the
    // Org_A_Doc_Interface
    this.document = new Adapter(); } // The Main Method
    public static void main(String Args[]) { // Instantiate a Client Object
    Client client = new Client(); // Create a string to store user input
    String inputFile = null;
    System.out.print("Input file name: "); // Read the Source xml file name provided as a command line argument
    Scanner scanner = new Scanner(System.in);
    inputFile = scanner.next(); // Create a string to store user input
    String recId = null;
    System.out.print("Input record ID: "); // Read the Source xml file name provided as a command line argument
    recId = scanner.next(); //Convert the String recId to an integer value
    int intRecID = Integer.parseInt(recId); if (inputFile != null && !inputFile.equals("")) { // Create and empty string to store the source xml file
    String file = ""; try {
    // Open the file
    FileInputStream fstream = new FileInputStream(inputFile); // Convert our input stream to a
    // BufferedReader
    BufferedReader d = new BufferedReader(new InputStreamReader(
    fstream)); // Continue to read lines while
    // there are still some left to read
    String temp = "";
    while ((temp = d.readLine()) != null) {
    // Add file contents to a String
    file = file + temp;
    }
    d.close(); // The Client Calls the storeRecord Method on the Client.Document
    // object
    if (!file.equals("")) {
    client.document.storeRecord(file, intRecID);
    } } catch (Exception e) {
    System.err.println("File input error");
    } } else
    System.out.println("Error: Invalid Input"); }}
      

  7.   

    这个是adapter类public class Adapter implements OnlineSystem_Interface{
    private Admin_DataMgr dataManager;
    private Admin_UpdateMgr updateManager;
    private FileDetailsProvider fp;
    Adapter(){
            fp= FileDetailsProvider.getInstance();
    fp.loadConfigFile("C:\\assignment5\\XMLTransformerConfig.xml");
    dataManager = new Admin_DataMgr();
    updateManager = new Admin_UpdateMgr();
    }
    public void storeRecord(String XMLSource, int recId){
    dataManager.addRecord(XMLSource, recId);
    } public String retrieveRecord(int recId){
    return dataManager.getRecord(Integer.toString(recId));
    } public void setDate(String date, int recId){
    dataManager.setRecordDate(date, recId);
    }
    public void updateRecord(int recId){
    updateManager.updateRecord(Integer.toString(recId));
    } public String setUpdateDate(String date){
    return updateManager.setLastUpdateDate(date);
    }
    }
      

  8.   

    import com.sun.xml.internal.bind.v2.model.core.Adapter; 当然会错了,你导入的类是错误的。