Rookie learning Java object-oriented, this, constructor

object

<student.java>

package it.heima.loop;

public class student {
    
    
    String name;
    int age;
    double score;

    public void print1(){
    
    
        System.out.println(name+"今年"+ age+"岁,期末"+ score+"分");
    }
    public void print2(){
    
    
        System.out.println(name+"期末"+ score+"分");
    }
}

<test.java>

package it.heima.loop;

public class test {
    
    
    public static void main(String[] args) {
    
    
        student s1 = new student();
        student s2 = new student();
        System.out.println(s1);
        System.out.println(s2);


        s1.name = "小黑";
        s1.age = 13;
        s1.score = 78;

        s2.name = "小美";
        s2.age = 14;
        s2.score = 88;

        s1.print1();
        s1.print2();

        s2.print1();
        s2.print2();

    }

}

output

it.heima.loop.student@3b07d329
it.heima.loop.student@41629346
小黑今年13岁,期末78.0分
小黑期末78.0分
小美今年14岁,期末88.0分
小美期末88.0

this

package it.heima.loop;

public class student {
    
    
    String name;
    int age;
    double score;

    public void print1(){
    
    
        System.out.println(name+"今年"+ age+"岁,期末"+ score+"分");
    }
    public void print2(){
    
    
        System.out.println(name+"期末"+ score+"分");
    }

    public void print3(double score){
    
    
        if (this.score > score){
    
    
            System.out.println("True");
        }else{
    
    
            System.out.println("false");
        }
    }
}
package it.heima.loop;

public class test {
    
    
    public static void main(String[] args) {
    
    
        student s1 = new student();
        s1.score = 78;
        s1.print3(63);
    }
}

output

True

In Java, the keyword "this" is used to refer to the current object. It can be used in an instance method of a class to represent a reference to the object on which the method is currently being executed. Here are the main uses of "this":

Referencing instance variables: When a parameter in an instance method has the same name as the instance variable, use the "this" keyword to refer to the instance variable. This makes it clear that an instance variable is to be used instead of a parameter. For example:

public class Person {
    
    
    private String name;

    public void setName(String name) {
    
    
        this.name = name; // 使用 "this" 引用实例变量
    }
}

Calling other constructors: When a class has multiple constructors, you can use the "this" keyword to call other constructors. This approach is called constructor overloading. For example:

public class Person {
    
    
    private String name;
    private int age;

    public Person(String name) {
    
    
        this.name = name;
    }

    public Person(String name, int age) {
    
    
        this(name); // 调用另一个构造方法
        this.age = age;
    }
}

In the example above, the second constructor calls the first constructor with "this(name)" to set the name.

Returning the current object: In some cases, the "this" keyword can be used to return the current object. This is common in method chaining or the builder pattern. For example:

public class Person {
    
    
    private String name;
    private int age;

    public Person setName(String name) {
    
    
        this.name = name;
        return this; // 返回当前对象
    }

    public Person setAge(int age) {
    
    
        this.age = age;
        return this; // 返回当前对象
    }
}

In the above example, the "setName" and "setAge" methods return the current object so that method calls can be made using the chaining syntax.

In summary, the "this" keyword is used in Java to refer to the current object, and can be used to refer to instance variables, call other constructors, and return the current object.

constructor

<tech.java>

public class tech {
    
    
    int aa;
    String name;
    public tech(){
    
    
        System.out.println("哈喽,构造器");
    }
    public tech(int a, String name){
    
    
        aa = a;
        this.name = name;
        System.out.println(this.name + "的年龄是"+ aa +"岁");
    }
}
```java public class gouza { public static void main(String[] args) { new tech(12, "小美"); } } ```

output

小美的年龄是12

Guess you like

Origin blog.csdn.net/AdamCY888/article/details/131494141