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());
    }
}


猜你喜欢

转载自blog.csdn.net/leon_lavie/article/details/82850654