java 不创建实例化,Java使用实例方法而不是类/静态方法为每个实例化对象创建唯一的ID...

本文讨论了在Java中如何为类的每个实例分配唯一的ID号。问题在于,当前ID变量是实例变量,导致每次创建新对象时ID重置为0。解决方案是将ID变量改为静态,使得它属于类而不是实例,从而确保所有对象共享同一个计数器。通过这样做,可以实现连续的ID分配。
摘要由CSDN通过智能技术生成

Quite new to this so I hope I have the terminology in the title right.

I am trying to figure out how to create an instance method that will do the following:

--An ID number is returned.

--As each object is created from the class constructor(instantiated?), a unique integer ID number is assigned to it. The first ID number is 1, and as new objects are instantiated, successive numbers will be assigned.

I am able to find examples of class/static methods that do the above however I am unable to figure out how to do this with an instance method. My attempt is below:

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;

}

}

My main method is as follows:

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());

}

}

Please note I have not included my entire code for the sake of simplicity and easiness to follow. I hope I have added enough.

I also don't want to use java.util.UUID.

解决方案

The problem right now is that your 'id' is an instance variable, meaning it belong to the objects you create.

Think of it that every time you create an object a new and fresh copy of your instance variable is made.

So every time you create an object the id is first set to 0, then post incremented once (thus all objects have an id=0).

If you want to create a variable that, say, automatically counts all objects you have created in a class or has the id, you need to make a class variable.

These variable belong to all the objects you create from a class and the keyword used for that is 'static'.

Note: I have used a static variable BUT not a static method. If you don't want to use static at all, it is a different question

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;

}

}

A simple change of keyword will make your program correct. You dont have to do too much complicated stuff.

It is difficult to grasp the concept of class and objects, static and instance variables at first. Let me know if you'd like more explanation :)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值