Boring JavaEE from entry to abandonment (eight) IDEA commonly used shortcut keys & equals() method & super keyword

table of Contents

1. IDEA shortcut keys and related operations

2. Rewrite the equals() function

3. Super keyword _ inheritance tree tracing


All properties and methods except the constructor are inherited. However, not all of them can be used directly.

When printing an object, if the toString() method is not called, the toString() method will be called automatically. Return: class name@object address


1. IDEA shortcut keys and related operations

1. The structure view of the class: alt+7
2. Look at the source code of the class: ctr|+Left button
3. View the relationship of the class: mouse on the class name, right click --> Diagram-->show Diagram

4. Automatically generate constructors, get, set methods, equals, etc.: alt+ insert

5. Quick document description prompt after setting mouse hover: File-->Settings-->Editor-->General-->

2. Rewrite the equals() function

Equals() function source code:

public boolean equals(Object obj) {
        return (this == obj);
}

The following code, without rewriting equals () before the output is:

false
false
true
true

Code:

import java.util.Objects;

public class Equals {
    int id;
    int age;
    String name;

    //选取时按Ctrl选多个
    public Equals(int id, int age, String name) {
        this.id = id;
        this.age = age;
        this.name = name;
    }

    public static void main(String[] args) {
        Equals e1=new Equals(1,22,"小明");
        Equals e2=new Equals(1,22,"小明");
        //比较对象地址
        System.out.println(e1==e2);
        System.out.println(e1.equals(e2));//通过equals()方法源码可看到equals也是用==比较,即比较对象是否是一个
        System.out.println("123".equals("123"));
        System.out.println("123"=="123");

    }
    //重写equals()函数,右键Generate自动生成,根据id判断是否相等
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        //空对象或者类型不一致
        if (o == null || getClass() != o.getClass()) return false;
        Equals equals = (Equals) o;
        return id == equals.id;
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }
}


The output after rewriting equals() is:

false
true
true
true

3. Super keyword _ inheritance tree tracing

Super "can be regarded as" is a reference to the direct parent object . You can use super to access the methods or attributes in the parent class that are overridden by the subclass.
Use super to call ordinary methods, there is no restriction on the position of the statement, and it can be called casually in the subclass. If
this
is the first line of code in the construction method without explicitly calling supe(..) or this(..); then Java will call super by default) meaning that it calls the parameterless construction method of the parent class. The super() can be omitted here.

Code 1:

public class TestSuper01 {
    public static void main(String[] args) {
        new ChildClass().f();
    }
}

class FatherClass{
    public int value;
    public void f(){
        value=100;
        System.out.println("FatherClass.value="+value);
    }
}

class ChildClass extends FatherClass{
    public int value;//覆盖一个变量

    public void f(){ //覆盖父类的函数
        super.f();//调用父类的普通方法
        value=200;
        System.out.println("ChildClass.value="+value);
        System.out.println(value);
        System.out.println(super.value);//调用父类的成员变量
    }
}

Output:

FatherClass.value=100
ChildClass.value=200
200
100

Code 2:

public class TestSuper02 {
    public static void main(String[] args) {
        System.out.println("开始创建一个ChildClass对象......");
        new ChildClass2();
    }
}
class FatherClass2{
    public FatherClass2(){
        //这里会先去调用父类即Object类的构造函数,相当于省略了super();
        System.out.println("创建FatherClass2对象");
    }

}

class ChildClass2 extends FatherClass2{
    public ChildClass2(){
        //这里会先去调用父类即FatherClass2的构造函数,相当于省略了super();
        System.out.println("创建ChildClass2对象");
    }

}

Output:

开始创建一个ChildClass对象......
创建FatherClass2对象
创建ChildClass2对象

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/weixin_44593822/article/details/115348351