构造函数中的参数较少的构造函数 [英] Constructor with fewer arguments from a constructor

查看:132
本文介绍了构造函数中的参数较少的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Constructor Tree(int a,int b,int c)和第二个Constructor Tree(int a,int b,int c,String s)。如何从第一个加载第二个构造函数来保存写所有的逻辑?
我想到这样的东西,但它给我'null'对象。

I have Constructor Tree(int a, int b, int c) and second Constructor Tree(int a, int b, int c, String s). How to load second constructor from first just to save writing all the logics? I thought about something like this but it gives me 'null' object.

public Tree(int a, int b, int c){
    Tree t1 = new Tree(a, b, c, "randomString");
}


推荐答案

c $ c> this ,例如

The magic word is this, e.g.

public Tree( int a, int b, int c, String d ) {
    // Do something
}

public Tree( int a, int b, int c ) {
    this( a, b, c, "randomString" );
}

这篇关于构造函数中的参数较少的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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