关于toString方法

toString方法用于表示对象值的字符串.
绝大多数的toString方法遵循这样的格式:类的名字,随后是一对方括号括起来的域值.下面是Employee类中的toString方法的实现(后面附上完整程序):

public String toString()
{
return"Employee[name"+name+",salary="+salary+",hireDay="+hireDay+"]";
}

实际上,还可以设计的更好一些,最好通过调用getClass().getName()得到类名得字符串,而不要将类名硬加到toString方法中.

public String toString()
{

return getClass().getName()+"[name+"+name+",salary="+salary+

",hireDay="+hireDay+"]";

}

toString方法也可以供子类调用.当然在设计子类时也应该定义自己得toString方法,并将子类得描述添加进去.如果超类使用了getClass().getName(),那么子类仅调用super.toString()就可以了,例如,下面是Manager类中的toString方法:

Class Manager extends Employee
{
.......
public String toString()
{
return super.toString()
+"[bonus="+bonus+"]";
}
}

调用toString:只要对象与一个字符串通过操作符"+"连接起来,java编译就会自动地调用toString方法,以便获得这个对象字符串描述.例如:

Point p=new Point(10,20);
String message="The current position is"+p;

//automatically invokes p.toString()

强烈建议为自己编写的每一个类增加toString方法.这样不仅自己受益,而且所有使用这个类的程序员也都会受益菲浅.



下面这个程序实现了Employee和Manager类的toString,equals和hashCode方法.

import java.util.*;

public class EqualsTest
{
public static void main(String[] args)
{
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.equals(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());
}
}

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

public boolean equals(Object otherObject)
{
// a quick test to see if the 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 name.equals(other.name)
&& salary == other.salary
&& hireDay.equals(other.hireDay);
}

public int hashCode()
{
return 7 * name.hashCode()
+ 11 * new Double(salary).hashCode()
+ 13 * hireDay.hashCode();
}

public String toString()
{
return getClass().getName()
+ "[name=" + name
+ ",salary=" + salary
+ ",hireDay=" + hireDay
+ "]";
}

private String name;
private double salary;
private Date hireDay;
}

class Manager extends Employee
{
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;
}

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
+ "]";
}

private double bonus;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值