InetAddress类的使用
没有构造方法,那么如何使类提供的功能呢?
没有构造方法的情况:
A:成员全部是静态的(Math,Arrays,Collection)
B : 单例设计模式(Runtime)
C:类中有静态方法返回该类的对象(InetAddress)
要掌握的功能:
获取任意主机:getByName
主机名:getHostName
主机Ip地址:getHostAddress
例子1:InetAddress类的使用
import java.net.InetAddress;
import java.net.UnknownHostException;
public classInetAddressDemo {
public static void main(String[] args) throws UnknownHostException {
// TODO Auto-generated method stub
//获取IP地址对象
// InetAddressaddress = InetAddress.getByName("wymA");
InetAddressaddress = InetAddress.getByName("219.216.80.19");
// 获取主机名
Stringname = address.getHostName();
// 获取IP地址
Stringip = address.getHostAddress();
System.out.println(name + "---" + ip);
}
}
运行结果:
219.216.80.20---219.216.80.20