public final native Class<?> getClass();
public native int hashCode();
protected native Object clone() throws CloneNotSupportedException;
public final native void notify();
public final native void notifyAll();
public final native void wait(long timeout) throws InterruptedException;
public final void wait(long timeout,int nanos) throws InterruptedException;
public final void wait() throws InterruptedException;
protected void finalize() throws Throwable{}
toString():打印对象内容
Object中toString方法规则:
public String toString(){
return getClass().getName()+"@"+Integer.toHexString(hashCode());
}
代码演示`:
public class TestObject{
public static void ain(String[] args){
TestObject to=new TestObject();
String str=to.toString();
//输出调用toString之后的返回结果
System.out.println(str);
//输出了对象to
System.out.println(to);
}
}
输出结果:
com.mage.oop.object.TestObject@15db9742
com.mage.oop.object.TestObject@15db9742
代码演示:
public class Computer{
String type;
double price;
public Computer(){
}
public String toString(){
return "Computer[type:"+this.type+"price:"+this.price+"]";
}
}
public class TestComputer{
public static void main(String[] args){
//1:创建Computer对象
Computer c=new Computer();
c.type="外星人";
c.price=13800;
//2:打印输出c对象
System.out.println(c);
}
}
//输出结果:Computer[type:外星人price:13800.0]
public boolean equals(object obj){
return(this==obj);
}
代码演示:
public class Computer{
String type;
double price;
public Computer(){
}
}
public class TestComputerEquals{
public static void main(String[] args){
//1:创建两个computer对象
Computer c1=new Computer();
Computer c2=new Computer();
System.out.println(c1);
System.out.println(c2);
boolean flag=c2.equals(c1);//(this==obj);
System.out.println(flag);
}
}
代码结果:
com.pxcoder.myproject4.Computer@15db9742
com.pxcoder.myproject4.Computer@6d06d69c
false
特别注意:==在比较基本数据类型时比较的是值,而equals方法比较引用类型时比较的是地址值。
public class Computer{
String type;
double price;
public Computer(){
}
@Override
public boolean equals(Object obj){
//将object强制转为子类类型
Computer other=(Computer)obj;
return this.price==other.price;
}
}
public class TestComputerEquals{
public static void main(String[] args){
//1:创建两个computer对象
Computer c1=new Computer();
c1.price=10000;
c1.type="联想";
Computer c2=new Computer();
c2.price=10000;
c2.type="联想";
System.out.println(c1.equals(c2));
}
}
//输出结果是:true
题目一:比较两个电脑如果品牌和价钱一致才认为是相等的
public class Demo3 {
String nickName;
int price;
public Demo3_3(){
}
public boolean equals(Object obj) {
//将object强制转为子类类型
Demo3 d=(Demo3)obj;
if(this.nickName.equals(d.nickName)&&this.price==d.price) {
return true;
}
return false;
}
}
public class TestDemo3 {
public static void main(String[] args) {
Demo3_3 a=new Demo3_3();
a.price=1000;
a.nickName="联想";
Demo3_3 b=new Demo3_3();
b.price=1000;
b.nickName="联想";
System.out.println(a.equals(b));
}
}
题目二:比较电脑和字符串
public class Computer{
String type;
double price;
public Computer(){
}
@Override
public boolean equals(Object obj){
//if判定如果为true证明不是computer则直接返回false如果结果是false证明是computer进行后续代码执行
if(!(obj instanceof Computer)){
return false;
}
Computer other=(Computer)obj;
if(this.type.equals(other.type)&&this.price==other.price){
return true;
}
return false;
}
}
public class TestComputerEquals{
public static void main(String[] args){
//1:创建两个computer对象
Computer c1=new Computer();
c1.type="联想";
String str="联想";
System.out.println(c1.equals(str));
}
}
//输出结果:false
instanceof关键字
是用来比较左边对象是否属于右边类型的实际,如果属于则返回true如果不属于返回false。题目三:测试其他
//3:比较两个电脑
Computer c3=new Computer();
System.out.println(c1.equals(c3));
//4:比较两个电脑
Computer c4=null;
System.out.println(c1.equals(c4));
String str1=null;