Java 使用实例方法而不是 class/static 方法为每个实例化的 object 创建一个唯一 ID

Java create a unique ID for each instantiated object using instance methods instead of class/static methods

对此很陌生,所以我希望标题中的术语正确。

我正在尝试弄清楚如何创建一个实例方法,它将执行以下操作:

--返回一个身份证号码。

--由于每个 object 都是从 class 构造函数(实例化?)中创建的,因此会为其分配一个唯一的整数 ID 号。第一个 ID 号是 1,随着新的 object 实例化,将分配连续的编号。

我能够找到执行上述操作的 class/static 方法示例,但是我无法弄清楚如何使用实例方法执行此操作。我的尝试如下:

class Coordinates
{
    private int iD = 0;
    private float xCoordinate;
    private float yCoordinate;

    public Coordinates()
    {
        //Asks for input and assigns it to the two variables below
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Please enter the X Coordinate followed by the return key");
        xCoordinate = keyboard.nextDouble();
        System.out.println("Please enter the Y Coordinate followed by the return key");
        yCoordinate = keyboard.nextDouble();

        iD++;
    }

    public getiD()
    {
        return iD;
    }

}

我的主要方法如下:

public class Machine
{
    public static void main(String[] args)
    {
        Coordinates c1 = new Coordiantes();
        Coordinates c2 = new Coordiantes();
        Coordinates c3 = new Coordiantes();

        System.out.println("ID: " + c1.getID());
        System.out.println("ID: " + c2.getID());
        System.out.println("ID: " + c3.getID());


    }
}

请注意,为了简单易懂,我没有包含我的全部代码。希望我已经加够了。

我也不想用java.util.UUID。

您可以将该 ID 设置为静态,或者您可以只创建一个名为 "Coordinate" 的父级 class(该 ID 再次为静态)和 "Point" 个子级,然后递增每个 "Point" 对象的构造函数中的 ID。

静态似乎是可行的方法。

维护 ID 序列是独立于对象的其余部分的责任,并且不属于坐标 class 的任何一个实例,因此它属于不同的对象。制作一个单独的对象来维护序列并分发数字,例如

public class MySequence {
    private AtomicLong currentValue = new AtomicLong(0L);
    public long getNextValue() {
        return currentValue.getAndIncrement();
    }
}

然后使用该序列初始化您的对象:

new CoordinatePair(mySequence.getNextValue(), x, y);

顺便说一下,将用户输入与模型分开可以使事情变得更简单,如果输入不是来自用户,您可能希望实例化坐标 class。控制台输入不进入构造函数。你可能有一个像

这样的坐标class
public CoordinatePoint {
    private long id;
    private float x;
    private float y;
    public CoordinatePoint(long id, float x, float y) {
        this.id = id;
        this.x = x;
        this.y = y;
    }
    public String toString() {
        return "id=" + id + ", (" + x + ", " + y + ")";
    }
}

还有一个像

这样的主要方法
public class Example {

    public static void main(String ... args) {
        MySequence seq = new MySequence();
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Please enter the X Coordinate followed by the return key");
        float xCoordinate = keyboard.nextDouble();
        System.out.println("Please enter the Y Coordinate followed by the return key");
        float yCoordinate = keyboard.nextDouble();
        CoordinatePoint c1 = new CoordinatePoint(seq.getNextValue(), xCoordinate, yCoordinate);
        System.out.println(c1.toString());
    }
}

在您当前代码的上下文中,最简单的操作如下:

import java.util.concurrent.atomic.AtomicInteger;

public class Coordinates {

    //static id generator shared among all instances of Coordinates 
    private static final AtomicInteger idGenerator = new AtomicInteger(1000);

    private final Integer id;

    public Coordinates() {
        //assign unique id to an instance variable
        id = idGenerator.getAndIncrement();
    }

    public int getId() {
        //return instance variable
        return id;
    }
}

测试

public class Test {

    public static void main(String[] args) {
        for(int i = 0; i < 10; ++ i){
            System.out.println(new CoordinatePoint().getId());
        }
    }
}

输出

1000 1001 1002 1003 1004 1005 1006 1007 1008 1009

现在的问题是你的 'id' 是一个实例变量,这意味着它属于你创建的对象。 想一想,每次创建对象时,都会创建实例变量的新副本。 因此,每次创建对象时,id 首先设置为 0,然后 post 递增一次(因此所有对象的 id=0)。

如果你想创建一个变量,比如说,自动计算你在 class 中创建的所有对象或具有 id,你需要创建一个 class 变量。 这些变量属于您从 class 创建的所有对象,用于此的关键字是 'static'.

注意:我使用了静态变量但不是静态方法。如果你根本不想用static,那就另当别论了

class Coordinates
{
private static int count = 0;
private int id=0;
private float xCoordinate;
private float yCoordinate;

public Coordinates()
{
    //Asks for input and assigns it to the two variables below
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Please enter the X Coordinate followed by the return key");
    xCoordinate = keyboard.nextDouble();
    System.out.println("Please enter the Y Coordinate followed by the return key");
    yCoordinate = keyboard.nextDouble();

    id=count++;
}

public getiD()
{
    return iD;
}

}

关键字的简单更改将使您的程序正确。你不必做太多复杂的事情。

一开始很难掌握class和对象、静态和实例变量的概念。如果您需要更多解释,请告诉我:)