简体   繁体   English

在类内部自动生成变量值

[英]automatic generation of a variable value inside a class

I want to program a class so that each time a new object is created, that object has a new generated code, but the trick is that I do not want to pass that value as an argument to the constructor. 我想对一个类进行编程,以便每次创建一个新对象时,该对象都有一个新生成的代码,但是窍门是我不想将该值作为参数传递给构造函数。 Roughly I have the following: 我大致有以下几点:

public class Article{
    private int cod;
    private String name;
    public Article(String name){
         this.name=name:
    }
}

then I have a class called invoice in which I can call this Article class: 那么我有一个称为发票的类,在其中可以称为“ Article”类:

public class Invoice{
     ArrayList<Article> detailList;
     public add(Article a){
          detailsList.add(a);
     }
     public ArrayList<Article> getArticleList(){
           return detailList;
     }
}

so I want that each time I make some articles in the main class and add those in the Invoie class to have the code generated automatically: 因此,我希望每次我在主类中编写一些文章并将其添加到Invoie类中时,以使代码自动生成:

main class

    ArrayList<Article> temp;
    Article a1=new Article(....)
    Article a2=new Article(....) 
    Article a3=new Article(....)
    Invoice inv;
    inv.add(a1)
    inv.add(a2)
    inv.add(a3)
    //for example I want the first element to get a code of 10, the next as 20 and so on
     temp=inv.getArticleList();
        for (int i=0;i<temp.size();i++){
            System.out.println(temp.get(i).getCod());
        }

I have tried using: 我尝试使用:

private static int cod in the Article class Article类中的private static int cod

and then adding +10 each time I call to the add method, but when I print the results from the list in the main class, it only prints me the last generated code; 然后每次调用add方法都加+10,但是当我从主类中的列表中打印结果时,它仅打印出我最后生成的代码; how can I fix that? 我该如何解决?

thanks 谢谢

You need two attributes, one static and one at the instance level: 您需要两个属性,一个为static属性,另一个为实例级别的属性:

public class Article {

    private int cod;
    private String name;
    private static int counter = 10;

    public Article(String name) {
        this.name = name;
        this.cod = counter;
        counter += 10;
    }

}

Using the above, each article will have a different code, starting at 10 and incrementing ten units each time. 使用以上内容,每篇文章将具有不同的代码,从10开始,每次增加10个单位。 I tested it using this Invoice class, which fixes some errors in the posted code: 我使用此Invoice类对其进行了测试,该类修复了已发布代码中的一些错误:

public class Invoice {
    ArrayList<Article> detailList = new ArrayList<Article>();
    public void add(Article a) {
         detailList.add(a);
    }
    public ArrayList<Article> getArticleList(){
          return detailList;
    }
}

Now this works as expected: 现在,这可以按预期工作:

Invoice inv = new Invoice();
inv.add(a1);
inv.add(a2);
inv.add(a3);
ArrayList<Article> temp = inv.getArticleList();
for (int i=0;i<temp.size();i++){
    System.out.println(temp.get(i).getCod());
}

It prints on the console: 它在控制台上打印:

10
20
30

Here's a thread safe version of Oscar's code: 这是Oscar代码的线程安全版本:

import java.util.concurrent.atomic.AtomicInteger;

public class Article {
  private int cod;
  private String name;
  private final static AtomicInteger counter = new AtomicInteger(10);

  public Article(String name) {
    this.name = name;
    this.cod = counter.getAndAdd(10);
  }
}

Using an AtomicInteger will allow you to create instances of Article from many threads concurrently. 使用AtomicInteger将允许您同时从多个线程创建Article的实例。 Using the regular int will likely cause "dirty" reads of the counter and thus different instances of Article will get different values of the counter. 使用常规int可能会导致计数器的“脏”读取,因此Article的不同实例将获得不同的计数器值。

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

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