java this 三种用法

java this 三种用法

 

 1.区分成员变量和局部变量

    public class ThisDemo {
            public static void main(String[] args) {
                Student s=new Student("小明明",20);
                System.out.println(s);
            }
        }
        class Student{
            String name;
            int age;
            public Student(String name, int age) {
                //注:没有加this
                name = name;
                age = age;
            }
            @Override
            public String toString() {
                return "Student [ name=" + name + ", age=" + age +"]";
            }
        }

    打印结果:Student [ name=null, age=0]
    赋值没有成功,说明:name(局部变量)=name(局部变量);
    而不是name(成员变量)=name(局部变量);
     
    public Student(String name, int age) {
            //注:可以使用this进行区分成员变量和局部变量
            this.name = name;
            this.age = age;
    }

    打印结果:Student [ name=小明明, age=20]
    这样子就可以赋值成功啦

2.代表当前对象

    public static void main(String[] args) {
                Student s=new Student("小明明",20);
                System.out.println(s);
        }
        //为什么加入this后就可以区分呢?
        因为this当前代表的是s实例(对象)相当于
        s.name="小明明";
        s.age="20";
        //再可以理解如下
        this.name=实例(对象).name="小明明";

3.构造器与构造器之间的调用

为Student再添加两个构造器,修改后如下:

        class Student{
            String name;
            int age;
            int id;
     
            public Student(int id,String name, int age) {
                this.id = id;
                this.name = name;
                this.age = age;
            }
     
     
            public Student(String name, int age) {
                this.name = name;
                this.age = age;
            }
     
            public Student(String name) {
                this.name = name;
            }
     
            @Override
            public String toString() {
                return "Student [ id=" + id + ", name=" + name + ", age=" + age +"]";
            }
        }

    //构造器用法-->this();跟函数一样()中可以写参数
    构造器调用一般是这样子用的.不同的构造器中存在相同代码.为了复用性。可以在参数少的构造器中调用参数多的构造器,如下:

    class Student{
            String name;
            int age;
            int id;
            public Student(String name, int age, int id) {
                this.name = name;
                this.age = age;
                this.id = id;
                System.out.println("构造器3已调用");
            }
            public Student(String name, int age) {
                this(name,age,0);
                System.out.println("构造器2已调用");
            }
     
            public Student(String name) {
                this(name,0);//参数不足,就使用参数默认值补全
                System.out.println("构造器1已调用");
            }
            @Override
            public String toString() {
                return "Student [ id=" + id + ", name=" + name + ", age=" + age +"]";
            }
        }

    测试结果1:
        public static void main(String[] args) {
            Student s=new Student("小明明");
            System.out.println(s);
        }   
     
        构造器3已调用
        构造器2已调用
        构造器1已调用
        Student [ id=0, name=小明明, age=0]
     
    测试结果2:
        public static void main(String[] args) {
            Student s=new Student("小明明",20);
            System.out.println(s);
        }
        构造器3已调用
        构造器2已调用
        Student [ id=0, name=小明明, age=20]
     
    总结:这样子可以在参数最多的构造器中编写代表。其他构造器负责调用参数最多的那个构造器就好了

this的三种常见用法介绍完毕!怎么样很容易吧.
--------------------- 
作者:Meliodasi 
来源:CSDN 
原文:https://blog.csdn.net/qq_38131751/article/details/81436398 
版权声明:本文为博主原创文章,转载请附上博文链接!

posted @ 2020-07-08 11:15  guguanghu  阅读(441)  评论(0编辑  收藏  举报