interface Shape {
void draw();
}
public class Main {
// interface type as instance variable
private Shape myShape;
// interface type as parameter type for a constructor
public Main(Shape s) {
this.myShape = s;
}
// interface type as return type of a method
public Shape getShape() {
return this.myShape;
}
// interface type as parameter type for a method
public void setShape(Shape s) {
this.myShape = s;
}
public void letItSwim() {
// interface type as a local variable
Shape locaShape = null;
locaShape = this.myShape;
// interface variable can invoke methods
// declared in the interface and the Object class
locaShape.draw();
}
}
更多建議: