object常用的方法有哪些

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/py245164372/article/details/78106485

1.我们最常用的方法就是toString()方法,将对象信息变成字符串返回。

class People
{
    String name = "Haixia";
    int age = 21;
}


public class Text{
    public static void main(String[] args)
    {
        People p = new People();
        System.out.println(p.toString());
    }
}

但是当我们采用toString输出对象的时候,实际输出了对像的地址时,需要对toString的方法进行重写

class People
{
    String name = "Haixia";
    int age = 21;
    public String toString()
    {
        return "姓名:"+name+",年龄:"+age;
    }
}

2.常用的方法还有equals()方法比较两个对象的地址是否相等,需要重写equals()方法。

3.hashcode(),用来返回所在对象的物理地址。

4.wait(),用来线程等待,让出cpu。

5.getClass()用来获取运行的类。

6.notify()唤醒某个线程。

7.notifyAll()唤醒所有线程。

猜你喜欢

转载自blog.csdn.net/py245164372/article/details/78106485
今日推荐