第三课
Posted Vanau
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第三课相关的知识,希望对你有一定的参考价值。
//--------------------------Employee.java
package MyPackage;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Objects;
public class Employee
private String name;
private double salary;
private Date hireday;
public Employee(String n,double s,int year,int month,int day)
name=n;
salary=s;
GregorianCalendar calendar=new GregorianCalendar(year,month-1,day);
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;
salary+=raise;
public boolean equals(Object otherObject)
if(this==otherObject)
return true;
if(otherObject==null)
return false;
if(getClass()!=otherObject.getClass())
return false;
Employee other=(Employee)otherObject;
return Objects.equals(name, other.name)&&salary==other.salary&&
Objects.equals(hireday, other.hireday);
//本来是只有toString()方法的,这里为了测试分为两个,看看效果
public String toString1()
//栗子1
return "Employee"+"[name="+name+",salary="+salary+",hireDay="+hireday+"]";
public String toString2()
//栗子2
return getClass()+"[name="+name+",salary="+salary+",hireDay="+hireday+"]";
public String toString3()
//栗子3
return getClass().getName()+"[name="+name+",salary="+salary+",hireDay="+hireday+"]";
public String toString()
//重写超类函数
return "Employee"+"[name="+name+",salary="+salary+",hireDay="+hireday+"]";
//-----------------------Manager.java
package MyPackage;
public class Manager extends Employee
private double bonus;
public Manager(String n, double s, int year, int month, int day)
super(n, s, year, month, day);
bonus=0;
public void SetBonus(double b)
bonus=b;
public double getSalary()
return super.getSalary()+bonus;
public boolean equals(Object otherObject)
if(!super.equals(otherObject))
return false;
Manager other=(Manager)otherObject;
return bonus==other.bonus;
//测试的两个继承自超类的toString,还有一个本身的,给比较一下
public String toString1()
//栗子11
return super.toString1()+"[bonus="+bonus+"]";
public String toString2()
//栗子12
return super.toString2()+"[bonus="+bonus+"]";
public String toString()
//栗子3
return super.toString()+"[bonus="+bonus+"]";
//-----------------------------------EmployeeTest.java
package MyPackage;
public class EmployeeTest
/**
* @param args
*/
public static void main(String[] args)
// TODO Auto-generated method stub
Employee a=new Employee("Alice",7500,1988,12,15);
Employee b=a;
Employee c=new Employee("Alice",7500,1988,12,15);
Employee d=new Employee("Didi",7500,1988,12,15);
System.out.println("------测试超类------");
System.out.println("------测试==------");
System.out.println("a==b? "+(a==b));
System.out.println("a==c? "+(a==c));
System.out.println("------测试equals()------");
System.out.println("a.equals(c)? "+(a.equals(c)));
System.out.println("a.equals(d)? "+(a.equals(d)));
System.out.println("------测试toString------");
System.out.println("Didi.toString():"+d);
System.out.println("Didi.toString():"+d.toString());
System.out.println("Didi.toString1():"+d.toString1());
System.out.println("Didi.toString2():"+d.toString2());
System.out.println("Didi.toString3():"+d.toString3());
Manager e=new Manager("Eastly",8000,1986,12,13);
Manager f=new Manager("Fred",9000,1985,12,10);
f.SetBonus(2000);
System.out.println("------测试子类------");
System.out.println("f.toString():"+f);
System.out.println("f.toString1():"+f.toString1());
System.out.println("f.toString():"+f.toString2());
System.out.println("e.equals(f)?"+e.equals(f));
解释一:
观察可知:第18行可以理解,浅复制引用的是同一块内存(对象),但第19行跟第22行输出完全不一样
摘自:http://www.cnblogs.com/dolphin0520/p/3592500.html
1)对于==,如果作用于基本数据类型的变量,则直接比较其存储的 “值”是否相等;
如果作用于引用类型的变量,则比较的是所指向的对象的地址
2)对于equals方法,注意:equals方法不能作用于基本数据类型的变量
如果没有对equals方法进行重写,则比较的是引用类型的变量所指向的对象的地址;
诸如String、Date等类对equals方法进行了重写的话,比较的是指向对象所存储的内容是否相等。
解释二:
如果x是任一对象,则
System.out.println(x);
/*这里会直接调用x.toString(),并打印输出得到的字符串。
*但是,如果在子类中没有重写,则仍然调用Object类定义的toString(),
*打印输出对象所属的类名和散列码。*/
解释三:
//关于getClass()方法
Employee e;
Class c1=e.getClass();//Object类中的getClass()会返回一个Class类型的实例。
//这里用一个Class对象表示一个Employee类的属性。
String name=c1.getName();//getName()将返回类的名字。
//如果类放在一个包里,则包的名字也作为类的一部分
/*另外,通常这样用来实现两个类对象比较的操作
if(e.getClass()==Employee.class)
*/
更多摘自:http://www.cnblogs.com/lianghui66/archive/2012/12/03/2799134.html
关于深拷贝和浅拷贝
首先摘自:http://www.cnblogs.com/Qian123/p/5710533.html
//-------------------------Employee.java
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Objects;
public class Employee implements Cloneable
private String name;
private double salary;
private Date hireday;
public Employee(String n,double s,int year,int month,int day)
name=n;
salary=s;
GregorianCalendar calendar=new GregorianCalendar(year,month-1,day);
hireday=calendar.getTime();
public String getName()
return name;
public void setName(String aname)
name=aname;
public double getSalary()
return salary;
public Date getHireday()
return hireday;
public void raiseSalary(double byPercent)
double raise=salary*byPercent;
salary+=raise;
public Employee clone() throws CloneNotSupportedException
Employee cloned=(Employee) super.clone();
return cloned;
public String toString()
//重写超类函数
return "Employee"+"[name="+name+",salary="+salary+",hireDay="+hireday+"]";
//-----------------------EmployeeTest.java
public class EmployeeTest
public static void main(String[] args)
try
Employee original=new Employee("John",6000,1988,12,15);
Employee copy=original.clone();
copy.raiseSalary(0.3); //改变值类型域
copy.setName("Jack"); //改变String类型域
System.out.println("original="+original);
System.out.println("copy="+copy);
System.out.println(original == copy); //输出false
catch(CloneNotSupportedException e)
e.printStackTrace();
输出结果(这里是浅拷贝)可能看不出差别。
但当Employee类中还有内部类时(比如员工住址Address),复制情况就完全不同。构造Employee类对象时必然要构造相应的Address对象,浅拷贝只是对同一Address的引用,而深拷贝可以实现两个对象在内存空间内完全独立存在,从而不能影响整个Employee对象。所以在克隆方法内
public Employee clone() throws CloneNotSupportedException
Employee cloned=(Employee) super.clone();
//这里添加对内部类对象的克隆
return cloned;
以上是关于第三课的主要内容,如果未能解决你的问题,请参考以下文章