【问题标题】:Java - Given Interface with methods - how are parameters passed?Java - 给定接口与方法 - 如何传递参数?
【发布时间】:2016-12-19 11:28:57
【问题描述】:

我试图保持代码尽可能通用,这只代表 基本设置。我是一名试图了解接口、类的 Java 初学者 和方法。我确实更改了接口名称和类名称以进行引用 他们更容易。我完全清楚这段代码不会编译。 我正在尝试理解这个概念。

Given 是一个接口,具有一个现有的 InterfaceClass 和另一个类 使用界面。

界面:

public interface IInterface extends Comparable<IInterface>{    
    String getContent() throws DumbException;
}

类:

public class InterfaceClass implements IInterface{
    public String getContent() throws DumbException {
        // here I would need a parameter (filepath) to get a file 
        // and read its content and return the content as string
        // however the interface doesn't provide a parameter for this
        // method. So how is this done?
    }
}

使用方法的类:

public class Frame extends AbstractFrame {
    public void setDetails(IInterface details) {
        // This is the call I don't understand...
        details.getContent();     
    }
}

我不明白的部分是: details 对象如何为 getContent() 提供任何参数? 我的意思是,除了 IInterface details

之外,我什至没有看到这个对象被定义

【问题讨论】:

标签: java methods parameters interface concept


【解决方案1】:

解决方案 1

您可以将IInterface重新定义为

public interface IInterface extends Comparable<IInterface>{    
    String getContent(String filepath) throws DumbException;
}

public class InterfaceClass implements IInterface{
    public String getContent(String filepath) throws DumbException {
        // use filepath to get the file's content
    }
}

用法是

public class Frame extends AbstractFrame {
    public void setDetails(IInterface details) {
        details.getContent("/path/to/some/folder");     
    }
}

解决方案 2

您不能更改IInterface,但您可以将构造函数添加到InterfaceClass

public class InterfaceClass implements IInterface{
    private String filepath;

    public InterfaceClass(String filepath) {
        this.filepath = filepath;
    }

    public String getContent() throws DumbException {
        // use filepath to get the file's content
    }
}

用法是

new Frame().setDetails(new InterfaceClass("path/to/some/folder"));

【讨论】:

  • 感谢您不仅理解我的困惑问题,而且还让问题变得简单明了!
【解决方案2】:
    public interface IInterface extends Comparable<IInterface>{

            String getContent() throws DumbException;
        }

    public class InterfaceClass implements IInterface{
        public String getContent() throws DumbException {//this method is of string type so you must provide return type 
            return "some details";
        }
    }

public class Frame extends AbstractFrame {
    public void setDetails(IInterface details) {
       System.out.println(details.getContent()); //output:- some details

    }
}

public class SomeClass{
public void someMethod(){
new Frame().setDetails(new InterfaceClass)
} 
}

请在此处关注SomeClass。在SomeClass 中,您传递的是扩展类的object,即interfaceClass(这是ploymorphism)。

和您一样,您需要将对象传递给interface 变量,例如IInterface details=new InterfaceClass()实现IInterface but note that the class must extend thatIInterfaceand in your case it isinterfaceClass 的任何其他类`

如果你不这样做,它会抛出nullpointerExecption,因为它只是一个引用变量,所以你需要用那个方法传递实际类的对象,多态会处理休息。

注意这是访问子类的常用方法并提供松散耦合,如果您使用spring,您会更清楚。

希望对你有帮助

【讨论】:

    【解决方案3】:

    details 是作为实现IInterface 的类型传递给setDetails 的参数。任何实现IInterface 的东西都可以用作details,因为该接口为您提供合同保证getContent 实际上是该实现的严格成员。

    【讨论】:

      【解决方案4】:

      您可以在基类的构造函数中传递数据

      public class InterfaceClass implements IInterface{
      
          private String data;
      
          public InterfaceClass(String data) {
              this.data = data;  
          }
      
          public String getContent() throws DumbException {
              //do something here with data
      
              // here I would need a parameter (filepath) to get a file 
              // and read its content and return the content as string
              // however the interface doesn't provide a parameter for this
              // method. So how is this done?
          }
      }
      

      你现在可以使用这个类了:

      IInterface myNewClass = new InterfaceClass("blablabla");
      frameClass.setDetails(myNewClass);
      

      并将其传递给二等舱。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-04
        • 1970-01-01
        • 1970-01-01
        • 2023-03-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多