public class InetAdress {
public static void main(String[] args) throws UnknownHostException {
InetAddress inetAddress = InetAddress.getByName("192.168.100.112");
// InetAddress inetAddress = InetAddress.getByName("DESKTOP-O1HAIQJ");
String hostName = inetAddress.getHostName();
// 获取主机名
System.out.println("hostName => " + hostName);
// 获取 IP 地址
String hostAdress = inetAddress.getHostAddress();
System.out.println("hostAdress => " + hostAdress);
}
}
注意到: InetAddress 使用 InetAddress.getByName("192.168.100.112");
。
如果一个类没有构造方法:
1、成员全部是静态的(例如:Math、Arrays、Collections);
2、单例设计模式(例如:Runtime);
3、类中有静态方法返回该类的对象(InetAddress)。
代码示例:
class Demo {
private Demo(){}
public static Demo getXxx() {
return new Demo();
}
}
看InetAddress的成员方法:
public static InetAddress getByName(String host):根据主机名或者IP地址的字符串表示得到IP地址对象
Runtime 使用举例
public class RuntimeDemo {
public static void main(String[] args) throws IOException {
Runtime runtime = Runtime.getRuntime();
runtime.exec("notepad");
runtime.exec("calc");
runtime.exec("shutdown -s -t 10000");
runtime.exec("shutdown -a");
}
}