试图获取“回调接口”上的句柄。据我了解,除以下内容外,该概念还很有意义
//FromSomeClass1
MyInterface conect;
public void setInterface(MyInterface myInter)
{
this.conect=myInter;
}
interface MyInterface
{
public void update(String str);
}
(模糊性从这里开始)所以当另一个班级试图
//FromSomeClass2 implements MyInterface
...onCreate()
{
SomeClass1 newC = new SomeClass1()
newC.setInterface(this) ;
}
update(String str){
....code
}
这将不起作用,因为我正在传递给新对象?除非我将Class1中的“ conect”变量设为静态(好主意,坏主意...后果???)
简单地说,将对象传递回“ setInterface”方法的正确方法是什么。
希望这是有道理的,谢谢。
ps对于所有希望很好地了解回电的人,此链接将有所帮助。
考虑一个Animal
带有单个says(String)
回调的示例接口,
interface Animal {
public void says(String msg);
}
接下来,让我们添加一个使用该Animal
接口来说点什么的类-
class Say {
public void say(Animal animal) {
animal.says("Bawk");
}
}
现在让我们实现两个不同的Animal
(一个或多个)-我们将有一个Cow
类和一个Sheep
类,
class Cow implements Animal {
public void says(String msg) {
System.out.printf("%s, I mean moo!%n", msg);
}
}
class Sheep implements Animal {
public void says(String msg) {
System.out.printf("%s, I mean baah!%n", msg);
}
}
最后,为了演示我们在上面定义的回调方法-
public static void main(String[] args) {
Say say = new Say();
say.say(new Cow());
say.say(new Sheep());
}
输出为
Bawk, I mean moo!
Bawk, I mean baah!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句