今日内容
一:面向对象就是:解决问题的时候,找别人,不用自己完成;
二:封装:将零散的数据,整合成一个整体;
三:类
-
格式:class 类名称{ }
-
举例:
//定义一个Stdunt类 class Student{}
-
自定义数据类型,内部数据怎处理?
-
内部数据采用变量来表示(不需要赋初始化值)
-
格式:数据类型 变量名;
-
举例:
举例: class Student{ int id; String name; }
-
-
使用方法
-
初始化
-
格式:类名 变量名 = new 类名 ();
-
举例:
Student S1 = new Student(); Student S2 = new Student(); class Student{ int id; String name; }
-
-
赋值
-
赋值
-
格式:对象名.内部变量名 = 数值;
-
举例:
Student S1 = new Student(); S1.id = 1; S1.name = "哥哥"; class Student{ int id; String name; }
-
-
-
取值
-
直接取值
-
格式:System.out.println(对象名.内部变量名);
-
举例:
//格式:System.out.println(对象名.内部变量名); Student S1 = new Student(); S1.id = 1; S1.name = "哥哥"; System.out.println(S1.id); class Student{ int id; String name; }
-
-
赋值取值
-
格式:变量 = 对象名.内部变量名;
-
举例:
//格式:变量 = 对象名.内部变量名; Student S1 = new Student(); S1.id = 1; S1.name = "哥哥"; int a = S1.id; String b = S2.name; System.out.println(a); System.out.println(b); class Student{ int id; String name; }
-
-
-
四:类和数组的关系
-
数组存对象
-
格式:类名[ ] 数组名 = new 类名[长度];
-
举例:
//类名[ ] 数组名 = new 类名[长度]; Student[] arr = new Student[5]; arr[0] = S1; arr[1] = S2; arr[2] = S3; class Student{ int id; String name; }
-
-
数组取对象
-
格式:System.out.println(数组名[索引].内部变量名);
-
举例:
//类名[ ] 数组名 = new 类名[长度]; Student[] arr = new Student[5]; arr[0] = S1; arr[1] = S2; arr[2] = S3; System.out.println(arr[0].id); System.out.println(arr[0].name); class Student{ int id; String name; }
-