• Java的API和Object类


    Object类的概述:Object类是所有类的父类,他描述大方法所有子类都可以使用

    1.equlals方法:Object类中的equals方法是用来比较两个对象的地址是否相同,如果需要拿equals方法来做其他事情,就需要在子类中重写equals方法

    package com.oracle.demo08;
    
    public class Person {
        int age;
        String name;
        
        public Person(int age, String name) {
            super();
            this.age = age;
            this.name = name;
        }
        public Person() {
            super();
            
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        //复写父类的equals方法,重写称自己的比较方法
        public boolean equals(Object obj) {
            //判断当前调用equals方法的对象
            //和传递进来的对象是不是同一个
            if(this == obj){
                return true;
            }
            //判断当前传进来的值是否属于person类型
            if(!(obj instanceof Person)){
                return false;
            }
            //将obj向下转型成为person引用,访问属性
            Person p=(Person)obj;
            return this .age==p.age;
        }
    }
    package com.oracle.demo08;
    
    public class Test {
        public static void main(String[] args) {
            Person p1=new Person();
            p1.setName("小红");
            p1.setAge(18);
            Person p2=new Person();
            p2.setName("小明");
            p2.setAge(17);
            System.out.println(p1.equals(p2));
        }
    }

    2.tostring方法:

    toString方法返回该对象的字符串表示

    package com.oracle.demo09;
    
    public class fu {
        private int age;
        private String name;
        
        public fu() {
            super();
        }
        
        public fu(int age, String name) {
            super();
            this.age = age;
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String toString() {
            
            return name+"的年龄是"+age;
        }
    }
    package com.oracle.demo09;
    
    public class test {
        public static void main(String[] args) {
            fu f1=new fu(18,"小刘");
            fu f2=new fu(17,"小孙");
            System.out.println(f1.toString());
            System.out.println(f2);//默认带有tostring()方法
        }
    }

    tostring()方法重写前:

    tostring()方法重写后:

  • 相关阅读:
    关于分布式的一些理解和认识
    Git使用详细教程
    Kettle 7启动 Spoon.bat 时报错“A Java Exception has occurred.”的解决方法
    postgresql 9源码安装
    intel笔记本cpu型号后缀详解(M,U,QM,MQ,HQ,XM)
    LAMP部署流水
    四种mysql存储引擎
    MySQL日志
    MySQL存储引擎总结
    MySQL存储引擎中的MyISAM和InnoDB
  • 原文地址:https://www.cnblogs.com/mr171733/p/9711395.html
Copyright © 2020-2023  润新知