我需要在Java SDK中弃用API以使它们更通用.但我无法弄清楚如何做以下情况:
public class AdoptDog {
public interface OnDogAdoption {
public void onDogAdoption(String dogName);
}
public void adoptDog(final String dogName, OnDogAdoption callbackObj) {
// Perform asynchronous tasks...
// Then call the callback:
callbackObj.onDogAdoption(dogName);
}
}
Run Code Online (Sandbox Code Playgroud)
SDK的用户进行如下调用:
AdoptDog adoptDog = new AdoptDog();
adoptDog.adoptDog("Snowball", new OnDogAdoption {
@Override
public void onDogAdoption(String dogName) {
System.out.println("Welcome " + dogName);
}
};
Run Code Online (Sandbox Code Playgroud)
我想概括从Dog到Pet,并弃用提到Dog的API.为了向后兼容,我在弃用API时不必更改上面采用Snowball的代码片段.
我是如何尝试弃用Dog API的:
// Introduce Pet API
public class AdoptPet {
public interface OnPetAdoption {
public void onPetAdoption(String petName);
}
public void adoptPet(final …Run Code Online (Sandbox Code Playgroud) 我有块使用函数执行计算step().这些块可以相互连接connect(Block).
interface Block {
void connect(Block b);
void step();
}
Run Code Online (Sandbox Code Playgroud)
但是,从具体的块实现(例如step)中,应该可以read从连接的块中:
class ABlockImpl implements Block {
private Block src; // link to the block this block is connected to
public void connect(Block b) {
src = b;
}
public void step() {
double x = src.read(); // XXX src is of type Block and there is no read() in Block
/* ... */
}
public double read() {
return 3.14;
}
} …Run Code Online (Sandbox Code Playgroud)