美文网首页
Java this关键字浅析

Java this关键字浅析

作者: zclzhangcl | 来源:发表于2017-03-24 23:39 被阅读0次

在开始本文之前,先写以下代码的输出:

public class Flower{
    int petalCount = 0;
    String s = "init value";
    
    Flower(int petals){
        petalCount = petals;
        print("Constructor int arg only,petalCount = " + petalCount);
    }
    
    Flower(String s){
        print("Constructor string arg only,ss = " + s);
        this.s = s;
    }
    
    Flower(int petals,String s){
        this(petals);
        this.s = s;
        print("String & int args");
    }

    Flower(){
        this(47,"hi");
        print("default constructors ,no args");
    }
    
    void flowerPrint(){
        print("petalCount = "+petalCount+",s = " + s);
    }
    
    void print(String s){
        System.out.printf(s);
    }
    public static void main(String[] args){
        Flower flower = new Flower();
        flower.flowerPrint();
    }
}

(参考:Thinking in Java ,page86。)
如果有疑问,那么我复述一下通过this关键字实现构造器调用构造器的规则:
1、用this调用其他构造器时,仅能调用一个;
2、构造器仅能置于被调用构造器的入口处,否则编译器报错;
3、不能在非构造器中调用构造器;

其中规则2可以解释规则1(对于规则2的报错原因以及规则3的原因,待查)。

this:通常指这个对象或者当前对象,该关键字表示对当前对象的引用。

如果能理解上面这句话,那么对于static关键字的理解加深:static方法就是没有this的方法。
原因:static方法可以理解为类方法。而this方法的含义则是一个实例类的方法的引用,与static方法的含义冲突,如果采用this方法的形式进行调用,那么此时如何知道该this的static方法是属于哪一个实例呢?(有点绕口)

以上demo的输出我相信能理解以上意思的,都可以写出:
Constructor int arg only,petalCount = 47
String & int args
default constructors ,no args
petalCount = 47,s = hi

看了上面的例子,对this关键字有了初步的认识。

1、将方法入参赋给当前对象实例的成员变量

public class Demo{
    private String username;
    private String password;
    private void setUsername(String username){
        this.username = username;
    }
}

如果熟悉javaweb开发的都知道,其实这个setUsername方法就是由ide自动生成的set/get方法之一。

2、将对象实例自身作为参数传递给方法

public class HelloA {
    public static void main(String[] args) {
        A aaa = new A();
        aaa.print();
        B bbb = new B(aaa);
        bbb.print();
    }

    class A {
        public A() {
            // 调用B的方法
            new B(this).print();
        }
        public void print() {
            System.out.println("HelloAA from A!");
        }
    }
    class B {
        A a;
        public B(A a) {
            this.a = a;
        }
        public void print() {
            //调用A的方法
            a.print();
            System.out.println("HelloAB from B!");
        }
    }
}

3、调用类内的其他构造函数
这种用法针对拥有多个成员变量可以使用,对不定参数也可采用类似方法。

public class Demo{
    private String userName;
    private String password;
    private String nickName;
    
    public Demo(){
        
    }
    
    public Demo(String userName){
        this.userName = userName;
    }
    
    public Demo(String userName,String password){
        this(userName);
        this.password = password;
    }
    
    public Demo(String userName,String password,String nickName){
        this(userName,password);
        this.nickName = nickName;
    }
}

4、其他
当然了,还有其他用法,用的不多,就不列举了。
最后,再提一下与this关键字类似的一个关键字,super。super的作用则是调用父类(也称为基类)的方法或者构造参数等,这个也比较常见。可以找找相关资料,了解一下。

相关文章

网友评论

      本文标题:Java this关键字浅析

      本文链接:https://www.haomeiwen.com/subject/peuqottx.html