第15章 常用类
15.01 练习
(见上一章 最后一项)
15.02 Object类
java中的所有类都直接或间接的继承于Object类,自动的拥有Object类的所有方法。
Ctrl + Shift + T --> Open Type
Ctrl+ o ,找到toString
baidu
java se api中文版
https://tool.oschina.net/apidocs/apidoc?api=jdk-zh
https://tool.oschina.net/apidocs/apidoc?api=jdk-zh
toString方法用于对象的内容格式化为一个字符串
Alt+Shift+S -->构造器 选择
例一:
class Animal{
private String name;
private int age;
public Animal(String name, int age) {
super();
this.name = name;
this.age = age;
}
}
public class Day1301 {
public static void main(String[] args) {
Animal a1 = new Animal("pet",1);
String str = a1.toString();
System.out.println(str); //Animal@1db9742
}
}
例二:为了输出对象的内容,需要重写Object类的toString方法。
Alt +Shift+S --> Geneate toString
class Animal{
private String name;
private int age;
public Animal(String name, int age) {
super();
this.name = name;
this.age = age;
}
//重写toString方法
@Override
public String toString() { //这个toString就会覆盖Object的toString, 因为此处已经被重写了。
return "Animal [name=" + name + ", age=" + age + "]";
}
}
public class Day1301 {
public static void main(String[] args) {
Animal a1 = new Animal("pet",1);
String str = a1.toString();
System.out.println(str); //Animal@1db9742
}
}
//----------------------------
//result:
//Animal [name=pet, age=1]
tips:
system.out.println
(println是一个实例方法)
System.out.println();
out是一个对象,它所属的类是PrintStream
out后面跟了一个println,说明后面是一个方法,println是一个实例方法
PrintStream也是一个类,按CTRL点进去
这两种方法是等效的。
15.02.01 equals方法
例一:equals方法用于比较两个对象是否相等。
class Client{
private int id;
public Client(int id) {
super();
this.id = id;
}
}
public class Day1302 {
public static void main(String[] args) {
Client c1 = new Client(25);
Client c2 = new Client(25);
Client c3 = c1;
System.out.println(c1 == c2);// false
System.out.println(c1 == c3);//true
//Object类的equals方法和==相同
System.out.println(c1.equals(c2));//false
System.out.println(c1.equals(c3));//true
}
}
例二:
Object类的equals方法是使用==比较两个对象是否相等,也就是说比较两个对象的引用。
需要重写equals方法来比较对象的内容。
class Client{
private int id;
public Client(int id) {
super();
this.id = id;
}
//重写Object类的equals方法
public boolean equals(Client c){
return this.id == c.id;
}
}
public class Day1302 {
public static void main(String[] args) {
Client c1 = new Client(25);
Client c2 = new Client(25);
Client c3 = c1;
System.out.println(c1 == c2);// false
System.out.println(c1 == c3);//true
//Object类的equals方法和==相同
System.out.println(c1.equals(c2));//true
System.out.println(c1.equals(c3));//true
}
}
例三
class Client{
private int id;
private String name;
private int age;
public Client(int id) {
super();
this.id = id;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Client other = (Client) obj;
if (age != other.age)
return false;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
//重写Object类的equals方法
//这样写还是有问题,会造成空指针异常NullPointerException
// public boolean equals(Client c){
// return (this.id == c.id) && (this.name.equals(c.name)) && this.age == c.age;
// }
public class Day1302 {
public static void main(String[] args) {
// Client c = null;
// c.toString();//NullPointerException,在一个null对象上调用方法
Client c1 = new Client(25);
Client c2 = new Client(25);
Client c3 = c1;
System.out.println(c1 == c2);// false
System.out.println(c1 == c3);//true
//Object类的equals方法和==相同
System.out.println(c1.equals(c2));//true
System.out.println(c1.equals(c3));//true
}
}