Java 在没有构造函数的情况下如何初始化类?

Java 在没有构造函数的情况下如何初始化类?,java,constructor,initialization,Java,Constructor,Initialization,所以我有一个承包商: public MyClass(boolean done, int lvl , String s, int x, int y, Skill parent, Item item, int cost) { this.done = done; this.lvl = lvl; this.name = s; this.Xpos = x; this.Ypos = y; this.parent = parent; this.item

所以我有一个承包商:

 public MyClass(boolean done, int lvl , String s, int x, int y, Skill parent, Item item, int cost) {
    this.done = done;
    this.lvl = lvl;
    this.name = s;
    this.Xpos = x;
    this.Ypos = y;
    this.parent = parent;
    this.item = item;
    addSkill(this, s);
}
有没有一种方法可以让我在另一个类中使用/初始化它而无需执行以下操作

MyClass myclass = new MyClass(false, 0, "", 0, 0, null, this.item, 1)
如果我这么做的话

MyClass myclass;

然后我得到了“可怕的”空指针异常。

听起来您想创建第二个不带参数的构造函数

然后你就可以写了

MyClass myclass = new MyClass();

听起来您想创建第二个不带参数的构造函数

然后你就可以写了

MyClass myclass = new MyClass();

MyClass-MyClass只是一个参考。你需要用一些东西把它绑起来。没有电视就不能使用遥控器。您还可以尝试缩短构造函数的长度,只需提供一个0参数的构造函数

 public MyClass() {
    this.done = "default";
    this.lvl = "default value";
    this.name = "default value";
    this.Xpos = "default value";
    this.Ypos = "default value";
    this.parent = "default value";
    this.item = "default value";
}

现在您可以这样做
MyClass mcRef=newmyclass()

MyClass MyClass只是一个参考。你需要用一些东西把它绑起来。没有电视就不能使用遥控器。您还可以尝试缩短构造函数的长度,只需提供一个0参数的构造函数

 public MyClass() {
    this.done = "default";
    this.lvl = "default value";
    this.name = "default value";
    this.Xpos = "default value";
    this.Ypos = "default value";
    this.parent = "default value";
    this.item = "default value";
}

现在您可以这样做
MyClass mcRef=newmyclass()

我建议您实现类似于。它用途广泛。您可以在Wikipedia上阅读有关的更多信息。

我建议您实现类似的功能。它用途广泛。您可以在Wikipedia上阅读有关的更多信息。

听起来您可能需要一些“默认参数”。在Python中,您可以执行以下操作:

class MyClass:
    def __init__(done=false, load=1, ...):
        self.done = done
        self.load = load
        # ...

a_new_instance = MyClass(done=true)
public class MyClass {
    private boolean done;
    private int lvl;

    // Now the constructor is private and takes a builder.
    private MyClass(MyClassBuilder builder) {
        // ... and your variables come from the ones you will build.
        this.done = builder.done;
        this.lvl = builder.lvl;
        // ...
    }

    public static class MyClassBuilder {
        // The builder also has the same members.
        private boolean done;
        private int lvl;

        // Notice that we return the builder, this allows us to chain calls.
        public MyClassBuilder done(boolean isDone) {
            this.done = isDone;
            return this;
        }   

        public MyClassBuilder level(int level) {
            this.lvl = level;
        }

        // And a method to build the object.
        public MyClass build() {
            MyClass mc = new MyClass();
            mc.done = this.done;
            mc.lvl = this.lvl;
            // ... copy over all your variables from the builder to the new class
            return mc;
        }
    }
}
基本上,所有变量都以默认值开始,但如果您愿意,可以更改它们

在Java中,它有点不同:

class MyClass {
    private boolean done = false; // Notice the default value for done will be false
    // ... you would list (and instantiate!) the rest of your variables

    public MyClass() {}

    public MyClass(boolean done, int lvl, ...) {
         this.done = done;
         // ...
    }
}
这样,只有在您想要更改默认值的情况下,才强制您调用构造函数。但如果只想更改1或2个值,会发生什么情况?那么,您可以创建新的构造函数:

public MyClass(boolean done) { this.done = done; }
public MyClass(boolean done, int lvl) { this.done = done; this.lvl = lvl; }
但这很快就会失控

所以,为了解决这个问题,我们可以使用“构建器”模式。看起来是这样的:

class MyClass:
    def __init__(done=false, load=1, ...):
        self.done = done
        self.load = load
        # ...

a_new_instance = MyClass(done=true)
public class MyClass {
    private boolean done;
    private int lvl;

    // Now the constructor is private and takes a builder.
    private MyClass(MyClassBuilder builder) {
        // ... and your variables come from the ones you will build.
        this.done = builder.done;
        this.lvl = builder.lvl;
        // ...
    }

    public static class MyClassBuilder {
        // The builder also has the same members.
        private boolean done;
        private int lvl;

        // Notice that we return the builder, this allows us to chain calls.
        public MyClassBuilder done(boolean isDone) {
            this.done = isDone;
            return this;
        }   

        public MyClassBuilder level(int level) {
            this.lvl = level;
        }

        // And a method to build the object.
        public MyClass build() {
            MyClass mc = new MyClass();
            mc.done = this.done;
            mc.lvl = this.lvl;
            // ... copy over all your variables from the builder to the new class
            return mc;
        }
    }
}
因此,现在当我们想要实例化一个
MyClass
对象时,我们可以这样做:

MyClass mc = MyClassBuilder.done(false);
或者,我们可以连锁呼叫:

MyClass mc2 = MyClassBuilder.done(true).level(4). // ... you can go on for a while


另一方面,有时一个类中有三到四个以上的变量是该类做得太多的标志。如果一个班有不止一个“责任”,你应该把它分成几个小班。这样您就不需要生成器类了。

听起来您可能需要一些“默认参数”。在Python中,您可以执行以下操作:

class MyClass:
    def __init__(done=false, load=1, ...):
        self.done = done
        self.load = load
        # ...

a_new_instance = MyClass(done=true)
public class MyClass {
    private boolean done;
    private int lvl;

    // Now the constructor is private and takes a builder.
    private MyClass(MyClassBuilder builder) {
        // ... and your variables come from the ones you will build.
        this.done = builder.done;
        this.lvl = builder.lvl;
        // ...
    }

    public static class MyClassBuilder {
        // The builder also has the same members.
        private boolean done;
        private int lvl;

        // Notice that we return the builder, this allows us to chain calls.
        public MyClassBuilder done(boolean isDone) {
            this.done = isDone;
            return this;
        }   

        public MyClassBuilder level(int level) {
            this.lvl = level;
        }

        // And a method to build the object.
        public MyClass build() {
            MyClass mc = new MyClass();
            mc.done = this.done;
            mc.lvl = this.lvl;
            // ... copy over all your variables from the builder to the new class
            return mc;
        }
    }
}
基本上,所有变量都以默认值开始,但如果您愿意,可以更改它们

在Java中,它有点不同:

class MyClass {
    private boolean done = false; // Notice the default value for done will be false
    // ... you would list (and instantiate!) the rest of your variables

    public MyClass() {}

    public MyClass(boolean done, int lvl, ...) {
         this.done = done;
         // ...
    }
}
这样,只有在您想要更改默认值的情况下,才强制您调用构造函数。但如果只想更改1或2个值,会发生什么情况?那么,您可以创建新的构造函数:

public MyClass(boolean done) { this.done = done; }
public MyClass(boolean done, int lvl) { this.done = done; this.lvl = lvl; }
但这很快就会失控

所以,为了解决这个问题,我们可以使用“构建器”模式。看起来是这样的:

class MyClass:
    def __init__(done=false, load=1, ...):
        self.done = done
        self.load = load
        # ...

a_new_instance = MyClass(done=true)
public class MyClass {
    private boolean done;
    private int lvl;

    // Now the constructor is private and takes a builder.
    private MyClass(MyClassBuilder builder) {
        // ... and your variables come from the ones you will build.
        this.done = builder.done;
        this.lvl = builder.lvl;
        // ...
    }

    public static class MyClassBuilder {
        // The builder also has the same members.
        private boolean done;
        private int lvl;

        // Notice that we return the builder, this allows us to chain calls.
        public MyClassBuilder done(boolean isDone) {
            this.done = isDone;
            return this;
        }   

        public MyClassBuilder level(int level) {
            this.lvl = level;
        }

        // And a method to build the object.
        public MyClass build() {
            MyClass mc = new MyClass();
            mc.done = this.done;
            mc.lvl = this.lvl;
            // ... copy over all your variables from the builder to the new class
            return mc;
        }
    }
}
因此,现在当我们想要实例化一个
MyClass
对象时,我们可以这样做:

MyClass mc = MyClassBuilder.done(false);
或者,我们可以连锁呼叫:

MyClass mc2 = MyClassBuilder.done(true).level(4). // ... you can go on for a while


另一方面,有时一个类中有三到四个以上的变量是该类做得太多的标志。如果一个班有不止一个“责任”,你应该把它分成几个小班。那么你就不需要构建类了。

这不是C++。所有引用的默认值均为
null
。如果不使用
new
,对象将保持
null

如果不喜欢多参数构造函数,有几种初始化方法。您可以让setter返回此
和链初始化,如:

Person p=newperson().setAge(18)、setName(“Tom”).setHeight(175)

或者干脆

Person p=newperson().年龄(18)、姓名(“汤姆”).身高(175)

这种方法并不常见,但肯定很容易阅读,也很难出错

您还可以使用静态工厂方法,如

class Person {

  private Person() {} // mark as private to force  creation with static method

  public static Person create() {
    Person p = new Person();
    //fill default values?
    return p;
  }
}

这不是C++。所有引用的默认值均为

null
。如果不使用
new
,对象将保持
null

如果不喜欢多参数构造函数,有几种初始化方法。您可以让setter返回此
和链初始化,如:

Person p=newperson().setAge(18)、setName(“Tom”).setHeight(175)

或者干脆

Person p=newperson().年龄(18)、姓名(“汤姆”).身高(175)

这种方法并不常见,但肯定很容易阅读,也很难出错

您还可以使用静态工厂方法,如

class Person {

  private Person() {} // mark as private to force  creation with static method

  public static Person create() {
    Person p = new Person();
    //fill default values?
    return p;
  }
}

创建一个无参数的第二个构造函数?您可以使用
依赖项注入
或创建一个工厂方法编写另一个调用该构造函数的构造函数,并提供一些默认参数。我认为您误解了左手边的
MyClass MyClass
。您正在声明内存中的空间以适合
MyClass
大小的对象。您正在调用此空间的位置
myclass
。但你不能在那个地方放任何东西。它是空的-因此是空指针异常。您需要构造一个对象并将其分配给该空间(例如,
MyClass MyClass=new MyClass();
)。创建对象的唯一方法是构造它,因此在没有构造函数的情况下永远无法初始化类。
MyClass MyClass
只是一个引用。如果你想要一个对象的实例,你必须创建一个。创建一个无参数的第二个构造函数?你可以使用
依赖项注入
或创建一个工厂方法编写另一个调用该实例的构造函数,提供一些默认参数。我想你误解了左手边的
MyClass MyClass
的作用。您正在声明内存中的空间以适合
MyClass
大小的对象。您正在调用此空间的位置
myclass