zoukankan      html  css  js  c++  java
  • equals

    package abstractClasses;
    
    import java.time.LocalDate;
    
    /**
     * Created by xkfx on 2016/12/20.
     */
    public class Employee extends Person{
        private String name;
        private double salary;
        private LocalDate hireDay;
    
        public Employee(String name, double salary, int year, int month, int day){
            super(name);
            this.salary = salary;
            hireDay = LocalDate.of(year, month, day);
        }
    
        public double getSalary(){
            return salary;
        }
    
        public LocalDate getHireDay(){
            return hireDay;
        }
    
        public String getDescription(){
            return String.format("an employee with a salary of $%.2f", salary);
        }
    
        public void raiseSalary(double byPercent){
            double raise = salary * byPercent / 100;
            salary += raise;
        }
    
        public boolean equals(Object otherObject){
            if(this == otherObject)
                return true;
            // 指向同一个对象就什么事情都没有了。
            if(otherObject  == null)
                return false;
            // 所比较对象指向null也不用比了。
            if(this.getClass() != otherObject.getClass())
                return false;
            // 经以上比较可知,两个引用不指向同一个对象,并且两对象属于同类。
            Employee other = (Employee)otherObject;
            // 经过类型强制转化,才能能够访问otherObject对象具体类的实例域
    
            // 测试实例域是否相同
           return name.equals(other.name) && salary == other.salary && hireDay.equals(other.hireDay);
        }
    }
    package abstractClasses;
    
    /**
     * Created by xkfx on 2016/12/20.
     */
    public class Manager extends Employee{
        private double bonus;
    
        public Manager(String name){
            super(name, 0, 2016, 10, 19);
        }
    
        public boolean equals(Object otherObject){
            // 比较父类实例域是否相等
            if(!super.equals(otherObject))
                return false;
            // 比较子类实例域是否相等
            Manager other = (Manager)otherObject;
            return this.bonus == other.bonus;
        }
    }
  • 相关阅读:
    python学习日记——基本数据类型
    python学习日记——安装及初识
    STF平台探索
    fiddler基本操作梳理
    fly.js抛物线连续不断加入购物车
    判断是否存在某个字段hasOwnProperty
    vue中提示toFixed不是函数
    vue中父组件给子组件传值,子组件给父组件传值
    js判断用户的浏览器设备是移动端还是pc端
    css预处理器--sass学习($变量名)
  • 原文地址:https://www.cnblogs.com/xkxf/p/6204701.html
Copyright © 2011-2022 走看看