面向对象(1.0)
面向对象(1.0)
面向对象学习主线
- Java 类及类的成员(属性/方法/构造器/代码块/内部类)
- 面向对象的三大特征(封装/继承/多态)
- 其他关键字(this/super/static/final/interface/package/import... .. .)
面向对象的说明
/*
* "把大象装入冰箱"
* 1.面向过程:强调的是功能行为 以函数为最小单位 考虑怎么做
*
* ① 把冰箱门打开
* ② 抬起大象 塞进冰箱
* ② 把冰箱门关闭
*
* 2.面向对象:强调具备了功能的对象 以类/对象为最小单位 考虑谁来做
*
* 人 {
* 打开(冰箱) {
* 冰箱.开开();
* }
*
* 抬起(大象) {
* 大象.进入(冰箱);
* }
*
* 关闭(冰箱) {
* 冰箱.闭合();
* }
*
* }
*
* 冰箱 {
* 开开() {}
* 闭合() {}
* }
*
* 大象 {
* 进入(冰箱) {
* }
* }
*/
面向对象两个要素
- 类:对一类事物的描述 是抽象的 是概念上的定义
- 对象:是实际存在的该类事物的个体 因而也称为实例(instance)
- 面向对象程序设计的重点是类的设计(设计类 就是设计类的成员)
类的成员
- 属性(成员变量 ---> field ---> 域 ---> 字段)
- 方法(成员方法 ---> 函数 ---> method)
- 创建类的对象(类的实例化/实例化类)
类和对象的使用(面向对象思想落地的实现)
- 创建类 设计类的成员
- 创建类的对象
- 通过"对象.属性"或"对象.方法"调用对象的结构
- 如果创建了一个类的多个对象 则每个对象都独立的拥有一套类的属性(非 static 的)(eg:修改一个对象的属性"A"则不影响另外一个对象属性"A"的值)
对象的内存解析(浅谈)
- Stack(栈)(线性表结构(eg: 一维数组)):局部变量(声明在方法/方法形参/代码块/构造器形参/构造器内部的变量)
- Heap(堆):对象 数组(new 实例化出的结构)属性/成员变量(直接定义在类的一对{}内 属性其实存在于 实例化的对象内存中 对象存储在堆中 所以属性也存储在堆中)
- Method area(方法区):
- 常量池:String
- 静态域:静态变量
- 类的加载信息
类中属性的使用
/*
* 类中属性的使用
* 属性(成员变量)/局部变量
* 1. 相同点:
* 1.1 定义变量的格式:数据类型 变量名 = 变量值;
* 1.2 先声明 后使用
* 1.3 变量都有其对应的作用域
*
*
* 2. 不同点:
* 2.1 在类中声明的位置的不同
* 属性:直接定义在类的一对"{}"内
* 局部变量:声明在方法/方法形参/代码块/构造器形参/构造器内部的变量
*
* 2.2 权限修饰符的不同
* 属性:可以在声明属性时指明其权限 使用权限修饰符
* 常用的权限修饰符:private/public/protected/缺省 ---> 封装性
* 局部变量:局部变量不可以使用权限修饰符
*
* 2.3 默认初始化值的情况:
* 属性:类的属性根据其类型 都有默认初始化值
* 整型(byte/short/int/long):0
* 浮点型(float/double):0.0
* 字符型(char):0 (或'\u0000')
* 布尔型(boolean):false
*
* 引用数据类型(类/数组/接口):null
*
* 局部变量:没有默认初始化值
* 1. 我们在调用局部变量之前 需要要显式赋值(形参在调用时 我们赋值即可)
*
* 2.4 在内存中加载的位置:
* 属性:加载到堆空间中(非static)
* 局部变量:加载到栈空间
*/
类中方法的声明和使用
/*
* 类中方法的声明和使用
* 方法:描述类应该具有的功能
* 1. Math类:sqrt()\random() \...
* 2. Scanner类:nextXxx() ...
* 3. Arrays类:sort()\binarySearch()\toString()\equals()\... .. .
*
* 1. 举例:
* public void eat() {}
* public void sleep(int hour) {}
* public String getName() {}
* public String getNation(String nation) {}
*
* 2. 方法的声明:
* 权限修饰符 返回值类型 方法名(形参列表) {
* 方法体
* }
* 注意:static/final/abstract 修饰的方法后续再述... .. .
*
* 3. 说明:
* 3.1 关于权限修饰符:默认方法的权限修饰符先都使用 public
* 3.2 Java 规定的4种权限修饰符:public/protected/private/缺省... .. .封装性后续再述... .. .
*
* 3.3 返回值类型的分类:有返回值/无返回值
* 3.3.1 如果方法有返回值 则必须在方法声明时指定返回值的类型 同时方法中需要使用 return 关键字来返回指定类型的变量或常量(return 数据)如果方法没有返回值 则方法声明时 使用
* void 关键字来表示 通常没有返回值的方法中 就不需要使用 return 关键字 但是如果使用的话 只能"return;"表示结束此方法... .. .
*
* 3.3.2 我们定义方法该不该有返回值
* 1. 题目要求
* 2. 具体问题具体分析(靠经验分析)
*
* 3.4 方法名的命名:方法名属于标识符 需要遵循标识符的规则和规范 需要见名知意
* 3.5 方法的形参使用: 方法可以声明0个或1个或者多个形参
* 3.5.1 格式:(数据类型1 形参1,数据类型2 形参2)
* 3.5.2 我们定义方法时该不该定义形参
* 1. 题目要求
* 2. 具体问题具体分析(靠经验分析)
*
* 3.6 方法体的作用:方法体是方法功能的体现
*
* 4. return 关键字的使用:
* 1. 使用范围:使用在方法体中
* 2. 作用:结束方法(针对于有返回值类型的方法 使用"return 数据"返回数据)
* 3. 注意点:return 关键字后面不可以声明执行语句
*
* 5. 方法的调用:方法在使用中可以调用当前类的属性或当前方法(方法A中又调用了方法A ---> 递归方法)(方法中不可以定义方法... .. .)
*/
对象数组
定义: 对象的集合 自定义类类型的数组 数组元素中实例化对象(类类型属于引用类型数组 引用数据类型数组元素中引用对象(实例化对象))
public class StudentTest {
public static void main(String[] args) {
// 声明 Student 类型的数组(类类型属于引用类型数组)
Student[] stus = new Student[20]; //String[] arr = new String[10];
// 遍历数组元素
for (int i = 0;i < stus.length;i++) {
// 数组元素实例化对象
stus[i] = new Student();
// 对象属性赋值(String 对象)
stus[i].number = (i + 1);
// 年级:[1,6]
stus[i].state = (int)(Math.random() * (6 - 1 + 1) + 1);
// 成绩:[0,100]
stus[i].score = (int)(Math.random() * (100 - 0 + 1));
}
// 遍历学生数组
for (int i = 0;i <stus.length;i++) {
System.out.println(stus[i].info());
}
System.out.println("---------------------------------------------------------------------------------");
// 打印出3年级的学生信息
for (int i = 0;i <stus.length;i++) {
if (stus[i].state == 3) {
System.out.println(stus[i].info());
}
}
System.out.println("---------------------------------------------------------------------------------");
// 使用冒泡排序按学生成绩排序并遍历所有学生信息
for (int i = 0;i < stus.length - 1;i++) {
for (int j = 0;j < stus.length - 1 - i;j++) {
if (stus[j].score > stus[j + 1].score) {
// 如果需要换序 交换的是数组的元素(交换的是 Student 对象 而不是去交换两个对象属性(学生的成绩))
Student temp = stus[j];
stus[j] = stus[j + 1];
stus[j + 1] = temp;
}
}
}
//遍历学生数组
for (int i = 0;i <stus.length;i++) {
System.out.println(stus[i].info());
}
System.out.println("---------------------------------------------------------------------------------");
}
}
class Student {
int number;// 学号
int state;// 年级
int score;// 成绩
// 显示学生信息的方法
public String info(){
return "学号:" + number + "\t年级:" + state + "\t成绩:" + score
}
}
改进... .. .
package pers.twenty_four.demo.main;
public class StudentTest {
public static void main(String[] args) {
StudentTest studentMain = new StudentTest();
// 创建类类型的数组
Student[] stus = new Student[20];
// 数组元素实例化对象
studentMain.ArrayIndexNewObject(stus);
// 遍历数组(学生信息)
studentMain.print(stus);
// 打印3年级的学生信息
studentMain.searchStateInfo(stus);
// 使用冒泡排序比哪里学生信息(根据学生成绩)
studentMain.bubbleSort(stus);
studentMain.print(stus);
}
/**
*
* @Description 数组元素实例化对象
* @author Twenty_four
* date 2021年3月31日下午3:14:18
* @param stus
*/
public void ArrayIndexNewObject(Student[] stus) {
// 遍历数组元素
for (int i = 0; i < stus.length; i++) {
// 数组元素实例化对象
stus[i] = new Student();
// Student 对象属性赋值
stus[i].number = i + 1;
// 随机班级(1 ~ 9)
stus[i].state = (int)(Math.random() * 8 + 1);
// 随机成绩[0,100]
stus[i].score = (int)(Math.random() * 101 + 0);
}
}
/**
*
* @Description 遍历数组(学生信息)
* @author Twenty_four
* date 2021年3月30日下午10:21:43
*/
public void print(Student[] stus) {
for (int i = 0; i < stus.length; i++) {
System.out.println("学号:" + stus[i].number + "\t班级:" + stus[i].state + "\t成绩:" + stus[i].score);
}
System.out.println("------------------------------------------------------------------------------------------");
}
/**
*
* @Description 打印3年级的学生信息
* @author Twenty_four
* date 2021年3月30日下午10:23:38
* @param stus
*/
public void searchStateInfo(Student[] stus) {
System.out.println("班级 ---> 3");
for (int i = 0; i < stus.length; i++) {
if (stus[i].state == 3) {
System.out.println("学号:" + stus[i].number + "\t成绩:" + stus[i].score);
}
}
System.out.println("------------------------------------------------------------------------------------------");
}
/**
*
* @Description 使用冒泡排序比哪里学生信息(根据学生成绩)
* @author Twenty_four
* date 2021年3月30日下午10:24:54
* @param stus
*/
public void bubbleSort(Student[] stus) {
/*
* n 个学生需要比较比较 n - 1 轮
* 每轮需要比较 arr.length - 1 - i 次
*/
for (int i = 0; i < stus.length - 1; i++) {
for (int j = 0; j < stus.length - 1 - i; j++) {
if (stus[j].score > stus[j + 1].score) {
Student temp = stus[j];
stus[j] = stus[j + 1];
stus[j + 1] = temp;
}
}
}
}
}
// Class ---> Student
package pers.twenty_four.demo.main;
public class Student {
// 学号
int number;
// 年级
int state;
// 成绩
int score;
}
浙公网安备 33010602011771号