我正在检查java.net.InetAddressClass 的源文件,发现很少有方法总是返回false.例如
/**
* Utility routine to check if the InetAddress is a site local address.
*
* @return a <code>boolean</code> indicating if the InetAddress is
* a site local address; or false if address is not a site local unicast address.
* @since 1.4
*/
public boolean isSiteLocalAddress() {
return false;
}
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?为什么我们需要一个总是返回false的方法?对于此类中以"is"开头的所有方法,它们都是相同的.请参阅http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/net/InetAddress.java
InetAddress有一些子类可以覆盖这些方法并返回有用的结果.
与Inet4Address一样:根据RFC 1918,如果地址是"本地",则返回true.有一行注释:
// refer to RFC 1918
// 10/8 prefix
// 172.16/12 prefix
// 192.168/16 prefix
Run Code Online (Sandbox Code Playgroud)
对于无上下文的 Internet地址(=非IPv4而不是IPv6),返回是有意义的,false因为没有上下文的本地站点不存在.