Java 该项目赢得了';不能使用子类而不是超类

Java 该项目赢得了';不能使用子类而不是超类,java,inheritance,subclass,superclass,cloudsim,Java,Inheritance,Subclass,Superclass,Cloudsim,有一个名为Datacenter的类,其中构造函数是: public Datacenter( String name, DatacenterCharacteristics characteristics, VmAllocationPolicy vmAllocationPolicy, List<Storage> storageList, double schedulingInterval) throws Ex

有一个名为
Datacenter
的类,其中构造函数是:

public Datacenter(
        String name,
        DatacenterCharacteristics characteristics,
        VmAllocationPolicy vmAllocationPolicy,
        List<Storage> storageList,
        double schedulingInterval) throws Exception {
    super(name);

    setCharacteristics(characteristics);
    setVmAllocationPolicy(vmAllocationPolicy);
    setLastProcessTime(0.0);
    setStorageList(storageList);
    setVmList(new ArrayList<Vm>());
    setSchedulingInterval(schedulingInterval);

    for (Host host : getCharacteristics().getHostList()) {
        host.setDatacenter(this);
    }

    // If this resource doesn't have any PEs then no useful at all
    if (getCharacteristics().getNumberOfPes() == 0) {
                throw new Exception(super.getName()
                    + " : Error - this entity has no PEs. Therefore, can't process any Cloudlets.");
    }

    // stores id of this class
    getCharacteristics().setId(super.getId());
}
运行程序的结果如下:

问题是,如果我通过扩展
数据中心
类来定义自己的数据中心,程序将无法工作。我将MyDatacenter类定义如下:

public class MyDatacenter extends Datacenter{

    /* My own variables */

    public MyDatacenter(String name, 
           DatacenterCharacteristics characteristics, 
           VmAllocationPolicy vmAllocationPolicy,
           List<Storage> storageList, 
           double schedulingInterval) throws Exception {
        super(name, 
                characteristics, 
                vmAllocationPolicy,
                storageList, 
                schedulingInterval);
    }

    /* My own mwthods */

}
不会将cloudlet分配给任何数据中心:


我甚至无法将创建的
数据中心
实例强制转换为
MyDatacenter

,如果您有以下内容:

Datacenter d = new MyDatacanter(...);
d
可以访问的唯一方法是在超类
数据中心
中定义的方法,除非将
d
强制转换为
MyDatacenter
对象:

d.yourCustomMethod(); //won't work
((MyDataCenter) d).yourCustomMethod(); //should work fine

根据对您问题的评论,问题似乎是您重写了方法,如
setCharacteristics(…)
setSchedulingInterval(…)
等,并在超级构造函数中调用这些方法

如果不了解系统正在做什么以及重写这些方法如何影响应用程序的内部工作,就很难给出您可能面临的问题的确切示例。因此,我将尝试提供一个更抽象的例子,并希望我能传达出可能出错的想法

假设我们有以下几类:

class SuperType {
  protected String name;

  public SuperType(String n) {
    setName( n );
  }

  protected void setName( String n ) {
    name = n;
  }    
}

class SubType extends SuperType {
  // setting 'id' happens here
  private int id = new Random().nextInt() + 1;

  {
    // initializer block, setting 'id' could happen here       
  }

  public SubType( String n ) {
    super( n ); 
    // setting 'id' could happen here as well
  }

  @Override
  protected void setName( String n ) {
    name = n + " " + id;
  }    
}
正如您所看到的,
SubType
覆盖了在
SuperType
构造函数中使用的方法
setName(…)
。为什么这是个问题

考虑调用
新子类型(“某个名称”)
时初始化发生的顺序:

  • 构造函数
    子类型(…)
    调用超级构造函数,即
    超级类型(…)
  • 在执行构造函数之前,将创建并初始化实例。
    对于层次结构中的每个类,从上到下(从超级到子类型)的顺序如下
    • 字段按其列出的顺序排列
    • 初始值设定项按其列出的顺序阻塞
    • 建造师
因此,在我们的示例中,我们有以下执行顺序(为了简单起见,我将保留
Object
和不存在的初始化)

  • 超类型(…)
    构造函数(因为没有初始值设定项块)
  • setName(…)
    正在被调用,但这是被覆盖的版本
  • SubType
    字段初始化,将
    id
    设置为随机数
  • 子类型
    初始化程序块运行
  • 子类型(…)
    构造函数运行

如您所见,在初始化
id
之前执行重写的
setName(…)
,因此该方法将看到的所有内容都将是其默认值(0表示基元
int
)。根据可能存在问题的应用程序的不同,重写的方法可能依赖于正确初始化的一些附加变量(例如not null),如果不发生这种情况,实例可能仍然会被创建,但从应用程序的角度来看是不可用的。

这并不能回答问题。这是一个运行时问题;您正在描述一个编译问题。注释不用于扩展讨论;这段对话已经结束。非常感谢@Thomas,是的,我的错误与你所说的几乎相同。我在子类中重写了一个set方法。再次感谢你的解释,我的朋友。
d.yourCustomMethod(); //won't work
((MyDataCenter) d).yourCustomMethod(); //should work fine
class SuperType {
  protected String name;

  public SuperType(String n) {
    setName( n );
  }

  protected void setName( String n ) {
    name = n;
  }    
}

class SubType extends SuperType {
  // setting 'id' happens here
  private int id = new Random().nextInt() + 1;

  {
    // initializer block, setting 'id' could happen here       
  }

  public SubType( String n ) {
    super( n ); 
    // setting 'id' could happen here as well
  }

  @Override
  protected void setName( String n ) {
    name = n + " " + id;
  }    
}