java - 如何返回接口对象?
问题描述
我有接口 Transport 和 2 个类 Cars 和 Motos。我在文件中写了有关某些汽车或摩托的所有详细信息。我怎样才能写一个方法
public static Transport inputTransport(InputStream in)
?我在文件中没有任何关于类类型(汽车或摩托车)的信息。我应该把这个信息写在文件中,然后在它创建新的 Car 或 Moto 并返回它之后,或者我可以写别的东西吗?
public interface Transport {
public void addModel(String model, double price) throws DuplicateModelNameException;
public void deleteModel(String model) throws NoSuchModelNameException;
public void changeModel(String model, String new_model) throws NoSuchModelNameException,DuplicateModelNameException;
public void changePrice(String model, double new_price) throws NoSuchModelNameException;
public void changeType(String type);
public String getType();
public int getNumber();
public String getPrice(String model) throws NoSuchModelNameException;
public String[] getAllModels();
public double[] getAllPrices();
}
class Cars implements Transport{
private String type;
private Models[] cars;
private int number;
Cars(String type, int number){
this.type = type;
this.number = number;
cars = new Models[number];
for (int i = 0; i < number; i++){
cars[i] = new Models("default", Double.NaN);
}
} ...
class Motos implements Transport {
private int count = 0;
private String type;
Motos(String type){
this.type = type;
}...
解决方案
您可以使用 ObjectOutputStream 并将对象保存到文件中,然后使用 ObjectInputStream 导入对象,此 InputStream 现在将是您对象的确切类。
我写了一个小示例程序,应该可以帮助您入门。
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class Main {
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
A a = new A();
B b = new B();
save(a);
C c = load();
System.out.println(c.getClass()); // prints "A"
if (c instanceof A) {
System.out.println("is of class A");
A loaded = (A) c;// you can now use this object
System.out.println(a.someMethod());
} else if (c instanceof B) {
System.out.println("is of class B");
B loaded = (B) c;
} else {
System.out.println("should not occur");
}
}
private static C load() throws IOException, FileNotFoundException, ClassNotFoundException {
ObjectInputStream in = new ObjectInputStream(new FileInputStream("f.txt"));
C s = (C) in.readObject();
in.close();
return s;
}
private static void save(C c) throws FileNotFoundException, IOException {
final FileOutputStream fout = new FileOutputStream("f.txt");
final ObjectOutputStream out = new ObjectOutputStream(fout);
out.writeObject(c);
out.flush();
out.close();
}
}
class A implements C {
// serialVersion is the version of the clas, so if you save the object with
// serialVersion 1 and then change something, change this version to two, so you
// get an error if you are trying to load "obsolete" objects
private static final long serialVersionUID = 1L;
@Override
public String getName() {
return "A";
}
public String someMethod() {
return "someMethod";
}
}
class B implements C {
private static final long serialVersionUID = 1L;
@Override
public String getName() {
return "B";
}
public String someOtherMethod() {
return "someOtherMethod";
}
}
interface C extends Serializable { //Objects saved to a File have to be Serializable
String getName();
}
推荐阅读
- python - 制作 RC4 算法,KSA(密钥调度算法)的问题
- java - 与 JavaFX 控制器相比,如何在 Spring MVC 中正确定义控制器
- python - 如何打印多个函数的返回值?
- c# - 扫描特定区域的像素
- matplotlib - 如何使用networkx将两个图形合二为一?
- java - 这个 Android LiveData/Threading 代码是否有可能给出某种并发问题或意外结果?
- javascript - React 组件正在重新安装任何父状态更改。不知道为什么
- javascript - Android webview - 如何从外部存储中获取文件
- vuejs3 - vue3:显示完整对象很好的奇怪情况,但访问其元素之一返回未定义
- javascript - 更改文本颜色,ini