如何创建空字符串?

发布于 2024-09-14 07:11:43 字数 446 浏览 5 评论 0原文

我有两个构造函数

MyObj(String s){ //first constructor
    ...
    if(s==null) s = somecode;
            this.s = s;
    ...
}

MyObj(): this(null) { } //second constructor

这样,如果调用空构造函数,它将重定向到第一个构造函数并初始化由某些代码确定的值。

但是,现在我有第三个构造函数

MyObj(Stream st){ //third constructor
    ...
}

现在第二个构造函数不知道是否应该调用第一个构造函数或第三个构造函数。我如何告诉它调用第一个构造函数?我尝试了 MyObj(): this(String s = null) ,但它也不起作用。

I have two constructors

MyObj(String s){ //first constructor
    ...
    if(s==null) s = somecode;
            this.s = s;
    ...
}

MyObj(): this(null) { } //second constructor

In this way, if the empty constructor is called, it will redirect to the first constructor and initialise the value as determined by some code.

However, now I have a third constructor

MyObj(Stream st){ //third constructor
    ...
}

Now the second constructor has no idea whether it is supposed to call the first constructor or the third. How do I tell it to call the first constructor? I tried MyObj(): this(String s = null) and it doesn't work either.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

再可℃爱ぅ一点好了 2024-09-21 07:11:43

也许:MyObj(): this((string) null) {}

Maybe: MyObj(): this((string) null) {}?

夏九 2024-09-21 07:11:43

我经常发现对具有多个构造函数的类使用初始化方法更容易保护我的理智。 每个构造函数都调用它

private void Init(string str, Stream stream, bool doOtherStuff)
{
    // do stuff, replace nulls with defaults, etc
}

,允许构造函数本身做自己的事情。我真的不喜欢构造函数调用其他构造函数,因为它很容易令人困惑,并且某些东西可能无法初始化......或者某些东西可以多次初始化。

I often find it easier to protect my sanity by using an initialization method for classes with several constructors. A single

private void Init(string str, Stream stream, bool doOtherStuff)
{
    // do stuff, replace nulls with defaults, etc
}

that gets called by every constructor, allowing the constructor itself to do its own thing. I really dislike constructors calling other constructors because it can get confusing easily, and something might not get initialized... or something can get initialized more than once.

旧情别恋 2024-09-21 07:11:43

我同意围攻。不要尝试链接构​​造函数,而是使用可以根据需要从每个构造函数调用的 Init 函数。

I agree with Siege. Don't try to chain constructors, instead use an Init function that can be called as needed from each ctor.

怪我太投入 2024-09-21 07:11:43

添加我的一点。

我还建议用静态 CreateXXX 方法替换多个构造函数,而将构造函数设为私有。

这对于您的类的用户来说将更加明确,其中用户可以调用他感兴趣的创建方法并仅传递相关参数。不用担心对象中还有什么。

Adding my bit.

I would also suggest replacing multiple constructors with static CreateXXX methods, and rather make the constructor private.

This will be more explicit to the user of your class, wherein user can call the create method that he is interested in and pass the relevant parameters only. And not worry about what else is there in the object.

你怎么这么可爱啊 2024-09-21 07:11:43

我认为下一段代码会更好。

MyObj(String s) //first constructor
{ 
    ...      
    this.s = s;
    ...
}

MyObj() //second constructor
{
    this(somecode);
} 

MyObj(Stream st) //third constructor
{ 
   ...
}

我的示例表明您可以避免在第一个构造函数中检查 null。在第一个构造函数中的字符串可以为空的情况下使用第二个构造函数可以提高代码的可读性

I think that it would be better the next piece of code.

MyObj(String s) //first constructor
{ 
    ...      
    this.s = s;
    ...
}

MyObj() //second constructor
{
    this(somecode);
} 

MyObj(Stream st) //third constructor
{ 
   ...
}

My example shows that you could avoid checking on null at first constructor. Usage of second constructor in case, where string s from first constructor is nullable, improves readability your code

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文