Android 之路15---Java基础9

来源:互联网 发布:仿牌大数据 编辑:程序博客网 时间:2024/05/28 03:02

导读

1.继承自Object的equals方法
2.重写Object中的equals方法
3.继承自Object的toString方法
4.重写Object中的toString方法
5.final关键字
6.注解

继承自Object的equals方法

package com.hala.test;import com.hala.animal.Dog;public class Test {    public static void main(String[] args) {        Dog one=new Dog("二哈",2);        Dog two=new Dog("二哈",2);        boolean flag1=one.equals(two);        //equals测试:继承Object中的equals方法时,比较的是两个引用是否指向同一个对象        System.out.println("equals值比较结果:"+flag1);        System.out.println("one 和 two的引用比较结果:"+(one==two));        String str1=new String("hello");        String str2=new String("hello");        boolean flag2=str1.equals(str2);        //equals在运用到字符串时,实际上是被重写了,它比较的不是引用,而是两个字符串的值是否相同        //所以flag2的值为true,而==连接的始终比较的是引用值        System.out.println("equals值比较结果:"+flag2);        System.out.println("str1 和 str2的引用比较结果:"+(str1==str2));    }}

输出结果
equals值比较结果:false
one 和 two的引用比较结果:false
equals值比较结果:true
str1 和 str2的引用比较结果:false

重写Object中的equals方法

Animal父类

package com.hala.animal;public class Animal {    protected String name;    private int math;    private String species;    private static int st1=1;    public static int st2=2;    //父类构造方法不能被继承,不能被重写    public Animal(){    }    public Animal(String name,int math){        this.setName(name);        this.setMath(math);    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getMath() {        return math;    }    public void setMath(int math) {        this.math = math;    }    public String getSpecies() {        return species;    }    public void setSpecies(String species) {        this.species = species;    }    //吃东西    public void eat(){        System.out.println(this.getName()+"正在吃东西。");    }    //这是对Object方法的重写    public boolean equals(Object obj){        if(obj==null)            //若没有此句,会出现空指针异常            return false;        Animal temp=(Animal)obj;        //若没有类型转换,下句会报错        if(this.getName().equals(temp.getName())&&(this.getMath()==temp.getMath()))            return true;        else return false;    }    //这是对上边方法的重载    public boolean equals(Animal ani){        if(ani==null)            return false;        if(this.getName().equals(ani.getName())&&(this.getMath()==ani.getMath()))            return true;        else return false;    }}
package com.hala.test;import com.hala.animal.Animal;import com.hala.animal.Dog;public class Test {    public static void main(String[] args) {        Animal one=new Animal("二哈",2);        Animal two=new Animal("二哈",2);        boolean flag1=one.equals(two);        //equals测试:重写equals后,可以直接运用        System.out.println("equals值比较结果:"+flag1);        System.out.println("one 和 two的引用比较结果:"+(one==two));        String str1=new String("hello");        String str2=new String("hello");        boolean flag2=str1.equals(str2);        //equals在运用到字符串时,实际上是被重写了,它比较的不是引用,而是两个字符串的值是否相同        //所以flag2的值为true,而==连接的始终比较的是引用值        System.out.println("equals值比较结果:"+flag2);        System.out.println("str1 和 str2的引用比较结果:"+(str1==str2));    }}

输出结果

equals值比较结果:true
one 和 two的引用比较结果:false
equals值比较结果:true
str1 和 str2的引用比较结果:false

继承自Object的toString方法

package com.hala.test;import com.hala.animal.Animal;public class Test {    public static void main(String[] args) {        Animal one =new Animal();        String str1="hello";        System.out.println(one.toString());        System.out.println(one);        /*         * toString 测试:         * 1.输出对象名时默认会直接调用类中的toString,如两个输出结果         * 2.继承自Object中的toString时,输出字符串的形式为:类型信息(工程,包,类名)+@+地址信息。如本例所示         */        System.out.println(str1.toString());        System.out.println(str1);        //String类型调用toString时,实际上是对Object的toString进行了重写    }}

输出结果

com.hala.animal.Animal@7852e922
com.hala.animal.Animal@7852e922
hello
hello

重写Object中的toString方法

package com.hala.animal;public class Animal {    protected String name;    private int math;    private String species;    private static int st1=1;    public static int st2=2;    //父类构造方法不能被继承,不能被重写    public Animal(){    }    public Animal(String name,int math){        this.setName(name);        this.setMath(math);    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getMath() {        return math;    }    public void setMath(int math) {        this.math = math;    }    public String getSpecies() {        return species;    }    public void setSpecies(String species) {        this.species = species;    }    //吃东西    public void eat(){        System.out.println(this.getName()+"正在吃东西。");    }    //对Object中toString进行重写    public String toString(){        return ("名字为:"+this.getName()+"月份为:"+this.getMath());    }}
package com.hala.test;import com.hala.animal.Animal;public class Test {    public static void main(String[] args) {        Animal one =new Animal("花花",2);        String str1="hello";        System.out.println(one.toString());        System.out.println(one);    }}

输出结果

名字为:花花月份为:2
名字为:花花月份为:2

final关键字

/* * 1.final class:该类不能被继承,没有子类 public final class/final public class * 2.final 方法:该方法可以被继承使用,但不能被重写 * 3.final 方法内局部变量:只要在使用前赋值即可,一旦赋值不允许修改 * 4.final 类中的属性成员:赋值方法(1)定义时直接初始化 *                            (2)在构造方法中初始化 *                            (3)在构造代码块中初始化 */

注解

⚠️调取父类可重写的快捷键:alt /

    //这里是注解,表示重写父类的方法    @Override    public void eat() {        // 继承父类eat方法的格式,要记住!        super.eat();        }
原创粉丝点击