1 packagecom.wh.Object;2 3 public classGoods {4 PrivateString name;5 Private Doubleprice ;6 Private intno;7 8 publicString getName () {9 returnname;Ten } one a public voidsetName (String Name) { - this. Name =name; - } the - public Doublegetprice () { - returnprice ; - } + - public voidSetprice (Doubleprice ) { + this. Price =price ; a } at - public intGetno () { - returnno; - } - - public voidSetno (intNo) { in this. No =no; - } to}
packagecom.wh.Object;Importjava.util.ArrayList;/*** list is used in conjunction with JavaBean *@authorwang, P.C. * @Date September 18, 2016 * @Description how the list collection is transformed into a javabean object, and a common use * method*/ public classTestgoods { public Static voidmain (string[] Args) {//classes and objects when object-orientedGoods d1=NewGoods (); Goods D2=NewGoods (); Goods D3=NewGoods (); Goods D4=NewGoods (); D1.setname ("decepticons"); D2.setname (oh, Megatron.); D3.setname (Optimus Prime); D4.setname ("spider-man");//Assign ValueD1.setprice (10); D2.setprice (20); D3.setprice (30); D4.setprice (40); D1.setno (1); D2.setno (2); D3.setno (3); D4.setno (4); String name=d1.getname ();//accept the value returned by the object D1 DoublePrice=D1.getprice (); intno=D1.getno (); System.out.println ("name" +name+ "unit price" +price+ "number" +no); //Some common methods of list collectionArrayList list=NewArrayList (); //three ways to add//boolean add (int index, E element)//Boolean AddAll (index,collection)//add01 directly add object, i.e. List.add (object obj)List.add (d1); //ADD02 The specified location to add an object,//that is, list.add (int index,object Obj) index must start at 0,//There's no fault in the middleList.add (1, d2); //to add a collection to another collection,//that is List.addall (int index,arraylist list2)ArrayList list2=NewArrayList (); List2.add (d3); List.addall (2, list2); //deletes the element at the specified position//Boolean Remove (int index)??? Incorrect return valueList.remove (2); //modifies the element at the specified position//set (int index, E element)List.addall (2, list2); List.add (3, d1); List.set (3, d4); //Enquiry//Get (int Index) returns the element at the specified position in the list/*sublist (intfromIndex,intToindex)*returns the FromIndex specified in the list (including)*and Toindex (not Included) are part of the Element. */ for(inti = 0; I < list.size (); I++) {//set Length size ()System.out.println (i+ "\t\t" +List.get (i)); } //1. List Collection add entity and output (type conversion required)Goods obj; for(inti = 0; I < list.size (); i++) {obj=(Goods) List.get (i); System.out.println (i+ "\t\t" +obj.getname () + "\ t" +obj.getprice () + "\ t" +Obj.getno ()); } }}
The application between list and class, that is, with JavaBean