zoukankan      html  css  js  c++  java
  • java之Object类

    Object是所有类的父类,也被称为基类,如果在类中未使用extends声明继承哪个类,则默认继承Object类。

    它的作用:

    public class Main {
        public static void test(Object obj) {
            if (obj instanceof Person){
                System.out.println("这是Person类的子类");
            }
            if (obj instanceof Student){
                System.out.println("这是Student类的子类");
            }
            if (obj instanceof Graduate){
                System.out.println("这是Graduate类的子类");
            }
        }
        
        public static void main(String[] args) {
            Person p = new Person();
            Student s = new Student();
            Graduate g = new Graduate();    
            test(p);
            test(s);
            test(g);
        }
    }

    输出:

    这是Person类的子类
    这是Person类的子类
    这是Student类的子类
    这是Person类的子类
    这是Graduate类的子类

    说明:对于test方法中的形参为一个Object对象的实例,也就是说可以传入任意的对象实例给test方法。

    Object中的主要方法:

    编号 方法名称 类型 描述
    1 public Object() 构造 构造方法
    2 public boolean equals(Object obj) 普通 对象比较
    3 public int hashCode() 普通 取得Hash码
    4 public String toString() 普通 对象打印时使用
  • 相关阅读:
    [转]
    Linux
    [转]
    [转]
    Linux 高级网络编程
    [转]
    [译]- 6-1 排列窗体上的控件(Laying Out Widgets on a Form)
    [转]
    [转]
    the thread has exited with code -1073741819
  • 原文地址:https://www.cnblogs.com/xiximayou/p/12049171.html
Copyright © 2011-2022 走看看