Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何返回接口对象?_Java_Interface_Inputstream - Fatal编程技术网

Java 如何返回接口对象?

Java 如何返回接口对象?,java,interface,inputstream,Java,Interface,Inputstream,我有接口运输和2类汽车和摩托车。我正在写一些汽车或摩托车的详细资料。如何编写方法 公共静态交通输入传输(输入流输入)?我的档案中没有关于课程类型(汽车或摩托车)的任何信息。我应该在文件中写下这些信息,然后创建新车或摩托车并返回,还是我可以写其他东西 public interface Transport { public void addModel(String model, double price) throws DuplicateModelNameException; pub

我有接口运输和2类汽车和摩托车。我正在写一些汽车或摩托车的详细资料。如何编写方法
公共静态交通输入传输(输入流输入)
?我的档案中没有关于课程类型(汽车或摩托车)的任何信息。我应该在文件中写下这些信息,然后创建新车或摩托车并返回,还是我可以写其他东西

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();
} 

您可以使用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();
}

我不能使用对象。。。我只能将DataInputStream用于此程序。

class Motos implements Transport {
    private int count = 0;
    private String type;

    Motos(String type){
        this.type = type;
    }...
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();
}