【问题标题】:Can we create constructor and methods in a single class.?我们可以在单个类中创建构造函数和方法吗?
【发布时间】:2020-05-08 07:32:04
【问题描述】:

我正在尝试在单个类中定义构造函数和方法,并从主类方法访问它。但是当我尝试如下调用方法 read() 时出现错误“构造函数 ConstructorConcept() 未定义”。 我的问题是,这就是我们通常为类创建对象并访问其属性的方式。那么,为什么在访问 read() 方法时会显示错误? 我们不能在一个类中定义方法和构造函数吗?

public class ConstructorConcept {

    String Location;
    String StateName;
    int CityCounts;
    int m=10, n=20;
    int sum;

    public void read() {
        sum=m+n;
        System.out.println(sum);
    }

    ConstructorConcept (String a,String b,int x)
    {
        this.StateName=a;
        this.Location=b;
        this.CityCounts=x;
    }

    ConstructorConcept (String i,String j)
    {
        this.StateName=i;
        this.Location=j;
    }

}


public class ConstructorMainClass {

    public static void main(String[] args) {
        // TODO Auto-generated method stub


        ConstructorConcept v1=new ConstructorConcept("Victoria","Australia",12);
        ConstructorConcept v2= new ConstructorConcept ("VIC", "AUS");
        System.out.println(v1.StateName+ " " +v1.Location+ " " +v1.CityCounts);
        System.out.println(v2.StateName+ " " +v2.Location);

        ConstructorConcept v3= new ConstructorConcept();            
        v3.read();
        System.out.println(v3.m);                               
    }
}

【问题讨论】:

  • 可以添加错误输出吗?

标签: java oop constructor automation


【解决方案1】:

问题出在这一行

ConstructorConcept v3= new ConstructorConcept();

您在这里调用了一个空的构造函数,它在 ConstructorConcept 类中不存在。在您的 ConstructorConcept 类中添加此构造函数声明,它将起作用:-

ConstructorConcept(){
}

【讨论】:

    【解决方案2】:

    V3 是错误的,你没有没有参数的构造函数。其他实例对我有用。

    您可以添加一个不带任何参数的新构造函数,也可以在 v3 的实例化中添加缺失值

    【讨论】:

      【解决方案3】:

      标准 Java Pojo 对其归档以供其他人访问有限制。 public, default, private.
      你需要一个默认的constructor,因为你已经有了自己的承包商。如果您没有任何constrcutors,则会自动添加默认承包商。
      在你的情况下,我认为下面的代码更合适:

      public class ConstructorConcept {
      
          String Location;
          String StateName;
          int CityCounts;
          int m=10, n=20;
          int sum;
      
          public void read() {
              sum=m+n;
              System.out.println(sum);
          }
      
          ConstructorConcept (String a,String b,int x)
          {
              this.StateName=a;
              this.Location=b;
              this.CityCounts=x;
          }
      
          ConstructorConcept (String i,String j)
          {
              this.StateName=i;
              this.Location=j;
          }
      
          //you need all the getters/setters to access fields
          public String getLocation() {
              return Location;
          }
      
          public void setLocation(String location) {
              Location = location;
          }
      
          public String getStateName() {
              return StateName;
          }
      
          public void setStateName(String stateName) {
              StateName = stateName;
          }
      
          public int getCityCounts() {
              return CityCounts;
          }
      
          public void setCityCounts(int cityCounts) {
              CityCounts = cityCounts;
          }
      
          public int getM() {
              return m;
          }
      
          public void setM(int m) {
              this.m = m;
          }
      
          public int getN() {
              return n;
          }
      
          public void setN(int n) {
              this.n = n;
          }
      
          public int getSum() {
              return sum;
          }
      
          public void setSum(int sum) {
              this.sum = sum;
          }
          // you need a default Constructor
          public ConstructorConcept() {
          }
      }
      
      
      public class ConstructorMainClass {
      
          public static void main(String[] args) {
              // TODO Auto-generated method stub
      
      
              ConstructorConcept v1=new ConstructorConcept("Victoria","Australia",12);
              ConstructorConcept v2= new ConstructorConcept ("VIC", "AUS");
              System.out.println(v1.getStateName()+ " " +v1.getLocation()+ " " +v1.getCityCounts());
              System.out.println(v2.getStateName()+ " " +v2.getLocation());
      
              ConstructorConcept v3= new ConstructorConcept();
              v3.read();
              System.out.println(v3.m);
          }
      }
      
      

      【讨论】:

        【解决方案4】:

        是的,我们可以在单个类中定义方法和构造函数。 我可以看到您的代码,您正在尝试实例化一个对象,而您的类 ConstructorConcept 中不存在的零参数构造函数在调用 read 方法之前编写该构造​​函数。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-04-10
          • 2017-11-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-05-22
          • 1970-01-01
          相关资源
          最近更新 更多