3.15 恢复施工🚧

  • 晚风依旧很温柔,一个人慢慢走

    一、导论

    1.何谓“面向对象”的编程思想?首先解释一下“思想”

    先问你个问题:你想做个怎样的人?
    可能你会回答:我想做个好人,孝敬父母,尊重长辈,关爱亲朋……
    你看,这就是思想。这是你做人的思想,或者说,是你做人的原则。
    做人有做人的原则,编程也有编程的原则。这些编程的原则呢,就是编程思想。

    2.面向过程(POP)和面向对象(OOP)

    二者都是一种思想,面向对象是相对于面向过程而言的。面向过程,强调的是功能行为,以函数为最小单位,考虑怎么做。面向对象,将功能封装进对象,强调具备了功能的对象,以类/对象为最小单位,考虑谁来做。
    eg.如何把大象放冰箱里?
    >面向过程:1.把冰箱门打开 2.把大象抬起来塞冰箱去 3.关上冰箱门
    >面向对象: ```java 人{ 打开(冰箱){
    1. 冰箱.开开();
    } 抬起(大象){
    1. 大象.进入(冰箱);
    } 关闭(冰箱){
    1. 冰箱.闭合();
    } }

冰箱{ 开开(){} 闭合(){} }

大象{ 进入(冰箱){} }

  1. <a name="aBseT"></a>
  2. ### 3.面向对象的两个要素
  3. >类:对一类事物的描述,是抽象的,概念上的定义<br />>对象:实际存在的该类事物的每个个体,因而也称为实例<br />>名言警句:“万事万物皆对象。”<br />>原理图:<br />![](https://cdn.nlark.com/yuque/0/2022/jpeg/25472905/1647586830807-0f5fea79-c5e0-41e1-ab7d-bdb5ff48034a.jpeg)
  4. <a name="SRvtf"></a>
  5. ### 4.面向对象的三个特性
  6. >封装性<br />>继承性<br />>多态性
  7. <a name="YDiiR"></a>
  8. ### 5.小结
  9. 1.面向对象思想编程内容的三条主线分别是什么<br />①类及类的成员:属性、方法、构造器、代码块、内部类<br />②面向对象的三大特征:封装、继承、多态<br />2.谈谈你对面向对象中类和对象的理解,并指出二者的关系?<br />类:抽象的、概念上的内容<br />对象:实实在在存在的一个个体。<br />3.面向对象思想落地实现的规则一<br />*1.仓建类,设计类的成员<br />*2.创建类的对象<br />*3.通过“对象.属性”或“对象.方法”调用对象的结构
  10. <a name="Ag9DW"></a>
  11. ## 二、类、对象、属性、方法
  12. <a name="md51F"></a>
  13. ### 1.类中属性与方法及类和对象的创建
  14. ```java
  15. /*
  16. *一、设计类其实就是设计类的成员
  17. *属性 = 成员变量 = field = 域、字段
  18. *方法 = 成员方法 = 函数 = method
  19. *创建类的对象 = 类的实例化 = 实例化类
  20. *二、类和对象的使用(面向对象思想落地的实现)
  21. *1.创建类,设计类的成员
  22. *2.创建类的对象
  23. *3.通过“对象﹒属性或"对象.方法"调用对象的结构
  24. *三、体会类与多个对象的关系
  25. *如果创建了一个类的多个对象,则每个对象都独立的拥有一套类的属性。(非static的)
  26. *意味着:如果我们修改一个对象的属性a,则不影响另外一个对象属性a的值。
  27. *特殊的:Person p3 = p1; 理解为指向同一个对象会发生相互影响
  28. */
  29. // 测试类
  30. public class Test {
  31. public static void main(String[] args) {
  32. // 创建Person类对象
  33. Person p1 = new Person();
  34. // 调用对象结构:属性、方法
  35. // 调用属性:对象.属性
  36. p1.age = 18;
  37. p1.name = "嘉然";
  38. p1.isMale = false;
  39. // 调用方法
  40. p1.eat();
  41. p1.sleep();
  42. p1.talk("中文");
  43. }
  44. }
  45. // 设计人类
  46. class Person{
  47. // 属性
  48. String name;
  49. int age;
  50. boolean isMale;
  51. // 方法
  52. public void eat(){
  53. System.out.println(name + "可以吃饭");
  54. }
  55. public void sleep(){
  56. System.out.println(name + "可以睡觉");
  57. }
  58. public void talk(String language){
  59. System.out.println(name + "可以说话,使用" + language);
  60. }
  61. }

2.体会类和多个对象的关系

  1. /*
  2. *>体会类与多个对象的关系:
  3. *如果创建了一个类的多个对象,则每个对象都独立的拥有一套类的属性。(非static的)
  4. *意味着:如果我们修改一个对象的属性a,则不影响另外一个对象属性a的值。
  5. *特殊的:Person p3 = p1; 理解为指向同一个对象会发生相互影响
  6. */
  7. // 测试类
  8. public class Test {
  9. public static void main(String[] args) {
  10. // 创建Person类对象
  11. Person p1 = new Person();
  12. // 调用对象结构:属性、方法
  13. // 调用属性:对象.属性
  14. p1.age = 18;
  15. p1.name = "嘉然";
  16. p1.isMale = false;
  17. // 调用方法
  18. p1.eat();
  19. p1.sleep();
  20. p1.talk("中文");
  21. // 创建第二个Person类对象 **************
  22. Person p2 = new Person();
  23. System.out.println(p2.name); // 输出不是嘉然 而是null 表明对象之间互不影响
  24. // 将p1变量保存的对象地址赋给p3使p3同时指向一个对象实体 **************
  25. Person p3 = p1;
  26. System.out.println(p3.name); // 输出嘉然
  27. }
  28. }
  29. // 设计人类
  30. class Person{
  31. // 属性
  32. String name;
  33. int age;
  34. boolean isMale;
  35. // 方法
  36. public void eat(){
  37. System.out.println(name + "可以吃饭");
  38. }
  39. public void sleep(){
  40. System.out.println(name + "可以睡觉");
  41. }
  42. public void talk(String language){
  43. System.out.println(name + "可以说话,使用" + language);
  44. }
  45. }

3.对象的内存解析

三:面向对象(上)(2022.3.19 施工完毕✅) - 图1

4.属性与局部变量的对比

  1. /*
  2. *属性(成员变量) VS 局部变量
  3. *相同点:
  4. *>1. 定义变量格式:数据类型 变量名 = 变量值
  5. *>2.先声明,后使用
  6. *>3.变量都有其作用域
  7. *不同点:
  8. *>1.声明位置不同:
  9. * 属性:直接定义在类的一对{ }里
  10. * 局部变量:声明在方法内、方法形参、代码块内、构造器形参、构造器内部的变量
  11. *>2.关于权限修饰符的不同
  12. * 属性:可以在声明属性时,指明其权限
  13. * 常用的权限修饰符:private、public、缺省、protected --->封装性 再说
  14. * 局部变量:不可以加
  15. *>3.默认初始化值
  16. * 类的属性根据数据类型都有默认的初始化值
  17. * 而局部变量可没有
  18. *>4.在内存中的加载位置
  19. * 属性加载到堆空间中(非static)
  20. * 局部变量加载到堆空间
  21. */
  22. // 测试类
  23. public class Test {
  24. public static void main(String[] args) {
  25. }
  26. }
  27. class User{
  28. // 属性或成员变量
  29. String name;
  30. int age;
  31. boolean isMale;
  32. public static void talk(String language) { // language 形参 也是局部变量
  33. System.out.println("使用" + language + "语言");
  34. }
  35. public static void eat(){
  36. String food = "烧饼"; // 局部变量
  37. System.out.println("嘉然爱吃" + food);
  38. }
  39. }

5.类中方法的声明和使用

  1. /*
  2. * 方法:描述类该具有的功能
  3. * 比如:
  4. * Math类:sqrt() \ random() ...
  5. * Scanner类:nextXxx() ...
  6. * Arrays:sort() \ binarySearch() \ toString() \ equals() ...
  7. *
  8. * 1.举例
  9. * public void eat(){}
  10. * public void sleep(int hour){}
  11. * public String getName(){}
  12. * public String getNation(String nation){}
  13. * 2.方法的声明
  14. * 权限修饰符 返回值类型 方法名(形参列表){
  15. * 方法体
  16. * }
  17. * 注意:static、final、abstract 来修饰方法 以后再说
  18. * 3.说明
  19. * >权限修饰符:private、public、缺省、protected
  20. * >有返回值的记得别忘了返回值类型,void没有返回值的话只能写return;可以直接结束方法
  21. *
  22. *4.return关键字的使用,
  23. *>使用范围:使用在方法体中
  24. *>作用:结束方法针对于有返回值类型的方法,使用"return数据"方法返回所要的数据。
  25. *>注意点:return关键字后面不可以声明执行语句。
  26. *
  27. *5.方法的使用中,可以调用当前类的属性或方法
  28. * 特殊的:方法A中又调用了方法A:递归方法。
  29. *
  30. */
  31. public class Test {
  32. }
  33. class Customer{
  34. // 属性
  35. String name;
  36. int age;
  37. boolean isMale;
  38. // 方法
  39. public void eat(){
  40. System.out.println("客户吃饭");
  41. return;
  42. System.out.println("此处无法到达"); // 前方为return; 无法到达对应3.2
  43. }
  44. public void sleep(int hour) {
  45. System.out.println("睡了" + hour + "小时");
  46. }
  47. public String getName(){ // 注意此处返回值设置将 void 改为 String
  48. return name;
  49. }
  50. public String getNation(String nation) {
  51. String info = "国籍是" + nation;
  52. return info;
  53. }
  54. }

6.练习1-类的设计

要求:
(1)创建Perlson类的对象,设置该对象的name.age和sex属性,调用study方法,输出字符串
“studying”,调用showAge()方法显示age值,调用addAge()方法给对象的age属性值增加2岁。
(2)创建第二个对象,执行上述操作,体会同一个类的不同对象之间的关系。

  1. /**
  2. * Test
  3. */
  4. public class Test {
  5. public static void main(String[] args) {
  6. Person p1 = new Person();
  7. p1.name = "嘉然";
  8. p1.age = 3;
  9. p1.sex = "female";
  10. p1.study();
  11. p1.showAge();
  12. p1.addAge();
  13. p1.showAge();
  14. // 完全独立互不影响
  15. Person p2 = new Person();
  16. p2.name = "珈乐";
  17. p2.age = 20;
  18. p2.sex = "female";
  19. p2.study();
  20. p2.showAge();
  21. p2.addAge();
  22. p2.showAge();
  23. }
  24. }
  25. class Person{
  26. String name;
  27. int age;
  28. String sex;
  29. public void study() {
  30. System.out.println("studying");
  31. }
  32. public void showAge() {
  33. System.out.println(age);
  34. }
  35. public void addAge() {
  36. age += 2;
  37. }
  38. }

7.练习2-算圆的面积类

利用面向对象的编程方法,设计类Cirlcle计算圆的面积

  1. /**
  2. * Test
  3. */
  4. public class Test {
  5. public static void main(String[] args) {
  6. Circle c1 = new Circle();
  7. c1.r = 3;
  8. double area = c1.getArea();
  9. System.out.println(area);
  10. }
  11. }
  12. class Circle{
  13. double r;
  14. public double getArea(){
  15. return Math.PI * r * r;
  16. }
  17. }

8.练习3-对象数组*

对象数组题目:
定义类Student,包含三个属性:学号number(int),年级state(int),成绩
score(int)。创建20个学生对象,学号为1到20,年级和成绩都由随机数确定。
问题一:打印出3年级(state值为3)的学生信息。
,问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息
提示:
1)生成随机数: Math.random(),返回值类型double;
2)四舍五入取整:Math.round(double d),返回值类型long.

  1. public class Test{
  2. public static void main(String[] args) {
  3. // 声明Student数组
  4. Student[] students = new Student[20];
  5. // 为每个元素赋值
  6. for(int i = 0;i < students.length;i++){
  7. students[i] = new Student();
  8. students[i].number = i + 1;
  9. // [1,6]
  10. students[i].state = (int)(Math.random()*(6-1+1)+1);
  11. // [0,100]
  12. students[i].score = (int)(Math.random()*(100-0+1)+0);
  13. }
  14. // 问题一:带条件的循环遍历
  15. for(int i = 0;i < students.length;i++){
  16. if(students[i].state == 3){
  17. System.out.println("学号:"+ students[i].number + "年纪:" + students[i].state + "分数:"+ students[i].score);
  18. }
  19. }
  20. // 问题二:冒泡排序
  21. Student temp;
  22. for(int i = 0;i < students.length - 1;i++){
  23. for(int j = 0;j < students.length - 1 - i;j++){
  24. if(students[j].score < students[j+1].score){
  25. temp = students[j];
  26. students[j] = students[j+1];
  27. students[j+1] = temp;
  28. }
  29. }
  30. }
  31. for(int i = 0;i < students.length;i++){
  32. System.out.println("学号:"+ students[i].number + "年纪:" + students[i].state + "分数:"+ students[i].score);
  33. }
  34. }
  35. }
  36. class Student{
  37. int number;
  38. int state;
  39. int score;
  40. }

9.对象数组的内存解析

三:面向对象(上)(2022.3.19 施工完毕✅) - 图2

10.匿名对象

  1. /*
  2. 匿名对象的使用
  3. 1.理解:我们创建的对象,没有显式的赋给一个变量名。即为匿名对象
  4. 2.特征:匿名对象只能调用一次。
  5. */
  6. public class Test {
  7. public static void main(String[] args) {
  8. new Phone.price = 1999;
  9. new Phone.showPrice(); // 0 互不影响 用后即销毁
  10. }
  11. }
  12. class Phone{
  13. int price;
  14. public double showPrice(){
  15. System.out.println(price);
  16. }
  17. }

11.自定义数组工具类

  1. /**
  2. * Test
  3. */
  4. public class Test {
  5. public static void main(String[] args) {
  6. int[] arr = new int[]{1,2,3};
  7. ArrayUtil arraytool = new ArrayUtil();
  8. arraytool.getReversal(arr);
  9. }
  10. }
  11. class ArrayUtil{
  12. public void getMax(int[] arr){
  13. // 求数组的最大值
  14. int max = arr[0];
  15. for(int i = 1;i < arr.length;i++){
  16. if(max < arr[i]){
  17. max = arr[i];
  18. }
  19. }
  20. System.out.println(max);
  21. }
  22. public void getMin(int[] arr){
  23. // 求数组的最小值
  24. int min = arr[0];
  25. for(int i = 1;i < arr.length;i++){
  26. if(min > arr[i]){
  27. min = arr[i];
  28. }
  29. }
  30. System.out.println(min);
  31. }
  32. public void getSum(int[] arr){
  33. // 求数组的总和
  34. int sum = arr[0];
  35. for(int i = 1;i < arr.length;i++){
  36. sum += arr[i];
  37. }
  38. System.out.println(sum);
  39. }
  40. public void getAver(int[] arr){
  41. // 求平均值
  42. int sum = arr[0];
  43. for(int i = 1;i < arr.length;i++){
  44. sum += arr[i];
  45. }
  46. int aver = sum / arr.length;
  47. System.out.println(aver);
  48. }
  49. public void getReversal(int[] arr){
  50. // 反转数组
  51. for(int i = 0, j = arr.length-1;i < j;i++, j--){
  52. int temp = arr[i];
  53. arr[i] = arr[j];
  54. arr[j] = temp;
  55. }
  56. for(int i = 0;i < arr.length;i++){
  57. System.out.println(arr[i]);
  58. }
  59. }
  60. public int[] getCopy(int[] arr){
  61. // 创建备份数组,且长度相等
  62. int[] beifen = new int[arr.length];
  63. // 遍历原数组并赋给备份数组
  64. for(int i = 0;i < arr.length;i++){
  65. beifen[i] = arr[i];
  66. }
  67. // 遍历备份数组
  68. for(int i = 0;i < beifen.length;i++){
  69. System.out.print(beifen[i]);
  70. }
  71. return beifen;
  72. }
  73. public void getOrder(int[] arr){
  74. // 数组排序
  75. for(int i = 0;i < arr.length - 1;i++){
  76. for(int j = 0;j < arr.length - 1 -i;j++){
  77. if(arr[j] > arr[j+1]){
  78. int temp = arr[j];
  79. arr[j] = arr[j+1];
  80. arr[j+1] = temp;
  81. }
  82. }
  83. }
  84. for(int i = 0;i < arr.length;i++){
  85. System.out.println(arr[i]);
  86. }
  87. }
  88. public void getSerch(int[] arr, int desitination){
  89. // 查找指定元素
  90. for(int i = 0;i < arr.length;i++){
  91. if(desitination == arr[i]){
  92. System.out.println("找到了索引为" + i);
  93. }
  94. }
  95. }
  96. }

12.方法的重载

  1. /**
  2. * 方法的重载(overload)
  3. * 定义:在同一个类中,允许存在一个以上的同名方法,只要它们的参数个数或者参数类型不同即可。
  4. */
  5. public class Test{
  6. public static void main(String[] args) {
  7. String name = "嘉然";
  8. String behavior = "喂我";
  9. Eat chi = new Eat();
  10. chi.eatApple(name, behavior);
  11. }
  12. }
  13. class Eat{
  14. public void eatApple(String name){
  15. System.out.println(name + "吃苹果");
  16. }
  17. public void eatApple(String name, String behavior){
  18. System.out.println(name + behavior +"吃苹果");
  19. }
  20. }

13.新特性:可变个数的形参方法

  1. /**
  2. * 具体使用:
  3. * 2.1 可变个数形参的格式:数据类型...变量名
  4. * 2.2当调用可变个数形参的方法时,传入的参数个数可以是:0个,1个,2个,。。。
  5. * 2.3 可变个数形参的方法与本类中方法名相同,形参不同的方法之间构成重载
  6. * 2.4 可变个数形参的方法与本类中方法名相同,形参类型也相同的数组之间不构成重载。
  7. * 2.5 可变个数形参在方法的形参中,必须声明在末尾 ***
  8. * 2.6 可变个数形参在方法的形参中,最多只能声明一个可变形参。***
  9. */
  10. public class Test{
  11. public static void main(String[] args) {
  12. Mutable test = new Mutable();
  13. test.show("嘉", "然", "我", "真", "的", "好", "喜", "欢", "你", "啊");
  14. }
  15. }
  16. class Mutable{
  17. public void show(String... str){
  18. System.out.println("可变参数测试用例");
  19. for(int i = 0;i < str.length;i++){
  20. System.out.println(str[i]);
  21. }
  22. }
  23. }

三、底层小机制(bushi

1.理解变量的赋值

  1. /*
  2. *
  3. *
  4. * 关于变量的赋值
  5. *
  6. *
  7. */
  8. public class Test{
  9. public static void main(String[] args) {
  10. int m = 10;
  11. int n = m;
  12. System.out.println("m = "+ m +",n = "+ n); //m = 10,n = 10
  13. n = 20;
  14. System.out.println("m = "+ m +",n = "+ n); //m = 10,n = 20
  15. //以上说明*基本数据类型*变量赋值并不是地址传递,而是实实在在的存在两份
  16. Order o1 = new Order();
  17. o1.Orderid = 1001;
  18. Order o2 = o1;
  19. System.out.println("o1.orderid = " + o1.Orderid + ",o2.orderid = " + o2.Orderid);//o1.orderid = 1001,o2.orderid = 1001
  20. o2.Orderid = 1002;
  21. System.out.println("o1.orderid = " + o1.Orderid + ",o2.orderid = " + o2.Orderid);//o1.orderid = 1002,o2.orderid = 1002
  22. //以上说明*引用数据类型*变量赋值是*地址传递*,指向堆中的同一个对象实体
  23. }
  24. }
  25. class Order{
  26. int Orderid;
  27. }

2.值传递机制

值传递机制:
如果参数是基本数据类型,此时实参赋给形参的是实参真实存储的数据值。如果参数是引用数据类型,此时实参赋给形参的是实参存储数据的地址值。

3.值传递机制练习

  1. /*
  2. * 尝试从堆和栈分析分析
  3. */
  4. public class Test{
  5. public static void main( String args[]) {
  6. Test test = new Test();
  7. test.first();
  8. }
  9. public void first(){
  10. int i = 5;
  11. Value v = new Value();
  12. v.i = 25;
  13. second(v, i);
  14. System.out.println(v.i);
  15. }
  16. public void second(Value v, int i){
  17. i = 0;
  18. v.i = 20;
  19. Value val = new Value();
  20. v = val;
  21. System.out.println(v.i + " " + i);
  22. }
  23. }
  24. class Value{
  25. int i = 15;
  26. }

三:面向对象(上)(2022.3.19 施工完毕✅) - 图3
emmmm…大概就是这样…理解一下

4.递归大练习

  1. /*
  2. * 递归
  3. */
  4. public class Test{
  5. public static void main( String args[]) {
  6. //计算1-100的和
  7. //法1
  8. int sum = 0;
  9. for(int i = 1;i <= 100;i++){
  10. sum += i;
  11. }
  12. System.out.println(sum);
  13. //法2
  14. Test test = new Test();
  15. int sum1 = test.getSum(100);
  16. System.out.println(sum1);
  17. //已知有一个数列:f(0) = 1,f(1) = 4,f(n+2)=2*f(n+1) + f(n) 求f(10)
  18. int f10 = test.getFunction(10);
  19. System.out.println(f10);
  20. // 1 1 2 3 5 8 13 求斐波那契的第7个数
  21. int num = test.getFibonacii(7);
  22. System.out.println(num);
  23. }
  24. public int getSum(int n){
  25. if(n == 1){
  26. return 1;
  27. }else{
  28. return n + getSum(n - 1);
  29. }
  30. }
  31. public int getFunction(int n){
  32. if(n == 0){
  33. return 1;
  34. }else if(n == 1){
  35. return 4;
  36. }else{
  37. return 2 * getFunction(n-1) + getFunction(n-2);
  38. }
  39. }
  40. public int getFibonacii(int n){
  41. if(n == 1){
  42. return 1;
  43. }else if(n == 2){
  44. return 1;
  45. }else{
  46. return getFibonacii(n - 1) + getFibonacii(n-2);
  47. }
  48. }
  49. }

四、封装性

1.封装性引入

为什么要封装?
用洗衣机不用了解内部结构,用就行
开小车不用了解发动机结构,开就行
>程序设计追求“高内聚,低耦合”
高内聚:类的内部数据操作细节自己完成,不允许外部干涉。
低耦合:仅对外暴露少量的方法用于使用。
>作用和意义
隐藏对象内部的复杂性,只对外公开简单的接口。便于外界调用,从而提高系统的可扩展性、可维护性。通俗的说,把该隐藏的隐藏起来,该暴露的暴露出来。这就是封装性的设计思想。

2.封装性的体现

```java /*

  • 面向对象的特征一:
  • 封装与隐藏
  • 一。问题的引入:
  • 当我们创建一个类的对象以后,我们可以通过”对象.属性”的方式,对对象的属性进行赋值。
  • 这里,赋值操作要受属性的数据类型和存储范围的制约。
  • 除此之外,没有其他制约条件。
  • 但是,在实际问题中,我们往往需要给属性赋加入额外的限制条件。
  • 这个条件就不能在属性声明时体现,我们只能通过方法进行限制条件的添加。(比如: set方法)
  • 同时,我们需要避免用户再使用”对象.属性”的方式对属性进行赋值。则需要将属性声明为私有的(private)
  • *此时针对属性就体现了封装性*
  • 二、封装性的体现
  • 我们将类的属性xxx私有化(private),同时提供公共的(public)方法来获取(getXxx)和设置(setXxx)来设置此属性的值
  • but 这并不等同于封装性的全部,前方的路依旧孤单漫长…
  • 拓展:1.如上 2.不对外暴露的私有方法 3.单例模式(构造器) 4…. */ public class Test{ public static void main( String args[]) {
    1. Animal a = new Animal();
    2. a.name = "大黄";
    3. //a.age = 10; The field Animal.legs is not visible 这个字段不可见
    4. //a.legs = 4; The field Animal.legs is not visible 这个字段不可见
    5. a.setAge(10);
    6. a.setLegs(4); // 使用 private + setLegs方法 为设置腿数增加限制
    7. a.show();
    } }

class Animal{ String name; private int age;

  1. private int legs;
  2. //提供age的set和get方法
  3. public void setAge(int a){
  4. if(a >= 0){
  5. age = a;
  6. }else{
  7. System.out.println("年龄异常重新设置");
  8. }
  9. }
  10. public int getAge(){
  11. return age;
  12. }
  13. //提供legs的set和get方法
  14. public void setLegs(int l){
  15. if(l >= 0 && l % 2 == 0){
  16. legs = l;
  17. }else{
  18. System.out.println("腿数异常重新设置");
  19. }
  20. }
  21. public int getLegs(){
  22. return legs;
  23. }
  24. //展示该对象实例的详细信息
  25. public void show(){
  26. System.out.println("name = " +name + ",age = " + age + ",legs = " + legs);
  27. }

}

  1. <a name="LPV02"></a>
  2. ### 3.四种权限修饰
  3. Java规定的4种权限(从小到大排列): private、default、protected、public
  4. | 修饰符 | 类内部 | 同一个包 | 不同包的子类 | 同一个工程 |
  5. | --- | --- | --- | --- | --- |
  6. | private | Y | | | |
  7. | default | Y | Y | <br /> | |
  8. | protect | Y | Y | Y | |
  9. | public | Y | Y | Y | Y |
  10. 注:<br />>default为缺省 就是啥权限也不声明<br />>protect 基本用不着<br />>包可以理解为同一个文件夹下<br />>暂时这么多
  11. 四种权限都可以修饰类和类的内部结构:属性、方法、构造器、内部类(套娃类),而修饰类自身只能用缺省或public
  12. <a name="MOBzC"></a>
  13. ### 4.封装性小练习
  14. ```java
  15. /*
  16. *
  17. * 封装性小练习
  18. *
  19. */
  20. public class Test{
  21. public static void main( String args[]) {
  22. Person p1 = new Person();
  23. p1.setAge(18);
  24. System.out.println("这个人的年龄为" + p1.getAge());
  25. }
  26. }
  27. class Person{
  28. //设置私有
  29. private int age;
  30. //set方法和get方法
  31. public void setAge(int a){
  32. if(a >= 0 && a <= 130){
  33. age = a;
  34. }else{
  35. System.out.println("抛出异常");
  36. }
  37. }
  38. public int getAge(){
  39. return age;
  40. }
  41. }

五、构造器

1.基本理解及详细说明

  1. /*
  2. * 构造器(或构造方法、constructor)的使用
  3. * 一、作用:
  4. * 1.创建对象
  5. * 2.初始化属性
  6. * 二、说明:
  7. * 1.如果没有显式的定义类的构造器的话,则系统默认提供一个空参构造器
  8. * 2.定义构造器的格式:权限修饰符 类名(形参列表){}
  9. * 3.一个类中定义多个构造器,构成重载
  10. * 4.一旦我们显式的定义了类的构造器之后,系统就不再提供默认的空参构造器
  11. * 5.一个类中至少有一个构造器
  12. */
  13. public class Test{
  14. public static void main( String args[]) {
  15. //创建类的对象 new +构造器
  16. Person p1 = new Person("Tom");
  17. Person p2 = new Person();
  18. System.out.println(p1.name + p2.name);
  19. p1.eat();
  20. }
  21. }
  22. class Person{
  23. //属性
  24. String name;
  25. int age;
  26. //构造器1 此空参构造器默认存在
  27. public Person(){
  28. }
  29. //构造器2 传参类似python中init可用于初始化
  30. public Person(String n){
  31. name = n;
  32. }
  33. //方法
  34. public void eat(){
  35. System.out.println("人吃饭");
  36. }
  37. public void study(){
  38. System.out.println("人学习");
  39. }
  40. }

2.构造器练习-基本使用

  1. /*
  2. *
  3. * 利用构造器,创建Person对象时,同时初始化name和age属性
  4. *
  5. */
  6. public class Test{
  7. public static void main(String args[]) {
  8. Person p1 = new Person("Tom", 18);
  9. p1.setAge(18);
  10. System.out.println("姓名:" + p1.getName() + " 年龄:" + p1.getAge());
  11. }
  12. }
  13. class Person{
  14. //设置私有
  15. private String name;
  16. private int age;
  17. //构造器
  18. public Person(String n, int a){
  19. name = n;
  20. age = a;
  21. }
  22. //set方法和get方法
  23. public void setName(String n){
  24. name = n;
  25. }
  26. public String getName(){
  27. return name;
  28. }
  29. public void setAge(int a){
  30. if(a >= 0 && a <= 130){
  31. age = a;
  32. }else{
  33. System.out.println("抛出异常");
  34. }
  35. }
  36. public int getAge(){
  37. return age;
  38. }
  39. }

3.构造器练习-三角形

  1. /*
  2. *
  3. * 编写两个类,TriAngle和TriAngleTest,其中TriAngle类中声明私有的底边长base和高height,同时声明公共方法访问私有变量。
  4. * 此外,提供类必要的构造器。另一个类中使用这些公共方法,计算三角形的面积。
  5. *
  6. */
  7. public class Test{
  8. public static void main(String args[]){
  9. //1.空参构造器创建的对象
  10. TriAngle t1 = new TriAngle();
  11. t1.setBase(5.3);
  12. t1.setHeight(6.3);
  13. System.out.println("base : " + t1.getBase() + ",height :"+t1.getHeight());
  14. //2.带参构造器创建的对象
  15. TriAngle t2 = new TriAngle(7.3, 8.3);
  16. System.out.println("base : " + t2.getBase() + ",height :"+t2.getHeight());
  17. }
  18. }
  19. class TriAngle{
  20. //设置私有
  21. private double base;
  22. private double height;
  23. //构造器
  24. public TriAngle(){
  25. }
  26. public TriAngle(double b, double h){
  27. base = b;
  28. height = h;
  29. }
  30. //set方法和get方法
  31. public void setBase(double b){
  32. base = b;
  33. }
  34. public double getBase(){
  35. return base;
  36. }
  37. public void setHeight(double h){
  38. height = h;
  39. }
  40. public double getHeight(){
  41. return height;
  42. }
  43. }

六、各种知识点

1.属性赋值的先后顺序

1.默认初始化 int age;
2.显式初始化 int age = 18;
3.构造器中赋值
4.通过”对象.方法”或”对象.属性”的方式,赋值

2.JavaBean使用

概念:JavaBean是一种Java语言写成的可重用组件。
所谓JavaBean,是指符合如下标准的java类:
>类是公共的
>有一个无参的公共构造器
>有属性,且有对应的set、get方法

  1. /*
  2. *
  3. * 概念:JavaBean是一种Java语言写成的可重用组件。
  4. * 所谓JavaBean,是指符合如下标准的java类:
  5. * >类是公共的
  6. * >有一个无参的公共构造器
  7. * >有属性,且有对应的set、get方法
  8. *
  9. */
  10. // 一个公共的类
  11. public class Customer{
  12. //设置私有
  13. private int id;
  14. private String name;
  15. //空的构造器
  16. public Customer(){
  17. }
  18. //set方法和get方法
  19. public void setId(int i){
  20. id = i;
  21. }
  22. public int getId(){
  23. return id;
  24. }
  25. public void setName(String n){
  26. name = n;
  27. }
  28. public String getName(){
  29. return name;
  30. }
  31. }

3.拓展知识:UML类图

image.png

4.this调用属性、方法及构造器

  1. /*
  2. * 1.this可以修饰 属性、方法、构造器
  3. * 2.this修饰属性、方法可以理解为*当前对象* 修饰构造器时可以理解为*正在创建的对象*
  4. * 3.通常情况下,我们都选择省略"this. "。特殊情况下,如果方法的形参和类的属性同名时,我们必须显式的使用"this.变量"的方式,表明此变量是属性,而非形参。
  5. * 4.this修饰构造器时,this(形参)
  6. * >我们在类的构造器中,可以显式的使用"this(形参列表)"方式,调用本类中指定的其他构造器
  7. * >构造器中不能通过"this(形参列表)"方式调用自己
  8. * >如果一个类中有n个构造器,则最多有n - 1构造器中使用了"this(形参列表)"
  9. * >规定:"this(形参列表)"必须声明在当前构造器的首行构造器内部,最多只能声明一个"this(形参列表)",用来调用其他的构造器
  10. */
  11. public class Test {
  12. public static void main(String[] args) {
  13. Customer c1 = new Customer();
  14. c1.setId(5);
  15. c1.setName("小花");
  16. Customer c2 = new Customer(5, "嘉然");
  17. c2.eat();
  18. }
  19. }
  20. class Customer{
  21. //设置私有
  22. private int id;
  23. private String name;
  24. //空的构造器
  25. public Customer(){
  26. }
  27. //带参数的构造器
  28. public Customer(int id, String name){
  29. this.id = id; //使用this,形参就可以和构造器正在创建的对象的属性重名了
  30. this.name = name;
  31. }
  32. //set方法和get方法
  33. public void setId(int id){
  34. this.id = id; //使用this,形参就可以和属性重名了
  35. }
  36. public int getId(){
  37. return id;
  38. }
  39. public void setName(String name){
  40. this.name = name; //使用this,形参就可以和属性重名了
  41. }
  42. public String getName(){
  43. return name;
  44. }
  45. public void eat(){
  46. System.out.println("吃饭");
  47. this.study(); ////使用this,可以调用当前对象的方法
  48. }
  49. public void study(){
  50. System.out.println("学习");
  51. }
  52. }

5.this练习-Boy and Girl

构造如下两个类
image.pngimage.png

  1. class Boy{
  2. private String name;
  3. private int age;
  4. public Boy(){
  5. }
  6. public Boy(String name, int age){
  7. this.name = name;
  8. this.age = age;
  9. }
  10. public void setName(String name){
  11. this.name = name;
  12. }
  13. public String getName(){
  14. return name;
  15. }
  16. public void setAge(int age){
  17. this.age = age;
  18. }
  19. public int getAge(){
  20. return age;
  21. }
  22. public void marry(Girl girl){
  23. System.out.println("我想娶" + girl.getName());
  24. }
  25. public void shout(){
  26. if(this.age >= 22){
  27. System.out.println("你可以结婚了");
  28. }else{
  29. System.out.println("爬");
  30. }
  31. }
  32. }
  33. class Girl{
  34. private String name;
  35. private int age;
  36. public Girl(){
  37. }
  38. public Girl(String name, int age){
  39. this.name = name;
  40. this.age = age;
  41. }
  42. public void setName(String name){
  43. this.name = name;
  44. }
  45. public String getName(){
  46. return name;
  47. }
  48. public void setAge(int age){
  49. this.age = age;
  50. }
  51. public int getAge(){
  52. return age;
  53. }
  54. public void marry(Boy boy){
  55. System.out.println("我想嫁给" + boy.getName());
  56. boy.marry(this);
  57. }
  58. public int compare(Girl girl){
  59. if(this.age > girl.age){
  60. return 1;
  61. }else if(this.age < girl.age){
  62. return -1;
  63. }else{
  64. return 0;
  65. }
  66. }
  67. }
  68. public class Test{
  69. public static void main(String[] args) {
  70. Boy boy1 = new Boy("罗密欧", 99);
  71. Girl girl1 = new Girl("朱丽叶", 18);
  72. girl1.marry(boy1);
  73. Girl girl2 = new Girl("嘉然",3);
  74. int result = girl1.compare(girl2);
  75. System.out.println(result);
  76. }
  77. }

6.综合练习-Customer and Account

实验1:
image.pngimage.png
实验2:
image.png

  1. /**
  2. * 1、写一个名为 Account 的类模拟账户。
  3. * 该类包括的属性:账号 id,余额balance,年利率annuallnterestRate;
  4. * 包含的方法:访问器方法(getter和setter方法),取款方法withdraw(),存款方法deposit()。
  5. * 1、写一个名为 Customer 的类模拟账户。
  6. * 该类包括的属性:户名 name, 账户 account;
  7. * 包含的方法:访问器方法(getter和setter方法)。
  8. */
  9. public class Test {
  10. public static void main(String[] args) {
  11. //实验1
  12. Customer c = new Customer("嘉然");
  13. Account a = new Account(1, 1000.0, 0.123);
  14. c.setAccount(a);
  15. c.getAccount().deposit(1000);
  16. c.getAccount().withdraw(3000);
  17. c.getAccount().withdraw(2000);
  18. //实验2
  19. Bank b = new Bank();
  20. b.addCustomer("嘉然");
  21. b.getCustomer(0).setAccount(new Account(1, 1000, 0.123));
  22. b.getCustomer(0).getAccount().withdraw(500);
  23. b.addCustomer("珈乐");
  24. b.getCustomer(0).setAccount(new Account(1, 2000, 0.123));
  25. b.getCustomer(0).getAccount().withdraw(500);
  26. System.out.println(b.getNumCustomer());
  27. }
  28. }
  29. class Account{
  30. private int id;
  31. private double banlance;
  32. private double annuallnterestRate;
  33. //构造器
  34. public Account(int id,double banlance,double annuallnterestRate){
  35. this.id = id;
  36. this.banlance = banlance;
  37. this.annuallnterestRate = annuallnterestRate;
  38. }
  39. //set、get方法
  40. public void setId(int id){
  41. this.id = id;
  42. }
  43. public int getId(){
  44. return id;
  45. }
  46. public void setBanlance(double banlance){
  47. this.banlance = banlance;
  48. }
  49. public double getBanlance(){
  50. return banlance;
  51. }
  52. public void setAnnuallnterestRate(double annuallnterestRate){
  53. this.annuallnterestRate = annuallnterestRate;
  54. }
  55. public double getAannuallnterestRate(){
  56. return annuallnterestRate;
  57. }
  58. //取钱方法
  59. public void withdraw(double amount){
  60. if(banlance >= amount){
  61. this.banlance -= amount;
  62. System.out.println("成功取出" + amount + "元,余额" + this.banlance + "元");
  63. }else{
  64. System.out.println("存款不足,余额" + this.banlance + "元");
  65. }
  66. }
  67. //存钱方法
  68. public void deposit(double amount){
  69. this.banlance += amount;
  70. System.out.println("成功存入" + amount + "元,余额" + this.banlance + "元");
  71. }
  72. }
  73. class Customer{
  74. private String name;
  75. private Account account;
  76. //构造器
  77. public Customer(String name){
  78. this.name = name;
  79. }
  80. //set。get方法
  81. public void setName(String name){
  82. this.name = name;
  83. }
  84. public String getName(){
  85. return name;
  86. }
  87. public void setAccount(Account account){
  88. this.account = account;
  89. }
  90. public Account getAccount(){
  91. return account;
  92. }
  93. }
  94. class Bank{
  95. private Customer[] customers = new Customer[10];
  96. private int numCustomer;
  97. //构造器
  98. public Bank(){
  99. }
  100. //添加客户
  101. public void addCustomer(String name){
  102. Customer c = new Customer(name);
  103. customers[numCustomer] = c;
  104. numCustomer++;
  105. }
  106. //获取客户个数
  107. public int getNumCustomer(){
  108. return numCustomer;
  109. }
  110. //获取指定位置的客户
  111. public Customer getCustomer(int index){
  112. if (index >= 0 && index < numCustomer){
  113. return customers[index];
  114. }else{
  115. return null;
  116. }
  117. }
  118. }

7.package关键字

  1. package com.xxx.yyy.zzz;
  2. /**
  3. * 1.更好的实现项目中类的管理,提供包的概念。
  4. * 2.用package声明类或接口所属的包,声明在文件首行。
  5. * 3.包,属于标识符,遵循标识符的命名规则、规范(xxxyyyzzz)、见名知意。
  6. * 4.每"."—次,就代表一层文件目录。
  7. */

image.png

8.MVC设计模式*

MVC是常用的设计模式之一,将整个程序分为三个层次:数据模型层,控制器层,与视图模型层这种将程序输入输出、数据处理,以及数据的展示分离开来的设计模式使程序结构变的灵活而且清晰,同时也描述了程序各个对象间的通信方式,降低了程序的耦合性。
M:数据模型层(model)
V:视图模型层(view)
C:控制器层(controller)
image.png
理解就可…

8.import关键字

  1. package com.xxx.yyy.zzz;
  2. import java.xxx.*;
  3. public class cat{
  4. }
  5. /**
  6. * import:导入
  7. * 1.在源文件中显式的使用import结构导入指定包下的类、接口
  8. * 2.声明在包的声明和类的声明之间
  9. * 3.如果需要导入多个结构,则并列写出即可
  10. * 4.可以使用"xxx.*"的方式,表示可以导入xxx包下的所有结构
  11. * 5.如果使用的类或接口是java.lang包下定义的,则可以省略import结构 sysout之类的常用语句
  12. * 6.如果使用的类或接口是本包下定义的,则可以省略import结构
  13. * 7. 特殊的可以用类的全部路径调用类
  14. */