相关疑难解决方法(0)

如何在.net中实现和扩展Joshua的构建器模式?

下面是我试过的代码,有更好的方法吗?

    public class NutritionFacts
    {
        public static NutritionFacts.Builder Build(string name, int servingSize, int servingsPerContainer)
        {
            return new NutritionFacts.Builder(name, servingSize, servingsPerContainer);
        }

        public sealed class Builder
        {
            public Builder(String name, int servingSize,
            int servingsPerContainer)
            {
            }
            public Builder totalFat(int val) { }
            public Builder saturatedFat(int val) { }
            public Builder transFat(int val) { }
            public Builder cholesterol(int val) { }
            //... 15 more setters
            public NutritionFacts build()
            {
                return new NutritionFacts(this);
            }
        }
        private NutritionFacts(Builder builder) { …
Run Code Online (Sandbox Code Playgroud)

.net c#

18
推荐指数
2
解决办法
4990
查看次数

重载构造函数VS在Java中创建对象时传递null

如果一个类在对象创建期间需要多个字段信息,并且它允许更少的信息.我们有两个选项
1.提供多个构造函数,或
2.允许客户端在创建对象时传递null参数.

其中最佳做法就是这些.
例:案例1:

public class Test {
    Test(A ob1,B ob2, C ob3){

    }
    Test(A ob1,B ob2){
            this(ob1, ob2, null);    
    }
    public static void main(String args[]){
        Test ob = new Test(new A(),new B());
    }
}
Run Code Online (Sandbox Code Playgroud)

案例2:

public class Test {
    Test(A ob1,B ob2, C ob3){

    }
    public static void main(String args[]){
        Test ob = new Test(new A(),new B(), null);
    }
}
Run Code Online (Sandbox Code Playgroud)

我在同一个类中使用了main方法.请在其他课程中考虑这些主要方法.

谢谢,
Shantanu

java constructor

4
推荐指数
1
解决办法
3170
查看次数

如何在一个java构造函数中调用this()和super(sth)?

我需要知道在java中是否可以进行这种构造函数链调用?我正在扩展基础JButton类,我首先需要初始化超级变量,然后使用默认构造函数初始化我的类.

public CustomButton(){
    try {
        URL inp = CustomButton.class.getResource("/icons/noa_en/buttonBackground.png");
        background = ImageIO.read(inp);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public CustomButton(ImageIcon img){
    super(img);
    this();
}
Run Code Online (Sandbox Code Playgroud)

要么 :

public CustomButton(ImageIcon img){
    this();
    super(img);
}
Run Code Online (Sandbox Code Playgroud)

java constructor

1
推荐指数
1
解决办法
294
查看次数

标签 统计

constructor ×2

java ×2

.net ×1

c# ×1