1. 程式人生 > 實用技巧 >9.26學習記錄(構造器、物件記憶體分析、super、重寫、多型、介面、抽象、異常)

9.26學習記錄(構造器、物件記憶體分析、super、重寫、多型、介面、抽象、異常)

構造器

  • 特徵

    • 和類名相同

    • 沒有返回值

  • 作用

    • new 本質在呼叫構造方法

    • 初始化物件的值

  • 注意點

    • 定義有參構造之後,如果想使用無參構造,顯示的定義一個無參構造

    • alt+insert快捷生成構造器

public class Person {
//一個類即使什麼都不寫,它也會存在一個方法
String name;
//使用new關鍵字, 本質實在呼叫構造器
//用來初始化值
public Person(){

}
//有參構造:一旦定義了有參構造,無參就必須顯示定義
public Person(String name) {
this.name = name;
}
}

物件記憶體分析

  • 棧:存放的都是方法中的區域性變數。方法的執行一定要在棧中執行。

    • 區域性變數:方法的引數,或者是方法{}內部的變數

    • 作用域:一旦超出作用域,立刻從棧記憶體當中消失

  • 堆:凡是new出來的東西,都在堆當中。

    • 堆記憶體裡面的東西都是一個地址值:16進位制

    • 堆記憶體裡面的資料,都有預設值。規則:

      • 如果是整數: 預設為0

      • 如果是浮點數: 預設為0.0

      • 如果是字元: 預設為'\u0000'

      • 如果是布林: 預設為false

      • 如果是引用型別: 預設為null

  • 方法區:儲存.class相關資訊,包含方法的資訊

super注意點

  1. super呼叫父類的構造方法,必須在構造方法的第一個

  2. super必須只能出現在子類的方法或者構造方法中!

  3. super 和 this 不能同時呼叫構造方法!

  • super和this的不同

    • 代表的物件不同

      • this:本身呼叫者這個物件

      • super:代表父類物件的應用

    • 前提

      • this:沒有繼承也可以使用

      • super:只能在繼承條件下才能使用

    • 構造方法

      • this():本類的構造

      • super():父類的構造

重寫 @Override

  • 重寫的前提:需要有繼承關係,子類重寫父類的方法!

    • 方法名必須相同

    • 引數列表必須相同

    • 修飾符:範圍可以擴大但不能縮小

    • 丟擲的異常:範圍可以被縮小但不能被擴大 ClassNotFoundException - - > Exception(大)

  • 重寫:子類的方法和父類的方法必須要一致;但方法體不同

  • 為什麼要重寫:父類的方法子類不一定需要,或者不一定滿足!

多型

  • 注意事項:

    1. 多型是方法的多型,屬性沒有多型

    2. 父類和子類才可以轉換 否則型別轉換異常!ClassCastException!

    3. 存在條件:繼承關係,方法需要重寫,父類引用指向子類物件! Father f1= new Son();

  • 不能被重寫的方法

    • static方法,屬於類,它不屬於例項

    • final 常量

    • private方法

instanceof和型別轉換

  • instanceof引用型別,判斷一個物件是什麼型別,判斷兩個類直接是否存在父子關係

 public static void main(String[] args) {
//Object > String
//Object > Person > Teacher
//Object > Person > Student

// System.out.println(x instanceof y); 用於判斷是否可以編譯通過!
Object object=new Student();
System.out.println(object instanceof Student);//true
System.out.println(object instanceof Person);//true
System.out.println(object instanceof Object);//true
System.out.println(object instanceof Teacher);//false
System.out.println(object instanceof String);//false
System.out.println("===============================");
Person person=new Student();
System.out.println(person instanceof Student);//true
System.out.println(person instanceof Person);//true
System.out.println(person instanceof Object);//true
System.out.println(person instanceof Teacher);//false
// System.out.println(person instanceof String);//false 編譯出錯!
System.out.println("===============================");
Student student = new Student();
System.out.println(student instanceof Student);//true
System.out.println(student instanceof Person);//true
System.out.println(student instanceof Object);//true
// System.out.println(student instanceof Teacher);//false 編譯出錯!
// System.out.println(student instanceof String);//false 編譯出錯!

}
  • 強制轉換

    • 父類引用指向子類的物件

    • 把子類轉換為父類,向上轉型

    • 把父類轉換為子類,向下轉型:強制轉換

    • 方便方法的呼叫,減少重複的程式碼

    • 子轉父可能丟失方法;父轉子可能損失精度

Static關鍵字

  • 匿名程式碼塊及靜態程式碼塊

public class Person {
//第二個執行
{
//匿名程式碼塊 與物件同時產生,在構造方法之前,用於賦初始值。
System.out.println("匿名程式碼塊");
}
//第一個執行,最早執行,與類一起載入
static{
//靜態程式碼塊 永久只執行一次
System.out.println("靜態程式碼塊");

}
//第三個執行
public Person() {
System.out.println("構造方法");
}

public static void main(String[] args) {
Person person = new Person();

}

  • 靜態匯入包 靜態匯入後無需Math.就能使用

//靜態匯入包
import static java.lang.Math.random;
import static java.lang.Math.PI;
public class test {
public static void main(String[] args) {
System.out.println(random());
System.out.println(PI);
}
}

抽象類abstract

  • 不能new這個抽象類,只能靠子類去實現它,繼承自抽象類的子類,必須實現父類的所有抽象方法(Override)

  • 抽象類中可以寫普通的方法

  • 抽象方法必須在抽象類中

  • 抽象方法只有方法名字,沒有方法的實現

  • 抽象的抽象:約束

存在的意義:提高開發效率,後期可擴充套件性高!

介面(interface)

  • 作用

    • 約束

    • 定義一些方法,讓不同的人實現

    • 介面中預設的方法都是public abstract

    • 介面中所有的常量都是public static final

    • 介面不能被例項化 -----因為介面中沒有構造方法

    • implements可以實現多個介面

    • 實現介面必須要重寫介面中的方法

異常

  • 快捷鍵 :選中 CTRL+ALT+T 快捷生成try catch

 public static void main(String[] args) {
int a=1;
int b=0;
//Ctrl+Alt+T
try {
System.out.println(a/b);
} catch (Exception e) {
e.printStackTrace();//列印錯誤的棧資訊
} finally {

}
}

  • 丟擲異常

 public static void main(String[] args) {
try {
new Test1().test(1,0);
} catch (ArithmeticException e) {
e.printStackTrace();
}
}
//假設這個方法中,處理不了這個異常,在方法上丟擲異常
public void test(int a,int b) throws ArithmeticException{
if (b==0){
throw new ArithmeticException();//主動丟擲的異常,一般在方法中使用
}
}
  • 自定義異常

    • 異常的定義

    • public class MyException extends Exception {
      //傳遞數字>10
      private int detail;

      public MyException(int a) {
      this.detail = a;
      }
      // toString:異常的列印資訊
      @Override
      public String toString() {
      return "MyException{"+ detail + '}';
      }
      }
    • 異常的使用

    • public class Test {
      //可能會存在異常的方法
      static void test(int a) throws MyException {
      System.out.println("傳遞的引數為:"+a);
      if(a>10){
      throw new MyException(a);//丟擲
      }
      System.out.println("ok");
      }

      public static void main(String[] args) {
      try {
      test(1);
      } catch (MyException e) {
      System.out.println("MyException=>"+e);
      }
      }
      }

集合,IO流,多執行緒