Java 构造函数参数传递

Java 构造函数参数传递,java,Java,所有这些-我对Java和OOP都是新手-试图深入了解构造函数…..有人能告诉我为什么下面的构造函数返回0而不是51吗?如果是打字错误,我深表歉意,但我已经看了一段时间了 //Create a ListD class public class ListD { int x; // Create a class attribute // Create a class constructor for the ListD class

所有这些-我对Java和OOP都是新手-试图深入了解构造函数…..有人能告诉我为什么下面的构造函数返回0而不是51吗?如果是打字错误,我深表歉意,但我已经看了一段时间了

      //Create a ListD class
      public class ListD {
          int x;  // Create a class attribute

          // Create a class constructor for the ListD class
          public ListD(int x) {
              this.x = x;  // Set the initial value for the class attribute x
          }
      }

      public static void main(String[] args) {
          ListD myObj = new ListD(51); // Create an object of class ListD and pass initial value of 'x' (This will call the constructor)
          System.out.println(myObj.x); // Print the value of x
      }
//创建一个MyClass类
公共类列表
{
int x;//创建类属性
//为MyClass类创建类构造函数
公开作废清单d({
x=51;//设置类属性x的初始值
}
公共静态void main(字符串[]args){
ListD myObj=new ListD();/*创建MyClass类的对象(此
(将调用构造函数)*/
System.out.println(myObj.x);//打印x的值
}
}

构造函数名称应与类名相同,并且构造函数不允许任何返回类型。请参阅以下构造函数声明:

      //Create a ListD class
      public class ListD {
          int x;  // Create a class attribute

          // Create a class constructor for the ListD class
          public ListD(int x) {
              this.x = x;  // Set the initial value for the class attribute x
          }
      }

      public static void main(String[] args) {
          ListD myObj = new ListD(51); // Create an object of class ListD and pass initial value of 'x' (This will call the constructor)
          System.out.println(myObj.x); // Print the value of x
      }
在您上面的示例中:

      //Create a ListD class
      public class ListD {
          int x;  // Create a class attribute

          // Create a class constructor for the ListD class
          public ListD(int x) {
              this.x = x;  // Set the initial value for the class attribute x
          }
      }

      public static void main(String[] args) {
          ListD myObj = new ListD(51); // Create an object of class ListD and pass initial value of 'x' (This will call the constructor)
          System.out.println(myObj.x); // Print the value of x
      }
public static void main(String[] args) {
    ListD myObj = new ListD(); // Create an object of class MyClass (This will call the constructor)
    System.out.println(myObj.x); // Print the value of x
}
您应该首先调用void方法,使x值为51

      //Create a ListD class
      public class ListD {
          int x;  // Create a class attribute

          // Create a class constructor for the ListD class
          public ListD(int x) {
              this.x = x;  // Set the initial value for the class attribute x
          }
      }

      public static void main(String[] args) {
          ListD myObj = new ListD(51); // Create an object of class ListD and pass initial value of 'x' (This will call the constructor)
          System.out.println(myObj.x); // Print the value of x
      }
myObj.listD();
System.out.println(myObj.x);
此外,您应该有getter和setter来封装x。 同意您应该遵循前面提到的教程

请记住,
      //Create a ListD class
      public class ListD {
          int x;  // Create a class attribute

          // Create a class constructor for the ListD class
          public ListD(int x) {
              this.x = x;  // Set the initial value for the class attribute x
          }
      }

      public static void main(String[] args) {
          ListD myObj = new ListD(51); // Create an object of class ListD and pass initial value of 'x' (This will call the constructor)
          System.out.println(myObj.x); // Print the value of x
      }
1) 构造函数没有任何返回类型
2) 构造函数的名称与类名相同

      //Create a ListD class
      public class ListD {
          int x;  // Create a class attribute

          // Create a class constructor for the ListD class
          public ListD(int x) {
              this.x = x;  // Set the initial value for the class attribute x
          }
      }

      public static void main(String[] args) {
          ListD myObj = new ListD(51); // Create an object of class ListD and pass initial value of 'x' (This will call the constructor)
          System.out.println(myObj.x); // Print the value of x
      }

      //Create a ListD class
      public class ListD {
          int x;  // Create a class attribute

          // Create a class constructor for the ListD class
          public ListD(int x) {
              this.x = x;  // Set the initial value for the class attribute x
          }
      }

      public static void main(String[] args) {
          ListD myObj = new ListD(51); // Create an object of class ListD and pass initial value of 'x' (This will call the constructor)
          System.out.println(myObj.x); // Print the value of x
      }

是的,在这里它给你你想要的输出

public void listD()
不是构造函数。我建议使用关于构造函数的教程,例如。
public void listD()
不是构造函数,
public listD()
是。明白了-谢谢-非常感谢!构造函数不返回任何内容。他们只是创建实例。不鼓励只使用代码的答案。人们更应该解释这个问题,以及如何解决它。
      //Create a ListD class
      public class ListD {
          int x;  // Create a class attribute

          // Create a class constructor for the ListD class
          public ListD(int x) {
              this.x = x;  // Set the initial value for the class attribute x
          }
      }

      public static void main(String[] args) {
          ListD myObj = new ListD(51); // Create an object of class ListD and pass initial value of 'x' (This will call the constructor)
          System.out.println(myObj.x); // Print the value of x
      }