从 Java 中的另一个类访问 UUID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12602489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:31:12  来源:igfitidea点击:

Accessing UUID from another class in Java

java

提问by kamal

Here is the scenario: I generate Random Data in one class and use it as an argument in a method in the same class. How can i use the exact same value in another class ?

这是场景:我在一个类中生成随机数据,并将其用作同一类中方法的参数。我如何在另一个类中使用完全相同的值?

Here is a simplified version of my classes:

这是我的课程的简化版本:

public class A {
    @Test
    public void aMethod() {     
        RandomStringUUID ruuid = new RandomStringUUID();
        }
}

Then:

然后:

public class B { 
        @Test
    public void bMethod() {
        // Want to use ruuid , without instantiating RandomStringUUID 
                // as i dont want new values, i want the one from class A
        }
}

采纳答案by gtgaxiola

Make your variable a public static variable in class A

使您的变量成为 A 类中的公共静态变量

public static RandomStringUUID RUUID = new RandomStringUUID();

After the beatdown I'm getting in the voting count

击败后我进入投票计数

private RandomStringUUID uuid;

private createUuid() {
    uuid = new RandomStringUUID();  //Or any other way you create it
}

public static RandomStringUUID getRuuidForOtherClassesToCheck() {
    return ruuid;
}

Not enough info on the OP's part to infer it's design rules or just a simple JAVA question on how to get a Variable from Class to Class. :D

OP 部分没有足够的信息来推断它的设计规则,或者只是一个关于如何从类到类获取变量的简单 JAVA 问题。:D

回答by sics

use a static approach:

使用静态方法:

public class A {

     private final static RandomStringUUID ruuid = new RandomStringUUID();

     public final static RandomStringUUID getRuuid() {
         return ruuid;
     }

     @Test
     public void aMethod() {     
         RandomStringUUID ruuid = getRuuid();
     }
}

and

public class B { 
    @Test
    public void bMethod() {
         RandomStringUUID ruuid = A.getRuuid();
    }
}

回答by CPerkins

Either have A give the value to B

要么让 A 给 B 赋值

public class B {  
    @Test 
    public void bMethod(RandomStringUUID ruuid) { 
    // Want to use ruuid , without instantiating RandomStringUUID  
            // as i dont want new values, i want the one from class A 
    } 
} 

Or have A make it available to be accessed by B, as @sics said

或者让 A 使其可供 B 访问,正如@sics 所说

回答by Andreas Dolk

We sometimes test use helper classes. That could be a solution for your (test) case. So Awouldn't provide the value to Bbut both would obtain the same object from a third class:

我们有时会测试使用辅助类。这可能是您的(测试)案例的解决方案。所以A不会提供值,B但两者都会从第三个类获得相同的对象:

public class TestHelper {

  private final static RandomStringUUID UUID = new RandomStringUUID();

  public static RandomStringUUID getDummyRandomStringUUID() {
     return UUID;
  }
}


public class A {
  @Test
  public void aMethod() {     
    RandomStringUUID ruuid = TestHelper.getDummyRandomStringUUID();
  }
}

(and the same with B)

(与 相同B

It's still questionable for testing, because each test run uses a different UUID value, so the tests are not reproduceable. It would be better to define a static UUID that is used for every test run.

对于测试来说仍然有问题,因为每次测试运行使用不同的 UUID 值,因此测试是不可复制的。最好定义一个用于每次测试运行的静态 UUID。