经测试,成员变量的初始化顺序是,1.定义时候给的值-》2.构造函数中赋值。 //学习了类和继承,以及 派生类的构造以及 方法覆盖。 public static void Study_class() { //goods基类是否应该是一个虚类.其实并不是固定的.看场景,如果把goods基类看作现实物品的基类,那么就应该是虚类.因为不存在一个概念的物品. //但如果把goods 看作是某物品的基本信息类,那么可以是普通类.代表一个物品的基础信息. //下面就是作为基类创建. Listmygoods=new ArrayList (); goods book_cshart=new goods(0, "c#", 3.1f, enum_goods_type.book); goods book_javaGoods=new goods(1, "java", 4.5f, enum_goods_type.book); goods fruit_apple=new goods(3,"apple",1f,enum_goods_type.fruit); goods fruit_banana=new goods(4,"banana",0.9f,enum_goods_type.fruit); mygoods.add(book_cshart); mygoods.add(book_javaGoods); mygoods.add(fruit_apple); mygoods.add(fruit_banana); for(int i=0;i mygoods2=new ArrayList (); mygoods2.add(book_cshart); mygoods2.add(book_javaGoods); mygoods2.add(fruit_apple); mygoods2.add(fruit_banana); for(int i=0;i
基类:
package com.linson;import java.text.DecimalFormat;enum enum_goods_type{ book, fruit;}public class goods { public int id; public String name; public float price; public enum_goods_type gtype; public goods(int _id,String _name,float _price,enum_goods_type _type) { id=_id; name=_name; price=_price; gtype=_type; } public String MyBaseInfo() { DecimalFormat dt=new DecimalFormat(); dt.applyLocalizedPattern("0.00"); String priceformat=dt.format(price); return "ID:"+id+". Name:"+name+". price:"+priceformat+". type:"+gtype.name(); } public String MySpecialInfo() { return ""; }}
book
package com.linson;import java.util.Date;public class Book extends goods{ public String writer; public Date publicDate; public Book(int _id,String _name,float _price,String _writer,Date _publicdate) { super(_id, _name, _price, enum_goods_type.book); writer=_writer; publicDate=_publicdate; } @Override public String MySpecialInfo() { String retString=MyBaseInfo(); retString=retString+" .Writer:"+writer+"."; return retString; }}
fruit:
package com.linson;public class Fruit extends goods{ public String Color; public Fruit(int _id,String _name,float _price,String _color) { super(_id, _name, _price,com.linson.enum_goods_type.fruit); Color=_color; } @Override public String MySpecialInfo() { String retString=MyBaseInfo(); retString=retString+" .Color:"+Color+"."; return retString; }}