编程练习 请老师帮忙完善一下代码
Product
package com.lding.io;
import java.io.Serializable;
public class Product implements Serializable {
private int id;
private String name;
private String categories;
private double price;
public Product(){
}
public Product(int id, String name, String categories, double price) {
this.id = id;
this.name = name;
this.categories = categories;
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCategories() {
return categories;
}
public void setCategories(String categories) {
this.categories = categories;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "产品ID:" + id +
"\n产品名称:" + name +
"\n产品属性:" + categories +
"\n产品价格:" + price +
"元\n";
}
}
ProductTest
package com.lding.io;
import java.io.*;
public class ProductTest {
public static void main(String[] args) {
Product iphone= new Product(123,"iphone","telephone",4888.0);
Product ipad=new Product(234,"ipad","computer",5088.0);
Product macbook=new Product(345,"macbook","computer",10688.0);
Product iwatch=new Product(256,"iwatch","watch",4799.0);
FileInputStream fis=null;
ObjectInputStream ois=null;
FileOutputStream fos=null;
ObjectOutputStream oos=null;
try {
fis=new FileInputStream("lding.txt");
ois=new ObjectInputStream(fis);
fos=new FileOutputStream("lding.txt");
oos=new ObjectOutputStream(fos);
oos.writeObject(iphone);
oos.writeObject(ipad);
oos.writeObject(macbook);
oos.writeObject(iwatch);
oos.flush();
System.out.println("apple系列产品信息");
Product temp=(Product) ois.readObject();
System.out.println(temp);
temp=(Product) ois.readObject();
System.out.println(temp);
temp=(Product) ois.readObject();
System.out.println(temp);
temp=(Product) ois.readObject();
System.out.println(temp);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
fis.close();
fos.close();
oos.close();
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
10
收起
正在回答 回答被采纳积分+1
java工程师2020版
- 参与学习 人
- 提交作业 9393 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星