Java-给定的方法接口-参数是如何传递的?
我试图使代码尽量通用,这只代表 基本设置。我是一名Java初学者,试图理解接口、类和 和方法。我确实更改了接口名和类名以进行引用 让他们更容易。我完全知道,这段代码不会编译。 我正在努力理解这个概念 给定的是一个接口,包含一个现有的InterfaceClass和另一个类 使用接口 界面:Java-给定的方法接口-参数是如何传递的?,java,methods,parameters,interface,concept,Java,Methods,Parameters,Interface,Concept,我试图使代码尽量通用,这只代表 基本设置。我是一名Java初学者,试图理解接口、类和 和方法。我确实更改了接口名和类名以进行引用 让他们更容易。我完全知道,这段代码不会编译。 我正在努力理解这个概念 给定的是一个接口,包含一个现有的InterfaceClass和另一个类 使用接口 界面: public interface IInterface extends Comparable<IInterface>{ String getContent() throws Dumb
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
details
是作为实现IInterface
的类型传递给setDetails
的参数之外,我甚至没有看到这个对象被定义。实现IInterface
的任何内容都可以用作详细信息,因为该接口为您提供了合同保证,即getContent
实际上是该实现的严格成员。您可以在基类的构造函数中传递数据
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);
并将其传递给第二类。公共接口接口{
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)
}
}
字符串getContent()引发异常;
}
公共类InterfaceClass实现接口{
公共字符串getContent()引发异常{//此方法为字符串类型,因此必须提供返回类型
返回“一些细节”;
}
}
公共类框架扩展了抽象框架{
公共无效集合详细信息(界面详细信息){
System.out.println(details.getContent());//输出:-一些细节
}
}
公共类{
公共方法(){
新建框架().setDetails(新建接口类)
}
}
请注意这里的SomeClass
。在SomeClass
中,您正在传递扩展类的对象,即interfaceClass
(这是polymorphism
)
和您一样,您需要将对象传递给interface
变量,如IInterface details=newinterfaceclass()
或实现IInterface的任何其他类,但请注意,该类必须扩展该IInterface,在您的情况下,它是InterfaceClass`
如果您未能做到这一点,它将抛出nullpointerExecption
,因为它只是一个引用变量,所以您需要使用该方法传递实际类的对象,而polymorphism将处理rest
注意这是访问子类的常用方法,并提供松散耦合,如果您继续工作,您会知道得更好
希望对解决方案1有所帮助
您可以将接口重新定义为
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
}
}
解决方案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
}
}
用法是
public class Frame extends AbstractFrame {
public void setDetails(IInterface details) {
details.getContent("/path/to/some/folder");
}
}
new Frame().setDetails(new InterfaceClass("path/to/some/folder"));
setDetails(界面详细信息)
;)我希望我能帮上忙。我被你的困惑弄糊涂了。谢谢你不仅理解了我困惑的问题,而且把它说得简单明了!