我有一个名为Data
的空接口,它由类DataOne
和DataTwo
实现。
然后我有一个名为DataHolder
的类,其中包含一个Data
对象。
它看起来像这样:
public class DataHolder() {
public Data data;
}
public class DataOne() {
public int importantData;
public int getImportantData() {
return importantData;
}
public int setImportantData(int importantData) {
this.importantData = importantData;
}
}
public class DataTwo() {
public int notSoImportantData;
}
假设有一个函数,它接受一个DataHolder
对象并对importantData整数进行一些操作。
public void calculateImportantData(DataHolder dh) {
int importantData = 1234567890;
dh.data.setImportantData(importantData);
}
如何在不进行类型转换的情况下确保DataHolder
包含DataOne
个对象?
答案 0 :(得分:3)
怎么样:
public class DataHolder<T extends Data> {
public T data;
}
在您的代码中,您将拥有:
public void calculateImportantData(DataHolder<DataOne> dh) {
int importantData = 1234567890;
dh.data.setImportantData(importantData);
}
我认为你的意思是DataOne和DataTwo来实现数据。
答案 1 :(得分:0)
首先,我稍微调整了你的代码,
1-我创建了一个Interface,Data,包含一些随机方法someMethod():
package main.interfaces;
public interface Data {
int myData = 0;
public void someMethod();
}
2-然后,我创建了两个名为DataOne和DataTwo的类: 类DataOne :(注意我在这里添加了重要的业务方法setImportantData(),这提供了你工作的总封装。
package main;
import main.interfaces.Data;
public class DataOne implements Data{
public int importantData;
public int getImportantData() {
return importantData;
}
public void setImportantData(int importantData) {
this.importantData = importantData;
}
@Override
public void someMethod() {
System.out.println("here in DataOne!... ");
}
public void calculateImportantData(int importantData) {
// int importantData = 1234567890;
setImportantData(importantData);
}
}
Class DataTwo:
package main;
import main.interfaces.Data;
public class DataTwo implements Data{
public int notSoImportantData;
@Override
public void someMethod() {
System.out.println("here in DataTwo!...");
}
public void calculateUsualData(DataTwo d2) {
d2.someMethod();
}
}
之后,使用工厂设计模式 ...我创建了这个DataFactory类:
package main.factory;
import main.DataOne;
import main.DataTwo;
import main.interfaces.Data;
public class DataFactory {
public static Data getData(String dataType){
if(dataType == null){
return null;
}
if(dataType.equalsIgnoreCase("DATAONE")){
return new DataOne();
} else if(dataType.equalsIgnoreCase("DATATWO")){
return new DataTwo();
}
return null;
}
}
现在,回到你的问题解决方案,我使用DataHolder,在这里封装DataFactory:
package main.holder;
import main.factory.DataFactory;
import main.interfaces.Data;
public class DataHolder {
Data data;
public DataHolder(String dataType){
data = DataFactory.getData(dataType);
}
public Data getData(){
return data;
}
}
现在,尝试运行该应用程序,我添加了一些将出现在您的控制台上的评论,我希望它们会有所帮助:)
package main.run;
import main.DataOne;
import main.DataTwo;
import main.holder.DataHolder;
import main.interfaces.Data;
public class main {
public static void main(String[] args) {
// lets assume user of the method passed a DataOne Object, you can
// manage it by changing the value of flag string
String flag = "DataOne";
DataHolder dataHolder = new DataHolder(flag);
if (dataHolder.getData() instanceof DataOne) {
System.out
.println("you have a DataOne object , but a Data reference");
System.out
.println("/nso , you need to create a 'reference' to DataOne to work on that object ...");
} else if (dataHolder.getData() instanceof DataTwo) {
System.out
.println("you have a DataTwo object , but a Data reference");
} else {
System.out
.println("you dont have a DataOne nor DataTwo references , it is a "
+ dataHolder.getData().getClass() + " object!");
}
System.out
.println("in order for the compiler to pass the following test , you must cast he RHS ( right hand side ) to match the LHS (left hand side)");
// in order for the compiler to pass the following test , you must cast
// the RHS ( right hand side ) to match the LHS (left hand side)
DataOne d1 = (DataOne) dataHolder.getData();
// in case you wanted to test DataTwo scenario
//DataTwo d2 = (DataTwo) dataHolder.getData();
System.out.println("if you didnt do that , you can make it a Data Object , but you will not be able to access the method 'getImportantData()' created in DataOne");
Data data = dataHolder.getData();
}
}
(注意,这里的程序结构是:你在启动应用程序之前选择数据的类型,存储在main方法中的&#34; flag&#34;变量之后。之后,调用DataHolder方法在那之后,你可以检查返回的对象并检查它是否是你之前指定的。如果你想要它有点复杂,你可以在DataHolder的构造函数中传递对象类型,并且从那里检查,我只是为了简单而不想这样做。祝你好运。