[英]the code under class which implement the interface class does not execute
I have an interface class called intclass. 我有一个称为intclass的接口类。 this interface class has a method called mymethod. 该接口类具有一个称为mymethod的方法。 I have created a class called impclass which implements the interface class, whithin which have the code of mymethod. 我创建了一个名为impclass的类,该类实现了接口类,其中包含mymethod的代码。 however I have another method called dmeth which receives the interface intclass as a parameter. 但是,我还有另一个名为dmeth的方法,该方法接收接口intclass作为参数。 the method dmeth is in a different class called dclass. dmeth方法在另一个名为dclass的类中。 however when I run dclass, the code under mymethod does not execute. 但是,当我运行dclass时,mymethod下的代码无法执行。 I don't understand why. 我不明白为什么。 below is the code: 下面是代码:
public interface intclass{
public void mymethod(string message);
}
public class impclass
implements intclass{
public void mymethod(String message){
System.out.println("this is the message"+message);
}
}
public class dclass{
public void dmeth(intclass cl){
....
}
public static main(String[] arg0){
impclass icl =new impclass()
dclass d=new dclass();
d.dmeth(icl);
}
}
I have no Idea on what is going wrong.. In case there is even an error concerning the expected parameter for mymethod function, I expect the error, or crashing of the program. 我对出什么问题一无所知。如果万一存在关于mymethod函数的预期参数的错误,我希望该错误或程序崩溃。 but in my case the program run smoothly but the System.out.println("this is the message"+message); 但就我而言,程序运行平稳,但是System.out.println(“这是消息” + message); does not print. 不打印。 I tried to put a break in mymethod function and debug, but it was then that I realised mymethod function is not even reached. 我试图中断mymethod函数并进行调试,但是那时我才意识到甚至无法实现mymethod函数。 does anyone has an Idea on what ould be going on 有谁知道会发生什么
String and string are not the same thing. 字符串和字符串不是同一件事。 Be aware of it when you type 键入时请注意
public interface intclass{
public void mymethod(string message);
}
and 和
public class impclass
implements intclass{
public void mymethod(String message){ //Note the Uppercase S
System.out.println("this is the message"+message);
}
}
Also, your impclass is missing a constructor, and you are not calling the method in any way. 另外,您的impclass缺少构造函数,因此您不会以任何方式调用该方法。 Your dmeth should contain 您的dmeth应该包含
public void dmeth(intclass cl){
cl.mymethod("Message");
}
in order to the mymethod to be executed. 为了执行该方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.