1. 程式人生 > >Java基礎五 繼承(下)

Java基礎五 繼承(下)

1 受保護訪問

Java用於控制可見性的四個訪問修飾符:

僅對本類可見:private;

對所有類可見:public;

對本包和所有子類可見:protected;

對本包可見:預設

2 Object:所有類的超類

1 在Java中,只有基本型別不是物件,例如,數值,字元和布林型別的值都不是物件。所有的陣列型別,不管是物件陣列還是基本型別的陣列都擴充套件了Object類。在C++中沒有所有類的根類,不過,每個指標都可以轉化為void* 指標。

2 在進行比較的時候如果沒有重寫equals方法,那麼equals方法和==一樣,都是判斷兩個物件是否有相同的引用,具體的請看我的收藏。

static Boolean equals(type[] a,type[] b)  如果兩個陣列長度相同,並且在對應的位置上資料元素也均相同,將返回True,陣列的元素型別可以是Object,int,long,short,char,byte,boolean,float或double。

static boolean equals(Object a, Object b) 如果a和b都為null,返回true,如果其中之一為null,則返回false;否則返回a.equals(b)。

3 int hashCode()   返回物件的雜湊碼。雜湊碼可以是任意的整數,正數或負數。兩個相等的物件要求返回相等的雜湊碼。

static int hash(object...objects)  返回一個雜湊碼,由提供的所有物件的雜湊碼組合得到的。

static int hashCode(Object a)  如果a為null返回0,否則返回a.hashCode()。

static int hashCode((int|long|short|byte|double|float|char|boolean)   value)  返回給定值的雜湊碼。

static int hashCode(type[] a)  計算陣列a的雜湊碼。組成這個陣列的元素型別可以是object,int,long,short,char,byte,boolean,float或double。

4 只要一個物件與一個字串通過操作符“+”連線起來,Java編譯就會自動地呼叫toString方法,以便獲得這個物件的字串描述。在呼叫x.toString()的地方可以用“”+x替代。這條語句將一個空串與x的字串表示相連線。這裡的x就是x.toString()。與toString不同的是,如果x是基本型別,這條語句照樣能夠執行。

5   Class getClass() 返回包含物件資訊的類物件。

boolean equals(Object otherObject)  比較兩個物件是否相等,如果兩個物件指向同一塊儲存區域,方法返回true,否則返回false,在自定義的類中應該返回這個方法。

String toString()   返回描述該物件值的字串。在自定義的類中,應該覆蓋這個方法。

String getName()   返回這個類的名字

Class getSuperclass()     以Class物件的形式返回這個類的超類資訊。

package equals;

import java.time.*;
import java.util.Objects;

public class Employee {
    private String name;
    private double salary;
    private LocalDate hireday;

    public Employee(String name,double salary,int year,int month,int day)
    {
        this.name = name;
        this.salary = salary;
        hireday = LocalDate.of(year,month,day);
    }

    public String getName()
    {
        return name;
    }

    public double getSalary()
    {
        return salary;
    }

    public LocalDate getHireday()
    {
        return hireday;
    }

    public void raiseSalary(double byPercent)
    {
        double raise = salary * byPercent/100;
        salary+=raise;
    }

    public boolean equals(Object otherObject)
    {
        // a quick test to see if objects are identical
        if(this == otherObject) return true;

        // must return false if the explicit parameter is null
        if(otherObject == null) return false;

        //if the classes don't match,they can't be equal
        if(getClass()!=otherObject.getClass()) return false;

        // now we know otherObject is a non-null Employee
        Employee other = (Employee) otherObject;

        // test whether the fields have identical values
        return Objects.equals(name,other.name) && salary==other.salary && Objects.equals(hireday,other.hireday);
    }

    public int hashCode()
    {
        return Objects.hash(name,salary,hireday);
    }

    public String toString()
    {
        return getClass().getName() + "[name=" + name + ",salary=" + salary + ",hireday=" + hireday + "]";
    }
}
package equals;

public class Manager extends Employee {
    private double bonus;

    public Manager(String name,double salary,int year,int month,int day)
    {
        super(name,salary,year,month,day);
        bonus=0;
    }
    public double getSalary()
    {
        double baseSalary= super.getSalary();
        return baseSalary+bonus;
    }

    public void setBonus(double bonus)
    {
        this.bonus = bonus;
    }

    public boolean equals(Object otherObject)
    {
        if(!super.equals(otherObject)) return false;
        Manager other = (Manager) otherObject;
        // super.equals() checked that this and other belong to the same class
        return bonus==other.bonus;
    }

    public int hashCode()
    {
        return super.hashCode() + 17 * new Double(bonus).hashCode();
    }

    public String toString()
    {
        return super.toString() + "[bonus=" + bonus + "]";
    }
}
package equals;

public class EqualsTest {
    public static void main(String[] args)
    {
        String a="123";
        String b=a;
        b="456";
        String c="123";
        System.out.println("a==b:" +(a==b));
        System.out.println("a==c:" + (a==c));
        System.out.println(("a.equals(b):"+ a.equals(b)));
        System.out.println(("a.equals(c):"+ a.equals(c)));
        Employee alice1 = new Employee("Alice Adams",75000,1987,12,15);
        Employee alice2 = alice1;
        Employee alice3 = new Employee("Alice Adams",75000,1987,12,15);
        Employee bob = new Employee("Bob Brandson",50000,1989,10,1);

        System.out.println("alice1==alice2:" + (alice1==alice2));
        System.out.println("alice1==alice3:" + (alice1==alice3));

        System.out.println("alice1.equal(alice3):" + alice1.equals(alice3));
        System.out.println("alice1.equals(bob):" + alice1.equals(bob));
        System.out.println("bob.toString:" + bob);

        Manager carl = new Manager("Carl Cracker",80000,1987,12,15);
        Manager boss = new Manager("Carl Cracker",80000,1987,12,15);
        boss.setBonus(5000);

        System.out.println("boss.toString():" + boss);
        System.out.println("carl.equals(boss):" + carl.equals(boss));
        System.out.println("alice1.hashCode():" + alice1.hashCode());
        System.out.println("alice3.hashCode():" + alice3.hashCode());
        System.out.println("bob.hashCode():" + bob.hashCode());
        System.out.println("carl.hashCode():" + carl.hashCode());
    }
}