2015-11-29 3 views
0

Просто нужно, чтобы кто-нибудь быстро показал мне, как я могу получить macAddress в классе Address для генерации в классе сетевых устройств, спасибо.получить строки из других классов java

Адрес класса

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

} 
} 

ответ

2

Меняем главный в классе Address в функцию, как так:

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

}