简体   繁体   English

实现接口类的类下的代码不执行

[英]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.

相关问题 class中的接口方法没有实现吗? - Interface methods in a class that does not implement it? 扩展抽象 class 的 class 是否仍然必须实现抽象 class 正在实现的接口?:Z93F725A07423FE1C8486ZD4 - Does a class which is extending an abstract class still has to implement the interface that abstract class is implementing?: java 如何保留通过注释实现接口的类 - How to keep class which implement an interface with annotation 类是否可以实现该接口并在该接口后面执行逻辑 - Is it possible for class to implement the interface and execute logic behind that interface 类 SpringHibernateJpaPersistenceProvider 没有实现请求的接口 PersistenceProvider - Class SpringHibernateJpaPersistenceProvider does not implement the requested interface PersistenceProvider 为什么ZonedDateTime类没有实现TemporalAdjuster接口 - why ZonedDateTime class does not implement TemporalAdjuster interface 具体类实现了一个接口,该接口知道实现它的具体类 - Concrete class implements an interface which knows the concrete class that implement it 将最终类强制转换为该类未声明要实现的兼容接口 - Cast a final class to a compatible interface that the class does not claim to implement Map 接口是否扩展或实现了一些其他接口或 class? - Does Map interface extend or implement some other interface or class? 接口中的Java实现类 - Java implement class in interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM