java第2天:類,對象,封裝和構造方法

  • 2019 年 10 月 11 日
  • 筆記

1 面向對象簡述

將 {1,3,45,56,78,90}轉化為[1,3,45,56,78,90]

1-2 方法1:面向過程

程式碼塊  public class test {      public static void main(String[] args) {          int[] array = {1,3,45,56,78,90};  //        列印成[1,3,45,56,78,90]          System.out.print("[");          for (int i = 0; i < array.length; i++) {              if(i==array.length-1){                  System.out.print(array[array.length-1]+"]");              }              else {                  System.out.print(array[i]+",");              }            }      }  }

1-2 方法2:面向對象

程式碼塊  import java.util.Arrays;  public class test {      public static void main(String[] args) {          int[] array = {1,3,45,56,78,90};  //        列印成[1,3,45,56,78,90]          System.out.println(Arrays.toString(array));      }  }

2 面向對象的思想舉例

面向對象:把衣服扔進洗衣機
面向過程:自己動手洗衣服
***

3 類和對象的關係

對象是類的實例化
類是抽象的。
對象是類具體的。
***

4 定義一個學生類

public class Student {      int age = 13;      String name = "wangsiyu";        public void study(){          System.out.println("學生要學習");      }  }

1.成員變數是直接定義在類當中,在成員方法的裡面
2.成員方法的定義不需要寫static

5 對象的創建和使用

5-1第一步:導包

1.通常情況下,一個類並不能直接使用,必須實例化對象後才可以使用。
2.導包的格式: import 包名稱.類名稱
3.對於和當前類在同一包下,導包語句可以省略不寫

5-2 創建對象

格式:類名稱 對象名 = new 類名稱();
Student stu = new Student();

5-3 對象的使用

成員變數的使用:對象名.成員變數
成員方法的使用:對象名.成員方法();

5-4 實例化上文的學生類

public class Student {      int age = 13;      String name = "wangsiyu";        public void study(){          System.out.println("studdy  sdudsla");      }  }

6手機類的小練習

7 一個對象的記憶體圖

01-只有一個對象的記憶體圖.png

8 兩個對象使用一個類的記憶體圖

02-兩個對象使用同一個方法的記憶體圖.png

9 兩個引用指向同一個對象記憶體圖

03-兩個引用指向同一個對象的記憶體圖.png

10 使用對象類型作為方法的參數

10-1 創建一個學生類

程式碼塊  package demo1;    public class mystudent {      public static void main(String[] args) {          Student stu = new Student();          stu.age=18;          stu.name="wangsiyu";          method(stu);      }      public static void method(Student stu){          System.out.println(stu.age);          System.out.println(stu.name);          stu.think();      }  }

10-2實例化學生,將對象作為方法的參數

程式碼塊  package demo1;    public class mystudent {      public static void main(String[] args) {          Student stu = new Student();          stu.age=18;          stu.name="wangsiyu";          method(stu);      }      public static void method(Student stu){          System.out.println(stu.age);          System.out.println(stu.name);          stu.think();      }  }

10-3使用對象類型作為方法的參數記憶體圖

04-使用對象類型作為方法的參數.png

11 使用對象類型作為方法的返回值

11-1 創建一個學生類

程式碼塊  package demo1;    public class Student {      int age;      String name;        public void think(){          System.out.println(name+"學生可以思考");      }  }  

11-2使用學生類的對象作為方法的返回值

程式碼塊  package demo1;    public class mystudent {      public static void main(String[] args) {          Student res = method();          System.out.println(res.age);          System.out.println(res.name);      }      public static Student method(){          Student stu = new Student();          stu.name = "nezha";          stu.age = 12;          return  stu;        }  }

12 成員變數和局部變數的區別

null 位置 作用域 默認值 記憶體 生命周期
成員變數 方法外部,直接寫在類中 整個類作用域 有默認值,規則和數組一樣 堆記憶體 隨對象而生,隨垃圾回收消失
局部變數 寫在方法內部 只有方法可以調用,出了方法就失效 沒有默認值,想使用必須手動賦值 棧記憶體 隨方法進棧而生,隨方法出棧消失

13 封裝的思想——給一個數組,求最大值

package demo1;    public class getmax {      public static void main(String[] args) {          int[] array = {12,34,67,123,34,555,1024,1};          int res = getMax(array);          System.out.println("array數組的最大值是"+res);      }        public static int getMax(int[] array){          int max = array[0];          for (int i = 0; i < array.length; i++) {              if(array[i]>max){                  max = array[i];              }          }          return max;      }  }

14 private封裝

1.使用了private關鍵字修飾成員變數,本類中仍然可以訪問到該成員變數,但是超出本來之外則不能直接訪問該成員變數
2.使用了private關鍵字修飾成員變數,可以使用Getter和Settter方法來訪問,提高程式碼的安全性

14-1 使用了private,Getter和Setter的類

package demo1;    public class Student {      private int age;      String name;        public void setAge(int res){          age = res;      }      public int getAge(){          return age;      }  }

14-2 使用Setter來設置成員變數值,使用Getter來獲取值

package demo1;    public class mystudent {      public static void main(String[] args) {          Student stu = new Student();          System.out.println(stu.name);          System.out.println(stu.getAge());          stu.setAge(66);          System.out.println(stu.getAge());      }  }

15 使用private關鍵字來定義學生類

注意:布爾值的Setter不變,但是Setter是有區別的。

15-1 定義一個學生類

public class Student {      int age =18;      private boolean male =true;        public boolean isMale() {          return male;      }        public void setMale(boolean male) {          this.male = male;      }  }

15-2 獲得布爾值,和修改布爾值的私有成員變數

package demo1;    public class ms {      public static void main(String[] args) {          Student stu = new Student();          stu.setMale(false);          System.out.println(stu.isMale());      }  }

16 this關鍵字的作用

當方法的局部變數和類的成員變數重名時候,遵循就近原則
使用this.變數名表示使用成員變數
通過誰調用的方法,誰就是this

16-1 定義一個類

package demo1;    public class Student {      String name = "wangsiyu";        public void sayhello(String name){          System.out.println(name+"你好,我是"+this.name);      }  }

16-2 使用這個類

package demo1;    public class ms {      public static void main(String[] args) {          Student stu = new Student();          stu.name = "mayun";          stu.sayhello("wangjianlin");      }  }

17 構造方法

1.當我們使用new創建對象的時候,其實調用的就是構造方法

2.構造方法的名稱必須和類名稱大小寫完全一致

3.構造方法不要寫返回值,連void都不要寫

4.構造方法不能有返回值

18 定義一個標準的學生類

有私有成員變數
有構造方法
有Getter和Setter.

package demo1;    public class Student {      private String name;        public Student(){        }      public Student(String name){          this.name=name;      }        public void setName(String res){          name = res;      }      public String getName(){          return name;      }  }