【问题标题】:Static variables that are auto-generated and must be passed in constructor for object instantation自动生成的静态变量,必须在构造函数中传递以进行对象实例化
【发布时间】:2017-04-29 08:50:47
【问题描述】:

嗨,我是 Java 新手,我有一个任务,几天来我无法弄清楚如何为每个随机生成的对象属性设置唯一性。在我的班级预订中,我希望 kratisid 是唯一且随机的为每个对象生成,但如果我在 main() 方法的构造函数中这样做,我需要传递一个算术值而不是自动生成的值。这里是代码:

import java.utils.*;
import static org.apache.commons.lang3.RandomStringUtils.*;

public class reservation {

    String onoma;
    int afixi;
    int mdiam;
    int atoma;
    Domatio domat;//domat prosorinos deiktis sto object Domatio tha balo d meta
    Domatio d;
    Random rand = new Random();//this.kratisid=kratisid; kai stin main bazo random
    //random kratisi id
    static int kratisid = rand.nextInt(500) + 100;//It produces a random kratisid between 100 and 500

    String onoma = RandomStringUtils.randomAlphabetic(10);  //it produces a random alphabetic string for the onoma variable

    public reservation(String onoma, int kratisid, int afixi, int mdiam, int atoma, Domatio d)//boolean s)
    { //info for customer of a hotel reservation,d is reference to Domatio objects an another class
        d = null;

        this.onoma = onoma;   //System.out.println("Enter your name please");
        //Scanner scanner1= new Scanner(System.in);
        //onoma=scanner1.nextLine()         

        this.afixi = afixi;
        //System.out.println("Enter your day of arrival please ,it must be from 1 to 30 max");
        //Scanner scanner2=new Scanner(System.in);
        // afixi=scanner2.nextInt();
        // if(afixi<1 && afixi>30){
        //   afixi=0;
        //  System.out.println("Please re-enter your arrival within the month boundaries");
        //}
        this.mdiam = mdiam;
        //System.out.println("Please enter the number of days you will be staying");
        // Scanner scanner3=new Scanner(System.in);
        // mdiam=scanner3.nextInt();
        this.atoma = atoma;//

    }

现在在 main 方法中,当我创建对象时,例如 r,我需要将静态 kratisid 作为参数传递,但是我如何使对象具有静态随机 kratisid 而无需在 main 中的对象创建中传递它? 如果我没有告诉我尝试为每个对象制作一个唯一的随机 kratisid 和一个随机的 onoma,但如果用户想要,他可以输入他自己的。

【问题讨论】:

  • 请花时间提供minimal reproducible example,格式正确,没有随机注释掉的代码,遵循正常的命名约定。
  • 好的,但由于某种原因,我无法理解第一行总是会滑出对不起
  • 格式化您的代码@GeorgeJ!使用适当的缩进。几乎所有 IDE 都可以做到这一点:NetBeans、Eclipse、IDEA 等。
  • 这还能编译吗?你有一个 for 循环,你不能有一个!
  • 如果有人能提供建议将很有用,该项目的截止日期明天就结束了。

标签: java class variables random static


【解决方案1】:

如果你想创建具有不同随机初始化字段的对象,那么你应该考虑这样的事情:

public class Reservation {
    private static final Random r = new Random(0);
    private int kratisid = r.nextInt(500) + 100;
    // ...
}

请注意:

  • 随机数和唯一标识符是相关概念,但随机不等于唯一
  • 此解决方案存在严重的竞争条件,因为对象可能会干扰r

因此,请考虑以下解决方案:

public class Reservation {
    private static final Random r = new Random(0);
    private static Semaphore semaphore = new Semaphore(1);
    private static int getRandomInt() {
        final int res;
        semaphore.acquireUninterruptibly();
        res = r.nextInt(500) + 100;
        semaphore.release();
        return res;
    }
    private int kratisid = getRandomInt();
    // ...
}

【讨论】:

  • 使用 Semaphore 你想完成什么?你的代码很棒,但我不明白,使 kratisid 最终对于特定实例将具有相同的值对吗?还有关于我的预订对象我使用它来普遍处理预订类型的对象,因为我需要在其他类中是错误的吗?谢谢!
  • 我也不能让 kratisid 是静态的,练习中说要使用静态变量,以便每个对象都有一个唯一且随机的 id?
  • kratisid 是私有的,不是最终的,因此您可以更新其值。作为r 一个静态字段,如果多个对象同时调用r.nextInt,那么您最终可能会遇到并发问题(stackoverflow.com/questions/23396033/…)。一般来说,全局变量应该组织得当(en.wikipedia.org/wiki/Global_variable#Java)
  • kratisid 既不是静态的,也不是最终的。这是私人的
猜你喜欢
  • 2016-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多