编程练习 请老师帮忙完善一下代码

编程练习 请老师帮忙完善一下代码

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

}
}
}


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕小脸 2020-12-24 16:36:57

同学你好,经测试运行代码是正确的,棒棒的,继续加油~

祝学习愉快~

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师