首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

【JAVA】this跟super关键字的用法

2013-10-25 
【JAVA】this和super关键字的用法一、this关键字的用法(1)引用隐式参数在类的构造器中如果实例域名与显示参数

【JAVA】this和super关键字的用法

一、this关键字的用法

(1)引用隐式参数

在类的构造器中如果实例域名与显示参数名相同时,可以用this引用隐式参数以区分开。如下这段代码

class Employee{     public Employee(String n, double s,      int year, int month, int day)   {        name = n;      salary = s;      GregorianCalendar calendar         = new GregorianCalendar(year, month - 1, day);         // GregorianCalendar uses 0 for January      hireDay = calendar.getTime();   }   public String getName()   {      return name;   }   public double getSalary()   {        return salary;   }   public Date getHireDay()   {        return hireDay;   }   public void raiseSalary(double byPercent)   {        double raise = salary * byPercent / 100;      salary += raise;   }   private String name;   private double salary;   private Date hireDay;}class Manager extends Employee{     /**      @param n the employee's name      @param s the salary      @param year the hire year      @param month the hire month      @param day the hire day   */   public Manager(String n, double s,      int year, int month, int day)   {        super(n, s, year, month, day);      bonus = 0;   }   public double getSalary()   {       double baseSalary = super.getSalary();      return baseSalary + bonus;   }   public void setBonus(double b)   {        bonus = b;   }   private double bonus;}









热点排行