类中的构造函数是否可以有多个super()?(Java或GWT)
我有这个类类中的构造函数是否可以有多个super()?(Java或GWT),java,gwt,Java,Gwt,我有这个类公共类IndexedColumn extends Column。我希望该类能够根据不同的键变量设置不同的对象 以下代码可以,但它只有1个super(新的ClickableTextCell()) 但是,如果我想同时使用super(newclickabletextcell())和super(newbuttoncell()),那么它就会出错 public class IndexedColumn extends Column<List<String>, String>{
公共类IndexedColumn extends Column
。我希望该类能够根据不同的键变量设置不同的对象
以下代码可以,但它只有1个super(新的ClickableTextCell())
但是,如果我想同时使用super(newclickabletextcell())
和super(newbuttoncell())
,那么它就会出错
public class IndexedColumn extends Column<List<String>, String>{
private final int index;
public IndexedColumn(int index, int cellType) {
if(cellType==1)
super(new ClickableTextCell());
else{
super(new ButtonCell());
}
this.index = index;
}
}
如您所见,为了能够使用ButtonCell
列,我必须免费使用int-forNoGoodReason
变量
这样做是一种好的做法吗
或者你能找到更好的解决方法吗?在Java中,类的超类在类头中显式声明;e、 g
public class Foo extends Bar ... {
....
}
除了对象
之外,每个类在编译时都有一个确定的超类。不能动态/在运行时更改或选择它
GWT基于Java和Java类型系统,具有相同的限制
另一方面,您可以声明具有多个构造函数的类,如下所示:
public class IndexedColumn extends Column<List<String>, String>{
private final int index;
public IndexedColumn(int index) {
super(new ClickableTextCell());
this.index = index;
}
public IndexedColumn(int index, int forNothingKey) {
super(new ButtonCell());
this.index = index;
}
}
public class Bar ... {
public Bar (Integer i) { ... }
public Bar (Double d) { ... }
}
public class Foo extends Bar ... {
public Foo (Integer i, Double d, boolean b) {
super(b ? i : d);
...
}
}
public class Foo extends Bar ... {
public Foo (Integer i) {
super(i);
...
}
public Foo (Double d) {
super(d);
...
}
public static Foo createFoo(Integer i, Double d, boolean b) {
return b ? new Foo(i) : new Foo(d);
}
}
这样做:
public class Foo extends Bar ... {
public Foo (Integer i) {
super(i);
...
}
public Foo (Double d) {
super(d);
...
}
}
但是,你不能这样做:
public class IndexedColumn extends Column<List<String>, String>{
private final int index;
public IndexedColumn(int index) {
super(new ClickableTextCell());
this.index = index;
}
public IndexedColumn(int index, int forNothingKey) {
super(new ButtonCell());
this.index = index;
}
}
public class Bar ... {
public Bar (Integer i) { ... }
public Bar (Double d) { ... }
}
public class Foo extends Bar ... {
public Foo (Integer i, Double d, boolean b) {
super(b ? i : d);
...
}
}
public class Foo extends Bar ... {
public Foo (Integer i) {
super(i);
...
}
public Foo (Double d) {
super(d);
...
}
public static Foo createFoo(Integer i, Double d, boolean b) {
return b ? new Foo(i) : new Foo(d);
}
}
问题是构造函数中的super
调用必须在编译时根据提供给super
调用的参数的静态类型解析为超类中的单个构造函数重载
如果您要使用new
创建对象,那么获得类似结果的唯一方法是在超类中有一个统一的构造函数来处理这两种/所有情况;e、 g
public class Bar ... {
public Bar (Object o) {
if (o instanceof Integer) {
...
} else if (o instanceof Double) {
...
} // etcetera
}
}
。。。但这是非常丑陋的,更不用说脆弱和有害的耦合了
另一种选择是使用工厂方法。。。像这样:
public class IndexedColumn extends Column<List<String>, String>{
private final int index;
public IndexedColumn(int index) {
super(new ClickableTextCell());
this.index = index;
}
public IndexedColumn(int index, int forNothingKey) {
super(new ButtonCell());
this.index = index;
}
}
public class Bar ... {
public Bar (Integer i) { ... }
public Bar (Double d) { ... }
}
public class Foo extends Bar ... {
public Foo (Integer i, Double d, boolean b) {
super(b ? i : d);
...
}
}
public class Foo extends Bar ... {
public Foo (Integer i) {
super(i);
...
}
public Foo (Double d) {
super(d);
...
}
public static Foo createFoo(Integer i, Double d, boolean b) {
return b ? new Foo(i) : new Foo(d);
}
}
超级(cellType==1?新建ClickableTextCell():新建ButtonCell())怎么样。虽然不是最具可读性,但它应该可以工作。我建议首选
static
factory方法。我想要3个super还是4个super?@boristeider+1关于静态工厂方法。不过,我认为你的第一个想法行不通,除非超类有一个带有对象
参数的构造函数,或者其他一些类是ClickableTextCell
和ButtonCell
的超类。也就是说,编译器必须能够选择一个适合表达式的超类构造函数。根据我的测试,这不能用于在运行时选择两个超类构造函数。我认为问题是关于多个超类构造函数,而不是多个超类。是的。。。我意识到了这一点。@Stephen,我测试了“super(cellType==1?new ClickableTextCell():new ButtonCell()”&它在GWT中运行良好,那么它有什么问题呢?@Tum如果你有一个构造函数,它的参数类型是ClickableTextCell
和ButtonCell
的超类型,那么它就没有问题。(我甚至在我的答案中加入了一个例子!!!)但是如果你不这样做,Java编译器会拒绝它。