Java使用实例方法而不是类

Java使用实例方法而不是类

本文介绍了Java使用实例方法而不是类/静态方法为每个实例化对象创建唯一的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这很新,所以我希望标题中有术语.

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:

-返回ID号.

-由于从类构造函数创建了每个对象(实例化?),因此为其分配了唯一的整数ID号.第一个ID号是1,并且在实例化新对象时,将分配连续的编号.

--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;
    }

}

我的主要方法如下:

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.

我也不想使用java.util.UUID.

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

推荐答案

当前的问题是您的"id"是一个实例变量,这意味着它属于您创建的对象.考虑一下,每次创建对象时,都会创建实例变量的新副本.因此,每次创建对象时,id都会先设置为0,然后发布一次,然后递增一次(因此所有对象的id均为0).

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).

如果要创建一个变量,例如,该变量可以自动对在类中创建的所有对象或具有ID的对象进行计数,则需要创建一个类变量.这些变量属于您从类创建的所有对象,并且用于该类的关键字为"static".

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'.

注意:我使用的是静态变量,而不是静态方法.如果您根本不想使用静态,则是一个不同的问题

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 :)

这篇关于Java使用实例方法而不是类/静态方法为每个实例化对象创建唯一的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 16:41