zoukankan      html  css  js  c++  java
  • this关键字在构造函数中的应用

    /**
    * Created by 刘朋程.on 2014-6-12.博客园

    this的用法

    1、用于区分同名变量的情况(成员变量和局部变量同名的时候)

    2、用于构造函数之间互相调用。this语句只能定义在构造函数的第一行,以为初始化要先执行。
    */
    class this_persion {
        private String name;
        private int age;

        this_persion()
        {

    }
        this_persion(String name)
        {
            this.name = name ;
        }
        this_persion(String name,int age)
        {
            this(name);
            this.age = age ;
        }
        public boolean compare(this_persion p)
        {
            return this.age == p.age;
        }

    }

    Created by 刘朋程.on 2014-6-12.博客园
    class this_persionDomo
    {
       public static void main(String[] args)
       {
           this_persion p1 = new this_persion("张三",35);
           this_persion p2 = new this_persion("张三",35);
           boolean b = p1.compare(p2);
           System.out.println(b);
       }
    }

    Created by 刘朋程.on 2014-6-12.博客园

  • 相关阅读:
    Java第九次作业
    Java第八次作业
    Java第七次作业
    Java第六次作业
    Java第五次作业
    Java第四次作业
    Java第三次作业
    Java第二次作业
    Java第一次作业
    高级工程师和初级工程师之间的一道坎
  • 原文地址:https://www.cnblogs.com/liupengcheng/p/3783708.html
Copyright © 2011-2022 走看看