Aaron's blog

  • Home

  • About

  • Categories11

  • Archives37

  • Search

Java this关键字(调用属性,调用方法,调用当前对象)

Posted on 2019-08-13 Edited on 2019-09-14 In JAVA Views:

this关键字主要有三个应用:

应用

调用属性(代码示例):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class thisDemo01 {
public static void main(String[] args) throws Exception {
BlueMoon bm=new BlueMoon("渣渣辉", 100);
System.out.println(bm.getInfo());
}
}

class BlueMoon {
private String name;
private int level;

public BlueMoon(String name, int level) {
this.name = name;
this.level = level;
}

public String getInfo() {
return "大家好!我是" + this.name + ",我是贪玩蓝月的战士,等级:" + this.level;
}
}

调用方法(普通方法与构造方法)

  • 调用普通方法:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    public class thisDemo01 {
    public static void main(String[] args) throws Exception {
    BlueMoon bm = new BlueMoon("渣渣辉", 100);
    System.out.println(bm.getInfo());
    }
    }

    class BlueMoon {
    private String name;
    private int level;

    public BlueMoon(String name, int level) {
    this.name = name;
    this.level = level;
    }

    public void print() {
    System.out.println("************************");
    }

    public String getInfo() {
    this.print();//调用普通方法
    return "大家好!我是" + this.name + ",我是贪玩蓝月的战士,等级:" + this.level;
    }
    }
  • 调用构造方法:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    public class thisDemo01 {
    public static void main(String[] args) throws Exception {
    BlueMoon bm1 = new BlueMoon();
    BlueMoon bm2 = new BlueMoon("古天乐");
    BlueMoon bm3 = new BlueMoon("小志传奇", "陈赫", "法师");

    System.out.println(bm1.getInfo());
    System.out.println(bm2.getInfo());
    System.out.println(bm3.getInfo());
    }
    }

    class BlueMoon {
    private String game;
    private String name;
    private String title;
    private int level;

    public BlueMoon() {
    this("贪玩蓝月", "无名氏", "未定", 0);
    }

    public BlueMoon(String name) {
    this("贪玩蓝月", name, "剑士", 90);
    }

    public BlueMoon(String game, String name) {
    this(game, name, "战士", 100);
    }

    public BlueMoon(String game, String name, String title) {
    this();
    this.game = game;
    this.name = name;
    this.title = title;
    }

    public BlueMoon(String game, String name, String title, int level) {
    this.game = game;
    this.name = name;
    this.title = title;
    this.level = level;
    }

    public String getInfo() {
    return "欢迎来到" + this.game + "!我是" + this.name + ",职业:" + this.title + ",等级:" + this.level + "级";
    }
    }

当前对象调用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

class BlueMoon {
public void print() {
//哪个对象调用了print()方法,this就自动与此对象指向同一块内存地址
System.out.println("this=" + this);//this 就是当前调用对象
}
}

public class thisDemo02 {
public static void main(String[] args) throws Exception {
BlueMoon bm = new BlueMoon();
BlueMoon bm2 = new BlueMoon();
System.out.println("bm=" + bm);
bm.print();
System.out.println("---------------------");
System.out.println("bm2=" + bm2);
bm.print();

}
}


参考文献

https://blog.csdn.net/yanwenwennihao/article/details/79375611
https://blog.csdn.net/ikv1989/article/details/79182432

分布式数据库与区块链的区别
思想的转变
  • Table of Contents
  • Overview
Aaron

Aaron

Keep Learning
37 posts
11 categories
GitHub E-Mail
  1. 1. this关键字主要有三个应用:
  2. 2. 调用方法(普通方法与构造方法)
  3. 3. 当前对象调用:
  4. 4. 参考文献
© 2021 Aaron
Powered by Hexo v3.9.0
|
Theme – NexT.Gemini v7.3.0
|