有没有一种方法可以一起在Java中使用构造函数和突变器

发布于 2025-01-22 00:07:06 字数 637 浏览 3 评论 0原文

目前,代码不完整,但是我想知道是否可以使用构造函数在突变器内制造的字符串partno。

public static class Invoice{
    Scanner sam = new Scanner(System.in);
    int Quality;
    double price;
    public Invoice(){ 
        
    }
    public Invoice(String partNo, String description) { 
    }
    public void setPartNo() {
        System.out.println("Enter part no: ");
        String partNo  = sam.nextLine();
        
    
    }
    public static String getPartNo() {
        return Invoice.getPartNo();
        
    }
        }
    

    public static void main(String[] args) {
      Invoice.getPartNo();

The code is incomplete at the moment, but I was wondering if I could use the String partNo the one that the constructor is making inside the mutator.

public static class Invoice{
    Scanner sam = new Scanner(System.in);
    int Quality;
    double price;
    public Invoice(){ 
        
    }
    public Invoice(String partNo, String description) { 
    }
    public void setPartNo() {
        System.out.println("Enter part no: ");
        String partNo  = sam.nextLine();
        
    
    }
    public static String getPartNo() {
        return Invoice.getPartNo();
        
    }
        }
    

    public static void main(String[] args) {
      Invoice.getPartNo();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧人哭 2025-01-29 00:07:06
public static class Invoice{
    Scanner sam = new Scanner(System.in);
int Quality;
double price;
public Invoice(){ 
    
}
public Invoice(String partNo, String description) { 
}
public void setPartNo() {
    System.out.println("Enter part no: ");
    String partNo  = sam.nextLine();
    

}
public static String getPartNo() {
    return Invoice.getPartNo();
    
}
    }


public static void main(String[] args) {
  Invoice.getPartNo();

您在这里有一些构造函数的参数,您什么都不做,这是毫无意义的。构造函数完成执行后,它们将不再存在,因此您的突变器不可能将

这些参数获取,将它们设置为实例成员,这样您的突变器也可以访问它们。

public static class Invoice {
  Scanner sam = new Scanner(System.in);
  int quality;
  double price;

  // add the additional instance members
  String partNo;
  String description;

  public Invoice() { }

  public Invoice(String partNo, String description) {
    this.partNo = partNo;
    this.description = description;
  }

  // a normal mutator (setter) will not use a Scanner, but accept the value as parameter
  // but for now, I'm keeping your original design
  public void setPartNo() {
    System.out.println("Enter part no: ");
    this.partNo = sam.nextLine(); 
    // again, use this.partNo. If you declare it as a String here, it will no longer exist when the method is finished
  }

  // this getter is both pointless, and will cause trouble by creating an endless 
  // recursive loop. Delete it.
  public static String getPartNo() {
    return Invoice.getPartNo();
  }
}

现在,相同的代码具有一些改进:

public static class Invoice {
  int quality;
  double price;

  // add the additional instance members
  String partNo;
  String description;

  public Invoice() { }

  public Invoice(String partNo, String description) {
    this.partNo = partNo;
    this.description = description;
  }

  public void setPartNo(String partNo) {
    // you can always add validation on the new value here
    this.partNo = partNo;
  }

  public String getPartNo() {
    return partNo;
  }
}

这是您可以使用的:

public static void main(String[] args) {
  Invoice in = new Invoice("part", "description");
  System.out.pintln(in.getPartNo());
  in.setPartNo("new PartNo");
  System.out.println(in.getPartNo());
}

如果您想知道您的Getter是否应该是静态的:
仅当字段本身是静态时,它才应该是静态的。由于您通过传递给构造函数的参数设置值,因此可疑它应该是静态的。

构造函数的参数应(通常)仅设置该特定实例的值。
如果是静态字段,则将为该类型的每个实例设置。

public static class Invoice{
    Scanner sam = new Scanner(System.in);
int Quality;
double price;
public Invoice(){ 
    
}
public Invoice(String partNo, String description) { 
}
public void setPartNo() {
    System.out.println("Enter part no: ");
    String partNo  = sam.nextLine();
    

}
public static String getPartNo() {
    return Invoice.getPartNo();
    
}
    }


public static void main(String[] args) {
  Invoice.getPartNo();

You here have a few parameters to your constructor, you don't do anything with, which is pretty pointless. They won't exist anymore once the constructor has finished executing, so it's impossible for your mutators to get them

Those parameters, set them up as instance members, that way your mutators will also have access to them.

public static class Invoice {
  Scanner sam = new Scanner(System.in);
  int quality;
  double price;

  // add the additional instance members
  String partNo;
  String description;

  public Invoice() { }

  public Invoice(String partNo, String description) {
    this.partNo = partNo;
    this.description = description;
  }

  // a normal mutator (setter) will not use a Scanner, but accept the value as parameter
  // but for now, I'm keeping your original design
  public void setPartNo() {
    System.out.println("Enter part no: ");
    this.partNo = sam.nextLine(); 
    // again, use this.partNo. If you declare it as a String here, it will no longer exist when the method is finished
  }

  // this getter is both pointless, and will cause trouble by creating an endless 
  // recursive loop. Delete it.
  public static String getPartNo() {
    return Invoice.getPartNo();
  }
}

Now, the same code with some improvements:

public static class Invoice {
  int quality;
  double price;

  // add the additional instance members
  String partNo;
  String description;

  public Invoice() { }

  public Invoice(String partNo, String description) {
    this.partNo = partNo;
    this.description = description;
  }

  public void setPartNo(String partNo) {
    // you can always add validation on the new value here
    this.partNo = partNo;
  }

  public String getPartNo() {
    return partNo;
  }
}

this, you will be able to use as follows:

public static void main(String[] args) {
  Invoice in = new Invoice("part", "description");
  System.out.pintln(in.getPartNo());
  in.setPartNo("new PartNo");
  System.out.println(in.getPartNo());
}

If you are wondering whether your getter should be static:
it should only be static if the field is static itself. Since you set the value by a parameter you pass to the constructor, it is doubtfull it should be static.

A parameter to the constructor should (usually) only set the value for that particular instance.
If it's a static field, it will be set for every single instance of that type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文