该程序将无法使用子类而不是超类
The program won't work using subclass instead of superclass
有一个名为 Datacenter
的 class,其中构造函数是:
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());
}
我们使用此 class 在程序中创建数据中心:
private static Datacenter createDatacenter(String name, LinkedList myHarddriveList, double timeZone) {
/* Additional codes like defining Hots */
Datacenter datacenter = null;
try {
datacenter = new Datacenter(name, characteristics,
new VmAllocationPolicySimple(hostList), myHarddriveList, 0);
} catch (Exception e) {
System.out.println("Error: " + e);
}
return datacenter;
}
运行程序的结果是这样的:
问题是,如果我通过扩展 Datacenter
class 来定义自己的数据中心,程序将无法运行。我定义 MyDatacenter
class 如下:
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 */
}
因此,如果我将 createDatacenter()
方法的 return 类型从 Datacenter
更改为 MyDatacenter
,程序将无法运行。
private static MyDatacenter createDatacenter(String name, LinkedList myHarddriveList, double timeZone) {
/* No changes */
MyDatacenter datacenter = null;
try {
datacenter = new MyDatacenter(name, characteristics,
new VmAllocationPolicySimple(hostList), myHarddriveList, 0);
} catch (Exception e) {
System.out.println("Error: " + e);
}
return datacenter;
}
不会将 cloudlet 分配给任何数据中心:
我什至无法将创建的 Datacenter
实例转换为 MyDatacenter
。
当您遇到类似情况时:
Datacenter d = new MyDatacanter(...);
唯一可由 d
访问的方法是在超类 Datacenter
中定义的方法,除非您将 d
转换为 MyDatacenter
对象:
d.yourCustomMethod(); //won't work
((MyDataCenter) d).yourCustomMethod(); //should work fine
根据对您问题的评论,问题似乎是您重写了 setCharacteristics(...)
、setSchedulingInterval(...)
等方法并在超级构造函数中调用了这些方法。
如果不了解更多关于您的系统正在做什么以及覆盖这些方法如何影响您的应用程序的内部工作,就很难给出您可能面临的问题的确切示例。因此,我将尝试提供一个更抽象的示例,并希望我能传达出可能出错的想法。
假设我们有以下 classes:
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(...)
。为什么这是个问题?
考虑当我们调用 new SubType("some name")
:
时初始化发生的顺序
- 构造函数
SubType(...)
调用超构造函数,即SuperType(...)
- 在执行构造函数之前,将创建并初始化实例。
层次结构中的每个 class 从上到下(从超类型到子类型)按以下顺序发生
- 字段按它们列出的顺序排列
- 初始化块按照它们列出的顺序排列
- 构造函数
因此我们的示例中有以下执行顺序(为简单起见,我将保留 Object
和不存在的初始化)
SuperType(...)
构造函数(因为没有初始化块)
setName(...)
正在被调用,但这是被覆盖的版本
SubType
字段被初始化,设置 id
为随机数
SubType
初始化块 运行
SubType(...)
构造函数运行s
如您所见,覆盖的 setName(...)
在 id
初始化之前执行,因此该方法将看到的所有内容都将是其默认值(原始 int
为 0)。并且取决于您的应用程序可能是问题 - 覆盖的方法可能依赖于一些额外的变量被正确初始化(例如不为空),如果没有发生,实例可能仍然被创建但不能从您的应用程序中使用观点。
有一个名为 Datacenter
的 class,其中构造函数是:
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());
}
我们使用此 class 在程序中创建数据中心:
private static Datacenter createDatacenter(String name, LinkedList myHarddriveList, double timeZone) {
/* Additional codes like defining Hots */
Datacenter datacenter = null;
try {
datacenter = new Datacenter(name, characteristics,
new VmAllocationPolicySimple(hostList), myHarddriveList, 0);
} catch (Exception e) {
System.out.println("Error: " + e);
}
return datacenter;
}
运行程序的结果是这样的:
问题是,如果我通过扩展 Datacenter
class 来定义自己的数据中心,程序将无法运行。我定义 MyDatacenter
class 如下:
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 */
}
因此,如果我将 createDatacenter()
方法的 return 类型从 Datacenter
更改为 MyDatacenter
,程序将无法运行。
private static MyDatacenter createDatacenter(String name, LinkedList myHarddriveList, double timeZone) {
/* No changes */
MyDatacenter datacenter = null;
try {
datacenter = new MyDatacenter(name, characteristics,
new VmAllocationPolicySimple(hostList), myHarddriveList, 0);
} catch (Exception e) {
System.out.println("Error: " + e);
}
return datacenter;
}
不会将 cloudlet 分配给任何数据中心:
我什至无法将创建的 Datacenter
实例转换为 MyDatacenter
。
当您遇到类似情况时:
Datacenter d = new MyDatacanter(...);
唯一可由 d
访问的方法是在超类 Datacenter
中定义的方法,除非您将 d
转换为 MyDatacenter
对象:
d.yourCustomMethod(); //won't work
((MyDataCenter) d).yourCustomMethod(); //should work fine
根据对您问题的评论,问题似乎是您重写了 setCharacteristics(...)
、setSchedulingInterval(...)
等方法并在超级构造函数中调用了这些方法。
如果不了解更多关于您的系统正在做什么以及覆盖这些方法如何影响您的应用程序的内部工作,就很难给出您可能面临的问题的确切示例。因此,我将尝试提供一个更抽象的示例,并希望我能传达出可能出错的想法。
假设我们有以下 classes:
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(...)
。为什么这是个问题?
考虑当我们调用 new SubType("some name")
:
- 构造函数
SubType(...)
调用超构造函数,即SuperType(...)
- 在执行构造函数之前,将创建并初始化实例。
层次结构中的每个 class 从上到下(从超类型到子类型)按以下顺序发生- 字段按它们列出的顺序排列
- 初始化块按照它们列出的顺序排列
- 构造函数
因此我们的示例中有以下执行顺序(为简单起见,我将保留 Object
和不存在的初始化)
SuperType(...)
构造函数(因为没有初始化块)setName(...)
正在被调用,但这是被覆盖的版本SubType
字段被初始化,设置id
为随机数SubType
初始化块 运行SubType(...)
构造函数运行s
如您所见,覆盖的 setName(...)
在 id
初始化之前执行,因此该方法将看到的所有内容都将是其默认值(原始 int
为 0)。并且取决于您的应用程序可能是问题 - 覆盖的方法可能依赖于一些额外的变量被正确初始化(例如不为空),如果没有发生,实例可能仍然被创建但不能从您的应用程序中使用观点。