Java - 给定方法接口 - 如何传递参数?

Java - Given Interface with methods - how are parameters passed?

我试图让代码尽可能通用,这仅代表 基本设置。我是一个 Java 试图理解接口的初学者,Classes 和方法。我确实更改了接口名称和 Class 名称以进行引用 他们更容易。我完全知道此代码无法编译。 我正在尝试理解这个概念。

给出的是一个接口,有一个现有的接口Class和另一个class 使用界面。

接口:

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

Class:

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?
    }
}

class使用方法:

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

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

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

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

您可以在基础构造函数中传递数据 class

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?
    }
}

您现在可以使用这个class:

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

并将其传递给第二个 class。

    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 中,您传递的是扩展 class 的 object,即 interfaceClass(这是 ploymorphism)。

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

如果你不这样做,它会抛出 nullpointerExecption 因为它只是一个引用变量,所以你需要使用该方法传递实际 class 的对象,多态性将处理其余部分。

注意 这是访问子 classes 的常用方法并提供松耦合,如果你在 spring 上工作,你会知道得更多.

希望对您有所帮助

解决方案 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"));