菜鸟学Java 面向对象、this、构造器

对象

<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();

    }

}

输出

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);
    }
}

输出

True

在Java中,关键字 “this” 用于引用当前对象。它可以在类的实例方法中使用,表示对当前正在执行方法的对象的引用。以下是 “this” 的主要用法:

引用实例变量:当实例方法中的参数和实例变量同名时,使用 “this” 关键字来引用实例变量。这样可以明确指定要使用的是实例变量而不是参数。例如:

public class Person {
    
    
    private String name;

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

调用其他构造方法:当一个类有多个构造方法时,可以使用 “this” 关键字调用其他构造方法。这种方式被称为构造方法的重载。例如:

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;
    }
}

在上面的例子中,第二个构造方法使用 “this(name)” 调用了第一个构造方法来设置名称。

返回当前对象:在某些情况下,可以使用 “this” 关键字来返回当前对象。这在链式调用方法或构建者模式中很常见。例如:

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; // 返回当前对象
    }
}

上面的例子中,“setName” 和 “setAge” 方法返回了当前对象,以便可以使用链式语法进行方法调用。

总之,“this” 关键字在Java中用于引用当前对象,可以用于引用实例变量、调用其他构造方法以及返回当前对象。

构造器

<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, "小美"); } } ```

输出

小美的年龄是12

猜你喜欢

转载自blog.csdn.net/AdamCY888/article/details/131494141