我可以强制传递的对象实现一个或多个接口吗?
Can I force a passed object to implement one or more interfaces?
我正在尝试编写一个 class,它依赖于在启动时传递给其构造函数的对象。这个对象应该是某个抽象基础 class 并且必须实现在要启动的 class 中定义的接口。但是我不知道这在 Java 中是否可行。代码应如下所示:
// The Base class implementing all basic functionality
abstract class A {
public void letBaseDoSomething() {
System.out.println("A does something...");
}
}
// Child class extending A
public class B extends A implements Z_requirements {
public void letMoreDoSomething() {
System.out.println("B does something...");
}
public Boolean canDoStuff() { /* functionality */}
}
// Now this is the class I want to initiate:
public class Z {
public interface Z_requirements {
Boolean canDoStuff();
}
public Z(<A implements Z_requirements> base) {
// PROBLEM
// This "base" class needs to both extend from the
// Base class AND implement the Z_requirements interface
// but apart from defining it in the doc, how do I enforce
// this in code? This way I can call both:
base.letBaseDoSomething();
// and
base.canDoStuff();
}
}
如果我没记错的话,在 Objective-C 中可以指定实现几个 "protocols" 的委托对象,这些基本上是接口。
更新:
实际上,我可能批准得太快了......按照下面的建议,我现在如何将 T base
分配给委托对象,对 Z
私有?例如:
public class Z<T extends A & Z.Z_requirements> {
public static interface Z_requirements {/* definition */}
private T delegate;
public Z(T base) {
delegate=base;
}
}
如果我使用此代码,我总是会收到 "type safety" 警告:
Z z = new Z(class_B_implementing_Z_requirements);
...如果我在构造函数中包含 <T>
,我会收到 "type undefined" 错误:
Z<T> z = new Z<T>(class_B_implementing_Z_requirements);
更新
我弄明白了,因为我误解了类型:
Z<B> z = new Z<B>(class_B_implementing_Z_requirements);
你必须使用generics. Declare a generic method with a type parameter that is bounded多种类型:
public static <T extends A & Z_requirements> void doIt(T base) {
base.letBaseDoSomething();
base.canDoStuff();
}
或者 - 如果您想在构造函数中执行此操作 - 您必须以相同的方式约束您的 class Z
:
public class Z<T extends A & Z.Z_requirements> {
public static interface Z_requirements {
Boolean canDoStuff();
}
public Z(T base) {
base.letBaseDoSomething();
base.canDoStuff();
}
}
public interface W { public Boolean canDoStuff() { /* so do it */ } }
public ZZTop extends B implements W
{
// ...
}
public Z
{
public Z(ZZtop zztop) {}
}
ZZTop 将从 B 继承并且必须从 W 实现 canDoStuff。
我正在尝试编写一个 class,它依赖于在启动时传递给其构造函数的对象。这个对象应该是某个抽象基础 class 并且必须实现在要启动的 class 中定义的接口。但是我不知道这在 Java 中是否可行。代码应如下所示:
// The Base class implementing all basic functionality
abstract class A {
public void letBaseDoSomething() {
System.out.println("A does something...");
}
}
// Child class extending A
public class B extends A implements Z_requirements {
public void letMoreDoSomething() {
System.out.println("B does something...");
}
public Boolean canDoStuff() { /* functionality */}
}
// Now this is the class I want to initiate:
public class Z {
public interface Z_requirements {
Boolean canDoStuff();
}
public Z(<A implements Z_requirements> base) {
// PROBLEM
// This "base" class needs to both extend from the
// Base class AND implement the Z_requirements interface
// but apart from defining it in the doc, how do I enforce
// this in code? This way I can call both:
base.letBaseDoSomething();
// and
base.canDoStuff();
}
}
如果我没记错的话,在 Objective-C 中可以指定实现几个 "protocols" 的委托对象,这些基本上是接口。
更新:
实际上,我可能批准得太快了......按照下面的建议,我现在如何将 T base
分配给委托对象,对 Z
私有?例如:
public class Z<T extends A & Z.Z_requirements> {
public static interface Z_requirements {/* definition */}
private T delegate;
public Z(T base) {
delegate=base;
}
}
如果我使用此代码,我总是会收到 "type safety" 警告:
Z z = new Z(class_B_implementing_Z_requirements);
...如果我在构造函数中包含 <T>
,我会收到 "type undefined" 错误:
Z<T> z = new Z<T>(class_B_implementing_Z_requirements);
更新
我弄明白了,因为我误解了类型:
Z<B> z = new Z<B>(class_B_implementing_Z_requirements);
你必须使用generics. Declare a generic method with a type parameter that is bounded多种类型:
public static <T extends A & Z_requirements> void doIt(T base) {
base.letBaseDoSomething();
base.canDoStuff();
}
或者 - 如果您想在构造函数中执行此操作 - 您必须以相同的方式约束您的 class Z
:
public class Z<T extends A & Z.Z_requirements> {
public static interface Z_requirements {
Boolean canDoStuff();
}
public Z(T base) {
base.letBaseDoSomething();
base.canDoStuff();
}
}
public interface W { public Boolean canDoStuff() { /* so do it */ } }
public ZZTop extends B implements W
{
// ...
}
public Z
{
public Z(ZZtop zztop) {}
}
ZZTop 将从 B 继承并且必须从 W 实现 canDoStuff。