求解,小白问个问题,在这个例子里super和this为什么是同一个类了

javaissohard 2015-02-12 11:10:37
下面从书上截了一段下来,没看懂为什么super和this都是指代的Employee,,

public class CloneTest
{
public static void main(String[] args)
{
try
{
Employee original = new Employee("Tom", 100);
}
catch (CloneNotSupportedException e)
{
e.printStackTrace();
}
}
}

public class Employee implements Cloneable
{
private String name;
private double salary;
private Date hireDay;

public Employee(String n, double s)
{
name = n;
salary = s;
hireDay = new Date();
}

public Employee clone() throws CloneNotSupportedException
{
System.out.println(super.getClass().getName());-----这里输出为Employee
System.out.println(this.getClass().getName());-----这里输出也为Employee
Employee cloned = (Employee) super.clone();
return cloned;
}
...全文
254 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
爱摸鱼de老邪 2015-02-12
  • 打赏
  • 举报
回复
getClass是非覆盖式的从Object继承来的,Object的getClass()方法的释义是: 返回此 Object 的运行时类。返回的 Class 对象是由所表示类的 static synchronized 方法锁定的对象。 /** * Returns the runtime class of this {@code Object}. The returned * {@code Class} object is the object that is locked by {@code * static synchronized} methods of the represented class. **/ 释义指出,要返回此Object运行时类,这当然不可能指Object自己了,否则所有类调用getClass()方法都返回Object.class了。那到底谁是Object的运行时类呢,不是Object自己,当然就只能是他的儿子、或各种孙子了。这里就是Employee了(无论是用super.getClass()还是this.getClass())。如果想要从Employee中得到Object.class,可以用this.getClass().getSuperclass();
colin0242 2015-02-12
  • 打赏
  • 举报
回复
getClass()表示此对象运行时类的 Class 对象。 所以楼主super.getClass().getName()和this.getClass().getName()都是返回到Employee

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧