java constructor:this(。) [英] java constructor: this(.)

查看:225
本文介绍了java constructor:this(。)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么输出为021?为什么有0和1(因为i得到2为什么它改为1)?

Why the output is "021"? Why there are "0" and "1"(since "i" get "2" why it changes to "1")?

public class C {
       protected int i;
       public C(int i){
               this(i,i);
               System.out.print(this.i);
               this.i=i;
}
      public C(int i, int j) {
               System.out.print(this.i);
               this.i=i+j;
}
       public C(){
              this(1);
              System.out.print(i);
}
      public static void main(String[] args) {
             C c=new C();
}}


推荐答案

C(1,1)调用 C()


  • C(1,1)列印 0 this.i 的默认值)并将2( i + j )分配给 this.i

  • 然后 C(1)打印 2 < 1到 this.i

  • 然后 1

  • C(1,1) prints 0 (the default value of this.i) and assigns 2 (i+j) to this.i
  • then C(1) prints 2 and assigns 1 to this.i
  • then C() prints 1

这篇关于java constructor:this(。)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆