简体   繁体   English

如何将布尔值传递给新对象?

[英]How do i pass a boolean into a new object?

public class Clown {
    boolean standing = false;

    public Clown(boolean standing) { 
        Clown clown = new Clown(standing);
    }

I want to make a new Clown object with the boolean passed in but everytime I run this I get a stack overflow error. 我想用传入的布尔值创建一个新的Clown对象,但每次运行时我都会遇到堆栈溢出错误。 How can I fix this? 我怎样才能解决这个问题?

You obtain a StackOverflow Exception because you are doing a "recursive" call when you invoke the constructor from itself. 您获得StackOverflow Exception因为当您从自身调用构造函数时,您正在执行“递归”调用。

Do: 做:

public class Clown {

  boolean standing;

  public Clown(boolean standing) {
      this.standing  = standing;
  }

Then create the Object from some method of some class with: 然后从某个类的某个方法创建Object

Clown clown = new Clown(standing);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM