老师,作业求批改点评:
来源:5-3 自由编程
孬帮手慕小菜
2020-01-01 19:55:07
Test类:
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.util.ArrayList;
import java.util.List;
import mod.AppleGoods;
public class Test {
public static void main(String[] args) {
AppleGoods ag1 = new AppleGoods(123, "Iphone", "telephone", 4888.0);
AppleGoods ag2 = new AppleGoods(234, "IPad", "computer", 5088.0);
AppleGoods ag3 = new AppleGoods(345, "MacBook", "computer", 10688.0);
AppleGoods ag4 = new AppleGoods(256, "AppleWatch", "watch", 4799.0);
try {
FileOutputStream fos = new FileOutputStream("F://IO//test//IO.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
FileInputStream fis = new FileInputStream("F://IO//test//IO.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
//写数据
List<AppleGoods> list = new ArrayList<AppleGoods>();//创建一个存放AppleGoods的数组
//向数组内添加所有的产品信息
list.add(ag1);
list.add(ag2);
list.add(ag3);
list.add(ag4);
//增强型for循环写入
for(AppleGoods i:list){
oos.writeObject(i);
}
oos.flush();//清空缓冲区
System.out.println("写入完成");
//读数据
for(int i=0;i<list.size();i++){
try {
AppleGoods ags = (AppleGoods)ois.readObject();
System.out.println(ags);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//关闭流
fos.close();
oos.close();
fis.close();
ois.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
}Goods类:
import java.io.Serializable;
public class AppleGoods implements Serializable{
private int GoodsID;
private String GoodsName;
private String GoodsProperty;
private Double GoodsPrice;
/**
* 苹果系产品类
* @param goodsID 产品id
* @param goodsName 产品名
* @param goodsProperty 产品属性
* @param goodsPrice 产品价格
*/
public AppleGoods(int goodsID, String goodsName, String goodsProperty, Double goodsPrice) {
super();
this.GoodsID = goodsID;
this.GoodsName = goodsName;
this.GoodsProperty = goodsProperty;
this.GoodsPrice = goodsPrice;
}
/**
*
* @return
*/
public int getGoodsID() {
return GoodsID;
}
public void setGoodsID(int goodsID) {
this.GoodsID = goodsID;
}
public String getGoodsName() {
return GoodsName;
}
public void setGoodsName(String goodsName) {
this.GoodsName = goodsName;
}
public String getGoodsProperty() {
return GoodsProperty;
}
public void setGoodsProperty(String goodsProperty) {
this.GoodsProperty = goodsProperty;
}
public Double getGoodsPrice() {
return GoodsPrice;
}
public void setGoodsPrice(Double goodsPrice) {
this.GoodsPrice = goodsPrice;
}
/**
*用于显示信息的tostring方法
*/
@Override
public String toString() {
return "苹果系产品 [产品编号:" + GoodsID + ", 产品名:" + GoodsName + ", 产品属性:" + GoodsProperty
+ ", 产品价格:" + GoodsPrice +"元"+"]";
}
}老师,临放假,期末考试,由于众所周知的原因我已经很久没有学这个了,现在感觉前面的知识有点模糊,我是应该继续向下学习遇到不会的再回去看呢还是找本书先快速看看前面的内容?或者老师您有什么更好的建议呢?
1回答
同学你好,1. 当变量名由一个单词组成时,则该单词均小写。当由多个单词组成时,第一个单词所有字母均小写,从第二个单词开始,每个单词的首字母大写。如:AppleGoods类中的GoodsID应改为:goodsId
2. 建议在AppleGoods类创建无参构造方法,如果只定义带参构造但未定义无参构造,则此时使用无参构造创建对象,会找不到无参构造导致报错。
3. 对于之前学的知识,要常常复习,温故知新。建议同学不要着急,比照课程目录,将自己薄弱的地方多看几遍,可以挑重点,快进等等。不要跳步贪多,夯实基础,一步一个脚印反而是进步最快的!学习过程中,可以先看一遍视频,然后第二遍跟着老师写代码,第三遍再看一下,查漏补缺。代码一定要多写,这样才能牢固掌握知识。总之,同学不要着急,一定要多加练习,有问题在问答区提问,老师们会帮助解决。
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
相似问题