构造函数的条件调用
Conditional calling of a constructor
我的情况类似于 this。
如果我仍然想有条件地调用构造函数怎么办? (虽然他们说创建单独的 classes 是可取的)
需求结构:
超级class:
public class Super
{
public Super(DTO1 dto1Object){
this.dto1Object = dto1Object;
}
public Super(DTO2 dto2Object)){
this.dto2Object = dto2Object;
}
}
派生class:
public class Derived extends Super
{
public Derived(Object obj)
{
//some_condition to check if passed object obj is dto1Object
//do something with dto1Object
//some_condition to check if passed object is dto2Object
//do something with dto2Object
}
}
我该如何实施?
编辑:
根据以下建议以这种方式实现:
超级class:
public class Super
{
protected static DTO1 dto1Obj;
protected static DTO2 dto2Obj;
public Super(DTO1 dto1Object){
this.dto1Object = dto1Object;
}
public Super(DTO2 dto2Object)){
this.dto2Object = dto2Object;
}
}
派生class:
public class Derived extends Super
{
public Derived(DTO1 dto1Object){ super(dto1Object); }
public Derived(DTO2 dto2Object){ super(dto2Object); }
public static Derived create(Object obj) {
if (obj.equals(dto1Obj) {
return new Derived((DTO1) obj);
}
if (obj.equals(dto2Obj) {
return new Derived((DTO2) obj);
}
// ...
private String Function(String str){
if(create(dto1Obj).equals(dto1Obj) {
//do something
}
else if(create(dto2Obj).equals(dto2Obj)){
//do something else
}
return str;
}
}
}
编辑2:
根据下面的建议,这是使用 instanceof
的正确方法吗?
if (create(dto1Obj) instanceof DTO1) {
//something
}
else if(create(dto2Obj) instanceof DTO2) {
//something else
}
显示以下错误:
Incompatible conditional operand types Derived and DTO1
Incompatible conditional operand types Derived and DTO2
你不能在构造函数中,因为 super(...)
必须是第一个语句。
我能想到的唯一方法是使用静态工厂方法,并调用构造函数的 class 特定重载:
public class Derived extends Super
{
private Derived(DTO1 dto1Object){ super(dto1Object); }
private Derived(DTO2 dto2Object){ super(dto2Object); }
public static Derived create(Object obj) {
//some_condition to check if passed object obj is dto1Object
//do something with dto1Object
if (someCondition) {
return new Derived((DTO1) obj);
}
//some_condition to check if passed object is dto2Object
//do something with dto2Object
if (someOtherCondition) {
return new Derived((DTO2) obj);
}
// ...?
}
}
我的情况类似于 this。
如果我仍然想有条件地调用构造函数怎么办? (虽然他们说创建单独的 classes 是可取的)
需求结构:
超级class:
public class Super
{
public Super(DTO1 dto1Object){
this.dto1Object = dto1Object;
}
public Super(DTO2 dto2Object)){
this.dto2Object = dto2Object;
}
}
派生class:
public class Derived extends Super
{
public Derived(Object obj)
{
//some_condition to check if passed object obj is dto1Object
//do something with dto1Object
//some_condition to check if passed object is dto2Object
//do something with dto2Object
}
}
我该如何实施?
编辑:
根据以下建议以这种方式实现:
超级class:
public class Super
{
protected static DTO1 dto1Obj;
protected static DTO2 dto2Obj;
public Super(DTO1 dto1Object){
this.dto1Object = dto1Object;
}
public Super(DTO2 dto2Object)){
this.dto2Object = dto2Object;
}
}
派生class:
public class Derived extends Super
{
public Derived(DTO1 dto1Object){ super(dto1Object); }
public Derived(DTO2 dto2Object){ super(dto2Object); }
public static Derived create(Object obj) {
if (obj.equals(dto1Obj) {
return new Derived((DTO1) obj);
}
if (obj.equals(dto2Obj) {
return new Derived((DTO2) obj);
}
// ...
private String Function(String str){
if(create(dto1Obj).equals(dto1Obj) {
//do something
}
else if(create(dto2Obj).equals(dto2Obj)){
//do something else
}
return str;
}
}
}
编辑2:
根据下面的建议,这是使用 instanceof
的正确方法吗?
if (create(dto1Obj) instanceof DTO1) {
//something
}
else if(create(dto2Obj) instanceof DTO2) {
//something else
}
显示以下错误:
Incompatible conditional operand types Derived and DTO1
Incompatible conditional operand types Derived and DTO2
你不能在构造函数中,因为 super(...)
必须是第一个语句。
我能想到的唯一方法是使用静态工厂方法,并调用构造函数的 class 特定重载:
public class Derived extends Super
{
private Derived(DTO1 dto1Object){ super(dto1Object); }
private Derived(DTO2 dto2Object){ super(dto2Object); }
public static Derived create(Object obj) {
//some_condition to check if passed object obj is dto1Object
//do something with dto1Object
if (someCondition) {
return new Derived((DTO1) obj);
}
//some_condition to check if passed object is dto2Object
//do something with dto2Object
if (someOtherCondition) {
return new Derived((DTO2) obj);
}
// ...?
}
}