开发者们好。
那么问题来了:
我需要我的类(class)MyClass
实现接口(interface)IMyInterface
使用方法doSomething();
并覆盖方法doSomethiing()
另一个类(class)我们就叫他TheOtherClass
还实现 IMyInterface
并覆盖方法doSomethiing()
并加上实现一些逻辑 doSomething()
方法(在TheOtherClass
中)。
问题:
- 我怎样才能制作
doSomething()
MyClass
中实现的方法触发器doSomething()
来自TheOtherClass
自动 - 另外如果
MyClass
调用我想要doSomethig()
自动调用方法??
最佳答案
我想是这样的。
public class TheOtherClass implements IMyInterface{
public void doSomething(){}
}
public class MyClass implements IMyInterface{
// Instance initialization block:
// Runs before the constructor each time you instantiate an object
{
this.doSomething()
}
public MyClass(){
}
IMyInterface theOtherClass = new TheOtherClass ();
public void doSomething(){
theOtherClass .doSomething();
//Add more logic here
}
}
关于java - 类是否可以实现接口(interface)并执行该接口(interface)背后的逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21621472/