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

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

Product

1
package com.lding.io;<br><br>import java.io.Serializable;<br><br>public class Product implements Serializable {<br>    private int id;<br>    private String name;<br>    private String categories;<br>    private double price;<br>    public Product(){<br><br>    }<br><br>    public Product(int id, String name, String categories, double price) {<br>        this.id = id;<br>        this.name = name;<br>        this.categories = categories;<br>        this.price = price;<br>    }<br><br>    public int getId() {<br>        return id;<br>    }<br><br>    public void setId(int id) {<br>        this.id = id;<br>    }<br><br>    public String getName() {<br>        return name;<br>    }<br><br>    public void setName(String name) {<br>        this.name = name;<br>    }<br><br>    public String getCategories() {<br>        return categories;<br>    }<br><br>    public void setCategories(String categories) {<br>        this.categories = categories;<br>    }<br><br>    public double getPrice() {<br>        return price;<br>    }<br><br>    public void setPrice(double price) {<br>        this.price = price;<br>    }<br><br>    @Override<br>    public String toString() {<br>        return "产品ID:" + id +<br>                "\n产品名称:" + name  +<br>                "\n产品属性:" + categories +<br>                "\n产品价格:" + price +<br>                "元\n";<br>    }<br>}<br>

ProductTest

1
package com.lding.io;<br><br>import java.io.*;<br><br>public class ProductTest {<br>    public static void main(String[] args) {<br>        Product iphone= new Product(123,"iphone","telephone",4888.0);<br>        Product ipad=new Product(234,"ipad","computer",5088.0);<br>        Product macbook=new Product(345,"macbook","computer",10688.0);<br>        Product iwatch=new Product(256,"iwatch","watch",4799.0);<br>        FileInputStream fis=null;<br>        ObjectInputStream ois=null;<br>        FileOutputStream fos=null;<br>        ObjectOutputStream oos=null;<br>        try {<br>             fis=new FileInputStream("lding.txt");<br>             ois=new ObjectInputStream(fis);<br><br>             fos=new FileOutputStream("lding.txt");<br>             oos=new ObjectOutputStream(fos);<br><br>            oos.writeObject(iphone);<br>            oos.writeObject(ipad);<br>            oos.writeObject(macbook);<br>            oos.writeObject(iwatch);<br>            oos.flush();<br><br>            System.out.println("apple系列产品信息");<br>            Product temp=(Product) ois.readObject();<br>            System.out.println(temp);<br>            temp=(Product) ois.readObject();<br>            System.out.println(temp);<br>            temp=(Product) ois.readObject();<br>            System.out.println(temp);<br>            temp=(Product) ois.readObject();<br>            System.out.println(temp);<br><br>        } catch (FileNotFoundException e) {<br>            e.printStackTrace();<br>        } catch (IOException e) {<br>            e.printStackTrace();<br>        } catch (ClassNotFoundException e) {<br>            e.printStackTrace();<br>        } finally {<br>            try {<br>                fis.close();<br>                fos.close();<br>                oos.close();<br>                ois.close();<br>            } catch (IOException e) {<br>                e.printStackTrace();<br>            }<br><br>        }<br>    }<br>}<br>


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

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

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

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

祝学习愉快~

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

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

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

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

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