Android Road 15 - Java Foundation 9

Reading Guide

1. Equas method inherited from Object
2. Override equals method in Object
3. toString method inherited from Object
4. Override the toString method in Object
5.final keyword
6. Notes

Equas method inherited from Object

package com.hala.test;

import com.hala.animal.Dog;

public class Test {

    public static void main(String[] args) {

        Dog one=new Dog("Erhar",2);
        Dog two=new Dog("Erhar",2);
        boolean flag1=one.equals(two);
        //Equals test: inheriting the equals method in Object compares whether two references point to the same object
        System.out.println("equals Value comparison results:"+flag1);
        System.out.println("one and two Reference comparison results:"+(one==two));
        String str1=new String("hello");
        String str2=new String("hello");
        boolean flag2=str1.equals(str2);
        //equals is actually overridden when used with strings, it compares not references, but whether the values of the two strings are the same
        //So flag2 has a value of true, whereas == connections always compare reference values
        System.out.println("equals Value comparison results:"+flag2);
        System.out.println("str1 and str2 Reference comparison results:"+(str1==str2));



    }

}

Output Results
equals value comparison result: false
Comparison of one and two's citations results: false
Equas value comparison results:true
Reference comparison results for str1 and str2:false

Override equals method in Object

Animal parent

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;


    //Parent construction methods cannot be inherited or overridden
    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;
    }

    //Eat something
    public void eat(){
        System.out.println(this.getName()+"Eating.");
    }

    //This is an override of the Object method
    public boolean equals(Object obj){
        if(obj==null)
            //If this sentence is not present, a null pointer exception will occur
            return false;
        Animal temp=(Animal)obj;
        //The following sentence will fail if there is no type conversion
        if(this.getName().equals(temp.getName())&&(this.getMath()==temp.getMath()))
            return true;
        else return false;
    }

    //This is an overload on the above method
    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("Erhar",2);
        Animal two=new Animal("Erhar",2);
        boolean flag1=one.equals(two);
        //Equals test: once you override equals, you can use it directly
        System.out.println("equals Value comparison results:"+flag1);
        System.out.println("one and two Reference comparison results:"+(one==two));
        String str1=new String("hello");
        String str2=new String("hello");
        boolean flag2=str1.equals(str2);
        //equals is actually overridden when used with strings, it compares not references, but whether the values of the two strings are the same
        //So flag2 has a value of true, whereas == connections always compare reference values
        System.out.println("equals Value comparison results:"+flag2);
        System.out.println("str1 and str2 Reference comparison results:"+(str1==str2));



    }

}

Output Results

Equas value comparison results:true
Comparison of one and two's citations results: false
Equas value comparison results:true
Reference comparison results for str1 and str2:false

toString method inherited from Object

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 Test:
         * 1.By default, toString in the class is called directly when the object name is output, such as two output results
         * 2.When inherited from toString in Object, the output string takes the form of type information (project, package, class name) +@+ address information.As shown in this example
         */

        System.out.println(str1.toString());
        System.out.println(str1);
        //When a String type calls toString, it actually overrides the Object's toString




    }

}

Output Results

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

Override toString method in Object

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;


    //Parent construction methods cannot be inherited or overridden
    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;
    }

    //Eat something
    public void eat(){
        System.out.println(this.getName()+"Eating.");
    }

    //Rewrite toString in Object
    public String toString(){
        return ("The name is:"+this.getName()+"Month is:"+this.getMath());
    }


}
package com.hala.test;

import com.hala.animal.Animal;

public class Test {

    public static void main(String[] args) {

        Animal one =new Animal("tearful",2);
        String str1="hello";
        System.out.println(one.toString());
        System.out.println(one);





    }

}

Output Results

The name is: Flowering month is: 2
The name is: Flowering month is: 2

final keyword

/*
 * 1.final class: This class cannot be inherited and has no subclass public final class/final public class
 * 2.final method: This method can be inherited but not overridden
 * 3.final Local Variables: As long as they are assigned before use, once they are assigned they are not allowed to be modified
 * 4. Attribute members in final class: direct initialization when assignment method (1) is defined
 *(2) Initialize in the construction method
 *(3) Initialize in the construction code block
 */

annotation

⚠️Call parent overridable shortcut key: alt /

    //Here's the comment, which indicates how to override the parent class
    @Override
    public void eat() {
        // Inherit the format of the parent eat method, remember!
        super.eat();
        }

Tags: Attribute

Posted on Thu, 04 Jun 2020 12:37:13 -0400 by LostOne