Introduction of today's content
1. Definition and use of custom types
2. Memory diagram of custom class
3. Basic functions of ArrayList set
4. Random register case and inventory case code optimization
01 Reference Data Type _ class
* A: 数据类型 * a: java中的数据类型分为:基本类型和引用类型* B: 引用类型的分类 * a: Java为我们提供好的类,比如说:Scanner,Random等。 * b: 我们自己创建的类,按照类的定义标准,可以在类中包含多个方法与属性,来供我们使用。
02 Overview of Custom Classes
* A: 自定义类的概述 * java代码映射成现实事物的过程就是定义类的过程。 * 举例: 我们就拿一部手机进行分析,它能用来做什么呢?它可以打电话,上网,聊等, 这些就是手机所提供的功能,也就是方法;手机也有它的特征,如颜色、尺寸大小、品牌型号等, 这些就是手机的特征,也就是属性 * 目前,我们只关注类中的属性,类中的方法在面向对象部分再进行学习。
03 Formatting of custom Classes
* A: 自定义类的格式 * a: 使用类的形式,对现实中的事物进行描述。 * b: 事物由方法和属性两部分组成。 * 方法: 这个事物具备的功能。 * 属性: 这个事物具备的特征。 * c: 格式 public class 类名{ 属性定义 修饰符 数据类型 变量名 = 值 方法定义 修饰符 返回值类型 方法名(参数列表){ } }
04 Custom Phone Class
* A: 自定义的手机类 * a: 案例代码 public class Phone{ /* 定义手机的属性 */ String color ; String brand ; double size ; }
05 Test Phone Class
* A: 调用方法执行流程 * a: 实现引用类型的步骤 * 1: 导入包 , 类都是在同一个文件夹,不需要导入包 * 2: 创建引用类型的变量 * 3: 变量.类型中的功能 * b: 案例代码 public class TestPhone{ public static void main(String[] args){ // 2: 创建引用类型的变量 Phone p = new Phone(); //System.out.println(p); //输出内存的地址 //3: 变量.类型中的功能 //变量 p.的方式,调用类中的属性 //属性就是变量 , 赋值和获取值 p.color = "土豪金"; p.brand = "爱立信"; p.size = 5.0; //获取属性值 System.out.println(p.color+" "+p.brand+" "+p.size); } }
06 Memory diagram of the custom class _1
* A: 自定义类的内存图_1
07 Memory diagram of the custom class _2
* A: 自定义类的内存图_1
82 reference type variable memory graph
* A: 自定义类的内存图_1*
09 Exercises for custom Classes
* A: Code for the entity class/* rice cooker, including attributes (brand, capacity size, color, etc.) define class, describe things, rice cooker properties: brand, size, color definition class, class name, Rice cooker Class scope, define three properties */public class dianfanguo{//define three properties String brand; Double size; String color; }/* car, including attributes (branding, displacement, type, etc.) define class, class name car property brand Displacement type */public class car{//define car three properties String brand; Double Pailiang; String type; }/* students, including attributes (name, age, gender, etc.) define class, class name Student three properties: Name, age, Gender (char) */public class student{ String name; int age; char sex; }* B: Test Class Code/* Test class defined at the same time test, rice cooker, car, student */public class test{public static void Main (string[] A RGS) {//Create Rice Cooker Reference type Dianfanguo DFG = new Dianfanguo (); Dfg.brand = "Tesla"; Dfg.color = "Red"; Dfg.size = 30; System.out.println (dfg.brand+ "" +dfg.color+ "" +dfg.size "); Create car reference type car c = new car (); C.brand = "giant Force"; C.type = "Tractor"; C.pailiang = 0.5; System.out.println (c.brand+ "" +c.type+ "" +c.pailiang "); Create student reference type Student stu = new Student (); Stu.name = "Zhang San"; Stu.age = 20; Stu.sex = ' Male '; System.out.println (stu.name+ "" +stu.age+ "" +stu.sex "); } }
10ArrayList steps to create a variable
* A: ArrayList创建变量的步骤 * a: 导入包 java.util包中 * b: 创建引用类型的变量 数据类型< 集合存储的数据类型> 变量名 = new 数据类型<集合存储的数据类型>(); 集合存储的数据类型: 要将数据存储到集合的容器中 创建集合引用变量的时候,必须要指定好,存储的类型是什么 * c: 变量名.方法 注意: 集合存储的数据,8个基本类型对应8个引用类型 存储引用类型,不存储基本类型
11ArrayList Creating variable examples
* A: ArrayList创建变量的示例代码 import java.util.ArrayList; public class ArrayListDemo{ public static void main(String[] args){ //创建集合容器,指定存储的数据类型 //存储字符串 ArrayList<String> array = new ArrayList<String>(); //创建集合容器,存储整数 ArrayList<Integer> array2 = new ArrayList<Integer>(); //创建集合容器,存储手机类型 ArrayList<Phone> array3 = new ArrayList<Phone>(); } }
Common methods of 12ArrayList
* Common Methods of A:arraylist * A:ADD (parameters) add elements to the collection * b:get (int index) take the elements in the collection, the parameters of the Get method, write the index * C:SIZE () returns the length of the collection, set Number of stored elements * B: Case code import java.util.ArrayList; public class arraylistdemo_1{public static void Main (string[] args) {//define collection, store string element arraylis t<string> array = new arraylist<string> (); Call the collection method add storage element Array.add ("abc"); Array.add ("Itcast"); Array.add ("Love"); Array.add ("Java"); The length of the output collection, calling the collection method size, the return value type of the size method int int size = Array.size (); SYSTEM.OUT.PRINTLN (size); Gets an element in the collection, gets the 1 indexed element//collection method get, gets the element after the result data type String s = array.get (1); System.out.println (s); System.out.println (array.get (0)); System.out.println (Array.get (1)); System.out.println (Array.get (2)); System.out.println (Array.get (3)); } }
Traversal of the 13ArrayList collection
* A: 案例代码 /* 集合的遍历 实现思想也是索引思想 集合的索引从0开始,到 size()-1 方法get(int index) */ import java.util.ArrayList; public class ArrayListDemo_2{ public static void main(String[] args){ ArrayList<Integer> array = new ArrayList<Integer>(); array.add(121); array.add(125); array.add(123); array.add(120); array.add(128); //对集合进行遍历 //使用方法 size+get组合进行遍历 for(int i = 0 ; i < array.size(); i++){ System.out.println( array.get(i) ); } } }
14ArrayList Supplemental Methods
* A: ArrayList补充方法 * a: add(int 索引,存储的元素) 将元素添加到指定的索引上 * b: set(int 索引,修改后的元素) 将指定索引的元素,进行修改 * c: remove(int 索引) 删除指定索引上的元素 * d: clear() 清空集合中的所有元素* B: 案例代码 import java.util.ArrayList; public class ArrayListDemo_3{ public static void main(String[] args){ ArrayList<Integer> array = new ArrayList<Integer>(); array.add(1); array.add(2); array.add(3); array.add(4); //在索引2上,添加元素7 array.add(2,7); //将0索引上的元素,修改成10 array.set(0,10); //将4索引上的元素,删除 array.remove(4); array.clear(); //使用方法 size+get组合进行遍历 for(int i = 0 ; i < array.size(); i++){ System.out.println( array.get(i) ); } } }
15 Random name Register case study
* A: 随机点名器案例分析 全班同学中随机的找出一名同学,打印这名同学的个人信息。 我们对本案例进行分析,得出如下分析结果: 1.存储全班同学信息(姓名、年龄) 将容器换成集合,集合中存的是Student类型 2.打印全班同学每一个人的信息(姓名、年龄) 遍历集合 3.在班级总人数范围内,随机产生一个随机数,查找该随机数所对应的同学信息 (姓名、年龄) 随机点名器明确地分为了三个功能。如果将多个独立功能的代码写到一起,则代码相对冗长 ,我们可以针对不同的功能可以将其封装到一个方法中,将完整独立的功能分离出来。 而在存储同学姓名时,如果对每一个同学都定义一个变量进行姓名存储, 则会出现过多孤立的变量,很难一次性将全部数据持有。 此时,我们采用ArrayList集合来解决多个学生信息的存储问题
16 Random Name Register code implementation
* A: Random Register case code///random register, set improvement (student's name and age) In reality there are students in this thing, using the form of defining classes, describing student things attributes: Name, age name stores an array, Set string[] s = {"", ""}; Collection, is the name of the student stored? Should store student type store student: Student type, store in Collection Overview: Iterate collection random: Random number, as index, find element three functions in the collection, shared data, collection container, Define three methods, must pass set of parameters */import java.util.ArrayList; Import Java.util.Random; public class callname{public static void Main (string[] args) {//define collection, store studentname type variable ARR Aylist <StudentName> array = new arraylist<studentname> (); Call Add method (array); Call Traversal collection printarraylist (array); Randomstudentname (array); }/* Random number, as the index of the collection, to find the element in the collection */public static void Randomstudentname (ARRAYLIST<STUDENTNAME&G T Array) {Random r = new Random (); int number = R.nextint (Array.size ()); Random number, index, get Stud in the collectionEntname s = array.get (number); System.out.println (S.name + "" +s.age); }/* Overview Student's information, traverse Collection */public static void Printarraylist (Arraylist<studentname> ; Array) {for (int i = 0; i < array.size (); i++) {///storage collection, collection. Add (SN1) SN1 is a studentname type variable Gets the collection when the. Get method, gets out what, or studentname type variable studentname s = array.get (i); System.out.println (s.name+ "" +s.age); }}/* Define method, implement store student's name and age create studentname type variable, store in collection */public static void Add (arraylist<studentname> array) {//Create Studentname type variable studentname SN1 = new Studentnam E (); Studentname SN2 = new Studentname (); Studentname sn3 = new Studentname (); Studentname sn4 = new Studentname (); Studentname sn5 = new Studentname (); Sn1.name = "Zhang 31"; sn1.age = 201; Sn2.name = "Zhang 32"; Sn2.age = 202; Sn3.name = "Zhang 33"; Sn3.age = 203; Sn4.name = "Zhang 34"; Sn4.age = 204; Sn5.name = "Zhang 35"; Sn5.age = 205; The studentname variable is stored in the collection Array.add (SN1); Array.add (SN2); Array.add (SN3); Array.add (SN4); Array.add (SN5); } }
17 Inventory Case Analysis Join the collection
* A: 库存案例分析加入集合 * a: 参见\day06\day06(面向对象\day06_source\对象内存图.JPG
18 Stock Cases Add product information
* A: Case Code/* definition,. Description Commodity 4 Properties Commodity name size price inventory String double Double int Define class, class name Goods variables of this type, stored in the collection */public class goods{//define commodity name String brand; Size property double size; Price attribute double prices; inventory attribute int count; }/* Implement inventory Management case: 1. Store commodity Information Store commodity type variable, store the variable of the commodity type in the collection *///import Java.util.Arra Ylist; Import java.util.*; public class shopp{public static void Main (string[] args) {//Create ArrayList collection, store goods type Arrayli st<goods> array = new arraylist<goods> (); Call method Addgoods (array) to add the product information; }/* Defines the method that stores the information of the commodity in the collection as shared data for all methods, parameter passing */public static void Addgoods ( Arraylist<goods> array) {//Create a variant of the Goods type for the commodity type variable Goods g1 = new Goods (); Goods g2 = new Goods (); G1.brand = "MacBook"; G1.size = 13.3; G1.price = 9999.99; G1.count = 3; G2.brand = "Thinkpad"; G2.size = 15.6; G2.price = 7999.99; G2.count = 1; A variable of type goods, stored in the collection Array.add (G1); Array.add (G2); } }
19 Stock Case View inventory List
* A: Case Code/* Implement Inventory Management case: 1. Store commodity Information Store commodity type variables, stores the variables of the commodity type into the collection 2. Check Look at the inventory list to iterate through the collection, get the goods type variables stored in the collection to output each goods type of attribute calculation sum: Total inventory, total amount *///import Java.ut Il. ArrayList; Import java.util.*; public class shopp{public static void Main (string[] args) {//Create ArrayList collection, store goods type Arrayli st<goods> array = new arraylist<goods> (); Call method Addgoods (array) to add the product information; }/* Define method, view inventory list, Traverse Collection * * public static void Printstore (arraylist<goods> array) { Output header System.out.println ("----------Mall Stock List----------"); SYSTEM.OUT.PRINTLN ("Brand model size price Stock Number"); Define variables, save total stock count, and total amount int totalcount = 0; Double Totalmoney = 0; Traversal set for (int i = 0; i < array.size (); i++) {//get (index) Gets the elements in the collection, stores the goods class, and gets the gooDS Type//use Goods type variable, accept get method result Goods g = array.get (i); System.out.println (g.brand+ "" +g.size+ "" +g.price+ "" +g.count "); TotalCount = Totalcount+g.count; Totalmoney = Totalmoney + g.count*g.price; } System.out.println ("Total number of stocks:" +totalcount); SYSTEM.OUT.PRINTLN ("Total Commodity inventory Amount:" +totalmoney); }/* Defines the method that stores the information of the commodity in the collection as shared data for all methods, parameter passing */public static void Addgoods ( Arraylist<goods> array) {//Create a variant of the Goods type for the commodity type variable Goods g1 = new Goods (); Goods g2 = new Goods (); G1.brand = "MacBook"; G1.size = 13.3; G1.price = 9999.99; G1.count = 3; G2.brand = "Thinkpad"; G2.size = 15.6; G2.price = 7999.99; G2.count = 1; A variable of type goods, stored in the collection Array.add (G1); Array.add (G2); } }
20 Inventory Case Modify inventory and test code implementation
* A: Case Code/* Implement Inventory Management case: 1. Store commodity Information Store commodity type variables, stores the variables of the commodity type into the collection 2. Check Look at the inventory list to iterate through the collection, get the goods type variables stored in the collection to output each goods type of attribute calculation sum: Total inventory, total amount 3. Modify the inventory of the product Collection traversal, gets the property of the goods type variable that is stored in the collection called the Goods class's attribute count, the value is modified (keyboard input) *///import java.util.ArrayList; Import java.util.*; public class shopp{public static void Main (string[] args) {//Create ArrayList collection, store goods type Arrayli st<goods> array = new arraylist<goods> (); Call method Addgoods (array) to add the product information; Enter the Dead Loop while (true) {//Call the Select function method, get to the user input function ordinal int number = Choosefunction (); Judgment on the serial number, if = 1 Enter view inventory function = 2 Go to modify inventory function =3 End switch (number) {Case 1: Go to view inventory, call the method to view inventory, pass the collection Printstore (array) that stores the commodity information; Break Case 2://Enter modify inventory function, call the method to modify inventory, pass the collection update (array); Break Case 3:return; Default:System.out.println ("Without this function"); Break }}}/* method definition, modify the input of the inventory keyboard, change the attribute value in the goods, modify the */public static void upd Ate (arraylist<goods> array) {Scanner sc = new Scanner (system.in); Iterates through the collection, getting each element in the collection for (int i = 0; I < array.size (); i++) {//collection method get gets the element of the collection, the element type Goods Goods g = array.get (i); System.out.println ("Please enter the" +g.brand+ "in Stock Number"); Goods property, Count is modified G.count = Sc.nextint (); }}/* Defines the method, implements the selection menu, the user selects the menu according to function */public static int choosefunction () {Syst EM.OUT.PRINTLN ("-------------Inventory Management------------"); System.out.println ("1. View inventory list"); System.out.println ("2. Change the quantity of goods in stock"); System.out.println ("3. Exit"); System.out.println ("Please enter the number of actions to be performed:"); Scanner sc = new Scanner (system.in); int number = Sc.nextint (); return number; }/* Define method, view inventory list, Traverse Collection */public static void Printstore (arraylist<goods> array {//Output header System.out.println ("----------Store stock list----------"); SYSTEM.OUT.PRINTLN ("Brand model size price Stock Number"); Define variables, save total stock count, and total amount int totalcount = 0; Double Totalmoney = 0; Traversal set for (int i = 0; i < array.size (); i++) {//get (index) Gets the elements in the collection, stores the goods class, and gets the goods type Using the Goods type variable, accept the result of the Get method Goods g = Array.get (i); System.out.println (g.brand+ "" +g.size+ "" +g.price+ "" +g.count "); TotalCount = Totalcount+g.count; Totalmoney = Totalmoney + g.count*g.price; } System.out.println ("Total number of stocks:" +totalcount); SYSTEM.OUT.PRINTLN ("Total Commodity inventory Amount:" +totalmoney); }/* Defines the method that stores the information of the commodity in the collection as shared data for all methods, parameter passing */public static void Addgoods ( Arraylist<goods> array) {//Create a variant of the Goods type for the commodity type variable Goods g1 = new Goods (); Goods g2 = new Goods (); G1.brand = "MacBook"; G1.size = 13.3; G1.price = 9999.99; G1.count = 3; G2.brand = "Thinkpad"; G2.size = 15.6; G2.price = 7999.99; G2.count = 1; A variable of type goods, stored in the collection Array.add (G1); Array.add (G2); } }
06_java Base Syntax _ 6th day (custom class, ArrayList collection) _ Handouts