当前位置: 移动技术网 > IT编程>开发语言>Java > Java连载42-this不能省略的情况、构造方法设置默认值的方法

Java连载42-this不能省略的情况、构造方法设置默认值的方法

2019年10月19日  | 移动技术网IT编程  | 我要评论

一、 this什么时候是不能省略的,我们举个例子来说明

class user2{

  private int id;

  public int getid() {

    return id;

  }

  public void setid(int id) {

    this.id = id;//这里如果写成了id=id,那么根据就近原则,第一个id就是形式参数,第二个id也是形式

    //参数,所以这里必须要加上this才行,类似于python中的self

  }

}

 

 

this用来区分局部变量和实例变量的时候,是不能省略的。

二、我们对于构造方法的再次练习,注意我们的注释,这是这个联系的重要总结。

 

package com.bjpowernode.java_learning;

public class d42_ {

  public static void main(string[] args) {

    user2 u1 = new user2(2155,"张三");

    //这里说明了两个问题

    //首相可以传入参数,说明这是调用了user2类中的构造方法,可以看一下函数原型是需要两个参数的

    //如果不写这连个参数,那么我们需要在类user2中增加不带参数的构造方法(也就是重载函数)

    //虽然id和name是private类型的,但是我们传入参数可以看出来,照样可以修改,但是我们后面再修改的时候

    //就必须得调用那两个实例函数了

    system.out.println(u1.getname());

   

  }

 

}

class user2{

  private int id;

  private string name;

​

  public int getid() {

    return id;

  }

  public void setid(int id) {

    this.id = id;//这里如果写成了id=id,那么根据就近原则,第一个id就是形式参数,第二个id也是形式

    //参数,所以这里必须要加上this才行,类似于python中的self

  }

  public string getname() {

    return name;

  }

  public void setname(string name) {

    this.name = name;

  }

 

  public user2(int id,string name) {

    this.id = id;

    this.name = name;

  }

 

}

 

 
      

三、我们需要创建一个date类用来存储我们的日期,同时这里面有一个需求:如果创建对象的时候不提供年月日的话,我们需要给这个对象一个默认值1970-01-01

 

package com.bjpowernode.java_learning;

public class d42_constructerwithdefaultvalue {

  public static void main(string[] args) {

    date2 d1 = new date2(2019,10,19);

    date2 d2 = new date2();

    d1.outputdate();

    d2.outputdate();

  }

}

class date2{

  int year;

  int month;

  int day;

  /**

   * @param year

   * @param month

   * @param day

   */

  public date2(int year, int month, int day) {

    this.year = year;

    this.month = month;

    this.day = day;

  }

  /**

   * 需求:如果创建对象的时候不提供年月日的话,我们需要给这个对象一个默认值1970-01-01

   */

  public date2(){

    this(1970,1,1);

  }

  public void outputdate() {

    system.out.println("您想要的日期是:" + year + "年" + month + "月" + day + "日");

  }

 

}

我们看一下这里面的两个构造函数,一个是可以传入参数的,一个是不传入参数,我们不传入参数的有个固定的格式:this(默认参数),并且这个语句必须出现在这个构造方法的第一行,有什么其他的语句,在后面写

对此可以我们解释为啥可以多这个语法,我们可以这样写

 

public date2(){

        year = 1970;

        month = 1;

        day = 1;

}

 

 

​这样写运行是没有问题的,可以这样写,但是这样写代码是冗余的,因此不合适

我们还可以这样写

 

public date2(){

    new date2(1970,1,1);

}

 

 

这样等于我们又创建了一个对象,这样也是不好的,因此也不用这个方法

 

四、我们总结一下​:

this​可以使用在哪里:

(1)可以使用在实例方法之中,代表当前对象【​语法格式:this.】

(2)可以使用在构造方法之中,通过当前的构造方法调用其他的构造方法【​语法格式:this(实参);】

(3)this()这种语法只能出现在构造方法的第一行

三、源码:

d42_thesitustionofnotomittihskeyword.java

d42_constructerwithdefaultvalue

地址:

https://github.com/ruigege66/java/blob/masterd42_thesitustionofnotomittihskeyword.java

https://github.com/ruigege66/java/blob/master/d42_constructerwithdefaultvalue

2.csdn:https://blog.csdn.net/weixin_44630050(心悦君兮君不知-睿)

3.博客园:https://www.cnblogs.com/ruigege0000/

4.欢迎关注微信公众号:傅里叶变换,个人公众号,仅用于学习交流,后台回复”礼包“,获取大数据学习资料

 

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网