编程练习 请老师帮忙完善
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> |
15
收起
正在回答 回答被采纳积分+1
2回答
java工程师2020版
- 参与学习 人
- 提交作业 9402 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧