Java面向对象3----this关键字

Posted iostreamzl

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java面向对象3----this关键字相关的知识,希望对你有一定的参考价值。

文章目录

this关键字的三个作用

  • 当前类中的属性:this.属性名
  • 当前类中的方法:this()----狗仔方法, this.方法名()----普通方法
  • 指代当前对象

this调用属性

之前我们的构造方法的定义如下

public Person(String n, int a) 
        name = n;
        age = a;
    

参数n,a分别是未来name和age属性赋值的。但是这样的蚕食很容易让人不知道意义。所以最好的方式是如下的做法

public Person(String name, int age) 
        name = name;
        age = age;
    

但是这样的定义构造方法会带来一个问题:实例化时不能成功赋值

public class Demo 
    public static void main(String[] args) 
        new Person("匿名对象", 66).showInfo();
    


这是由于Java的变量作用域导致的问题。在构造方法中已经有name,和age的定义了。参照就近原则,在构造中使用的name,age就是参数中的name和age。而不是属性name,age。为了解决这个问题我们可以使用this.属性名,来指定属性

class Person 
    private String name; // 人的属性名字
    private int age; // 人的属性年龄

    // 定义构造方法,方法名与类名一致,且无返回值
    public Person(String name, int age) 
        this.name = name;
        this.age = age;
    

    public void showInfo() 
        System.out.println("name: " + this.name + "\\nage: " + this.age);
    

    // setter, getter方法省略


public class Demo 
    public static void main(String[] args) 
        new Person("匿名对象", 66).showInfo();
    



当然也可以使用之前的定义构造方法。但是那种方法是很不推荐的。在以后的程序中规范写法是只要是属性都需要采用this关键字来访问

this调用方法

在类中调用普通方法,加不加this关键字都是可以的。但是标准的写法还是需要加上this的

构造方法

构造方法是在实例化对象的时候调用的。所以this()只允许放在构造方法的首行。既然this在构造方法中使用,那么就是有可能会形成死循环的,在使用的时候需要特别注意

class Person 
    private String name; // 人的属性名字
    private int age; // 人的属性年龄

    // 定义构造方法,方法名与类名一致,且无返回值
    public Person(String name, int age) 
        this(name);
        this.age = age;
    

    // 定义一参构造
    public Person(String name) 
        this();
        this.name = name;
    

    // 定义无参构造
    public Person() 
        System.out.println("无参构造调用了");
    

    public void uselessFunc() 
        System.out.println("一个无用的方法调用了");
    

    public void showInfo() 
        System.out.println("name: " + this.name + "\\nage: " + this.age);
    

    // setter, getter方法省略


public class Demo 
    public static void main(String[] args) 
        new Person("匿名对象", 66).showInfo();
    


普通方法

public void uselessFunc() 
        System.out.println("一个无用的方法调用了");
    
public void showInfo() 
        this.uselessFunc();
        System.out.println("name: " + this.name + "\\nage: " + this.age);
    

public class Demo 
    public static void main(String[] args) 
        new Person("匿名对象", 66).showInfo();
    

综合实例

实现一个学生类,包含(name, age, sex, stuNo)四个属性。包含四个构造方法
无参构造:默认设置name: stu1, age: 18, sex:男, stuNo:1000
一参构造:传入name.默认设置age: 20, sex:女, stuNo:111111
二参构造:传入name,stuNo。默认设置age: 20
三餐构造:传入name,stuNo, age.
四参构造:传入name, stuNo, age, sex
一个返回学生信息的方法getInfo()
方式1,不采用this()调用构造方法

class Student 
    // 定义属性
    private String name; // 姓名
    private  long stuNo; // 学号
    private int age; // 年龄
    private String sex; // 性别

    // 定义无参构造
    public Student() 
        this.name = "stu1";
        this.stuNo = 10000L;
        this.age = 18;
        this.sex = "男";
    

    // 一参构造
    public Student(String name) 
        this.name = name;
        this.stuNo = 111111L;
        this.age= 20;
        this.sex = "女";
    

    // 二参构造
    public Student(String name, long stuNo) 
        this.name = name;
        this.stuNo = stuNo;
        this.age = 20;
    

    //三参构造
    public Student(String name, long stuNo, int age) 
        this.name = name;
        this.stuNo = stuNo;
        this.age = age;
    

    // 四参构造
    public Student(String name, long stuNo, int age, String sex) 
        this.name = name;
        this.stuNo = stuNo;
        this.age = age;
        this.sex = sex;
    

    // 返回信息
    public String getInfo() 
        return "姓名:" + this.name + "\\n学号: " + this.stuNo +
                "\\n年龄:" + this.age + "\\n性别:" + this.sex;
    



public class Demo 
    public static void main(String[] args) 
        Student stu = new Student("stu", 188888L, 22, "男");
        System.out.println(stu.getInfo());
    



可以看到上面的构造函数中存在大量的重复代码,这就可以使用this()来进行优化

class Student 
    // 定义属性
    private String name; // 姓名
    private  long stuNo; // 学号
    private int age; // 年龄
    private String sex; // 性别

    // 定义无参构造
    public Student() 
        this("stu1", 10000L, 18, "男");
    

    // 一参构造
    public Student(String name) 
        this(name, 111111L, 20, "女");
    

    // 二参构造
    public Student(String name, long stuNo) 
        this(name, stuNo, 20, null); // String的默认值为null
    

    //三参构造
    public Student(String name, long stuNo, int age) 
        this(name, stuNo, age, null);
    

    // 四参构造
    public Student(String name, long stuNo, int age, String sex) 
        this.name = name;
        this.stuNo = stuNo;
        this.age = age;
        this.sex = sex;
    

    // 返回信息
    public String getInfo() 
        return "姓名:" + this.name + "\\n学号: " + this.stuNo +
                "\\n年龄:" + this.age + "\\n性别:" + this.sex;
    



public class Demo 
    public static void main(String[] args) 
        Student stu = new Student("stuThis", 188888L, 22, "男");
        System.out.println(stu.getInfo());
    



在开发中灵活使用this()能大大简化代码。

以上是关于Java面向对象3----this关键字的主要内容,如果未能解决你的问题,请参考以下文章

Java基础02 面向对象

java学习---面向对象进阶

学号 2019-2020-1 《数据结构与面向对象程序设计》第五周学习总结

12月11日 php面向对象

java中的战斗机——类与对象

学号20182335 2019-2020-1 《数据结构与面向对象程序设计》实验四报告