首先,我调整您的代码一点点,
1-我创建了一个接口,数据,含有一些随机方法的someMethod():
package main.interfaces;
public interface Data {
int myData = 0;
public void someMethod();
}
2-然后,我创建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);
}
}
类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封装DataFact ORY这里:
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方法内的“flag”变量中。之后,将调用DataHolder方法,之后,您可以检查返回的对象并检查它是否是您之前指定的内容。如果你希望它有点复杂,你可以在DataHolder的构造函数中传递对象类型,并从那里进行检查,我不想仅仅为了简单起见。好运)
那么,你定义的东西的方式不能包含*广告DataOne或DataTwo,因为既不是从数据派生。 – 2014-10-16 15:57:06
如果您需要采用'DataHolder'的方法,则不需要。 (忽略代码片段中缺少'implements',我认为这是一个疏忽。) – 2014-10-16 15:57:07
类声明后面有一些奇怪的'()',我也认为它是错别字。 – aioobe 2014-10-16 16:01:50