从其他类java中获取字符串

时间:2015-11-29 23:19:36

标签: java string class

只需要有人快速告诉我如何在Address类中获取macAddress以在Network设备类中生成,谢谢。

  

地址类

public class Address {

public static void main(String args[]){
Random rand = new Random();

    String result1 = String.format("%x",(int)(Math.random()*100));
    String result2 = String.format("%x",(int)(Math.random()*100));
    String result3 = String.format("%x",(int)(Math.random()*100));
    String result4 = String.format("%x",(int)(Math.random()*100));
    String result5 = String.format("%x",(int)(Math.random()*100));
    String result6 = String.format("%x",(int)(Math.random()*100));

String macAddress = (result1 + ":" + result2 + ":" + result3 + ":" + result4 + ":" + result5 + ":" + result6);
}
}
  

NetworkDevice类

public class NetworkDevice {
//get it to generate a new address from address class and print it out


public static void main (String args[]){
    Address thisAddress = new Address();
    System.out.println(thisAddress);

}
}

1 个答案:

答案 0 :(得分:2)

所以将Address类中的main更改为如下函数:

public class Address {

    public static String Macgen() {
        Random rand = new Random();

        String result1 = String.format("%x", (int) (Math.random() * 100));
        String result2 = String.format("%x", (int) (Math.random() * 100));
        String result3 = String.format("%x", (int) (Math.random() * 100));
        String result4 = String.format("%x", (int) (Math.random() * 100));
        String result5 = String.format("%x", (int) (Math.random() * 100));
        String result6 = String.format("%x", (int) (Math.random() * 100));

        String macAddress = (result1 + ":" + result2 + ":" + result3 + ":" +

        result4 + ":" + result5 + ":" + result6);
        return macAdress;
    }
}

像这样调用此函数

public class NetworkDevice {

public static void main (String args[]){
    System.out.println(Address.Macgen());

}