日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

Core Java笔记 2.继承

發(fā)布時間:2025/7/25 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Core Java笔记 2.继承 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

本章重點:

  • 繼承
  • 多態(tài)與動態(tài)綁定
  • Object類
  • 對象包裝器&自動打包

繼承

涉及到的概念:
超類(superclass)、子類(subclass)
extends關(guān)鍵字
super關(guān)鍵字
多態(tài)(polymorphism)、動態(tài)綁定(dynamic binding)

重點:

  • 如果子類的構(gòu)造器沒有顯式調(diào)用超類的構(gòu)造器,則自動調(diào)用超類默認構(gòu)造器; 如果超類沒有不帶參數(shù)的構(gòu)造器,并且子類的構(gòu)造器中又沒有顯式地調(diào)用超類的其他構(gòu)造器,則將報告錯誤.
  • super關(guān)鍵字: 一是調(diào)用超類的方法,二是調(diào)用超類的構(gòu)造器.
  • polymorphism: 一個對象變量可以引用多種實際類型的現(xiàn)象.
  • dynamic binding: 在運行時能夠自動地選擇調(diào)用哪個方法的現(xiàn)象. Java中dynamic binding是默認的處理方式, 除非用final標記.
  • 繼承層次

    Java 不支持多繼承. 采用 單繼承+接口.


    polymorphism & dynamic binding

    polymorphism

    多態(tài)示例:

    Employee e; e = new Employee(...); // Employee object expected e = new Manager(...); // OK, Manager can be used as wellEmployee[] staff = new Employee[3]; Manager boss = new Manager(...); staff[0] = boss; // OKManager m = staff[i]; // ERROR

    WARN

    package corejava.inheritance;/*** Created by guolong.fan on 15/4/21.*/ public class ArrayStoreExceptionTest {public static void main(String[] args) {Integer[] ints = new Integer[10];ints[0] = 10;Object[] objs = ints; // OK!objs[0] = new Object();System.out.println(ints[0]);}} /**Exception in thread "main" java.lang.ArrayStoreException: java.lang.Object at corejava.inheritance.ArrayStoreExceptionTest.main(ArrayStoreExceptionTest.java:12)*/

    dynamic binding

    調(diào)用對象方法的執(zhí)行過程:

  • 編譯器查看對象的聲明類型和方法名。比如方法f,編譯器會一一列舉類中及其超類中的所有訪問權(quán)限為public且名為f的方法。
  • 編譯器查看調(diào)用方法時提供的參數(shù)類型,這個過程是overloading resolution。首先尋找參數(shù)完全匹配的方法,找到直接選擇;次之,選擇可以轉(zhuǎn)化與之匹配的方法,沒有的話,編譯
    如果找到參數(shù)類型完全匹配,直接選擇;次之,沒有則選擇可以轉(zhuǎn)換與之匹配的方法;
  • private、static、final方法,編譯器可以準確地知道調(diào)用哪個方法,即靜態(tài)綁定(static binding)。
  • dynamic binding的原理是JVM預(yù)先給每個類創(chuàng)建了一個方法表(method table).
  • 小細節(jié):

    • Java SE5.0開始支持協(xié)變。

    final類和 final方法

    final類可以阻止繼承,final方法不可override.

    強制轉(zhuǎn)換與 instanceof

    強制轉(zhuǎn)換:

    double x = 3.405; int nx = (int)x; // 1. OKEmployee staff = new Manager(...); Manager boss = (Manager) staff; // 2. OK

    繼承鏈進行向下轉(zhuǎn)換,Java運行時系統(tǒng)運行下面的程序,會產(chǎn)生ClassCastException異常.

    Employee staff = new Employee(...); Manager boss = (Manager) staff; // ERROR

    總結(jié):

  • 只能在繼承層次內(nèi)進行類型轉(zhuǎn)換,盡量避免強制轉(zhuǎn)換.
  • 如有必要,在將超類轉(zhuǎn)換為子類前,應(yīng)該使用instanceof進行檢查.
  • if (staff instanceof Manager) {boss = (Manager) staff;... }

    Object類

    Object類是 Java 中所有類的最終祖先. 在 Java 中只有基本類型不是對象.

    equals 方法

    典型的equals方法的寫法:

    // super class class Employee {...@Overridepublic boolean equals(Object otherObject) {// 1. a quick test to see if the objects are identicalif (this == otherObject) return true;// 2. must return false if the explicit parameter is nullif (otherObject == null) return false;// 3.1 if the classes don't match, they can't be equalif (getClass() != otherObject.getClass()) {return false;}// or 3.2 如果所有的子類都擁有統(tǒng)一的語義,則使用instanceof檢測.if (!(otherObject instanceof ClassName)) return false;// 4. now we know otherObject is a non-null EmployeeEmployee other = (Employee) otherObject;// 5. test whether the fields have identical valuesreturn name.equals(other.name)&& salary == other.salary&& hireDay.equals(other.hireDay);} }// sub class Class Manager extends Employee {...@Overridepublic boolean equals(Object otherObject) {if (!super.equals(otherObject)) return false;// super.equals checked that this and otherObject belong to the same classManager other = (Manager) otherObject;// test whether the fields have identical values in subclassreturn bonus = other.bonus;} }

    hashCode 方法

    Object 類中的 hashCode 默認實現(xiàn)是對象的存儲地址. 當(dāng)一個類重新定義了 equals 方法時,就必須重新定義 hashCode 方法, 且 equals 與 hashCode 的定義必須保持一致.

    class Employee {public int hashCode() {return 7 * name.hashCode +11 * new Double(salary).hashCode +13 * hireDay.hashCode();}... }

    如果存在數(shù)組類型的域,可以使用靜態(tài)的 Arrays.hashCode 計算 hash,這個散列碼由數(shù)組元素的散列碼組成.

    toString 方法

    典型寫法:

    public class Employee {public String toString() {return getClass().getName()+ "[name=" + name+ ",salary=" + salary + ",hireDay=" + hireDay+ "]";}... }public Manager extends Employee {public String toString() {return super.toString()+ "[bonus=" + bonus+ "]";} }

    x.toString() 和 "" + x 等價. Object 類的 toString() 是className@hashCode.

    int[] luckyNumbers = {2, 3, 5, 7, 11, 13}; String s = "" + luckyNumbers; // [I@hashCode// 打印單維數(shù)組 String s = Arrays.toString(luckyNumbers); // "[2,3,5,7,11,13]" // 打印多維數(shù)組 String s = Arrays.deepToString(luckyNumbers);

    對象包裝器&自動打包

  • Java 的基本類型都有與之對應(yīng)的包裝器(wrapper).
  • Wrapper 是 final 類,并且是不可變的(即一單生成對象,對象本身不可變).
  • Java 泛型不支持 基本類型,可以使用 wrapper.
  • autoboxing & autounboxing 是編譯器行為,而不是 JVM. 編譯器通過添加相關(guān)代碼得以實現(xiàn)(new Integer(), Integer.intValue()).
  • String->Integer(int x = Integer.parseInt(s));
  • 下列情況不會發(fā)生 unboxing.

    Integer a = 1000; Integer b = 1000; if (a == b) ... // may equal, 自動打包要求 boolean、byte、char<=127, 介于-128~127的short和int被包裝到固定的對象中.

    因為 Java 中基本類型是不可變的,所以要求 wrapper 也是不可變的.

    public static void triple(int x) { // won't workx = 3 * x; }public static void triple(Integer x) { // won't workx = 3 * x; // 因為 Integer 對象是不可變的. }

    Integer.parseInt 與 Integer.valueOf 的區(qū)別:

    public static int parseInt(java.lang.String s, int i) throws java.lang.NumberFormatException { /* compiled code */ }public static int parseInt(java.lang.String s) throws java.lang.NumberFormatException { /* compiled code */ }public static java.lang.Integer valueOf(java.lang.String s, int i) throws java.lang.NumberFormatException { /* compiled code */ }public static java.lang.Integer valueOf(java.lang.String s) throws java.lang.NumberFormatException { /* compiled code */ }

    繼承設(shè)計的技巧

  • 將公共操作和域放在超類.
  • 不要使用受保護的域.
  • 使用繼承實現(xiàn) "is-a" relation.
  • 除非所有繼承的方法都有意義,否則不要使用繼承.
  • 在override時不要改變預(yù)期的行為.
  • 使用多態(tài), 而非類型信息(避免類型判斷),是代碼更加易于擴展.
  • 轉(zhuǎn)載于:https://www.cnblogs.com/nil2inf/p/4469213.html

    總結(jié)

    以上是生活随笔為你收集整理的Core Java笔记 2.继承的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。