Java—初学—InetAdress类

InetAdress类

          没有构造方法

---------------------------------------------------

如果一个类没有构造方法,可能是以下三种情况:

A:成员全部是静态的(Math,Arrays,Collections)

B:单例设计模式

C:类中有静态方法返回该类的对象(InetAdress)

class Demo{
        private Demo();
        public static Demo getXxx(){
              return new Demo();}
       }

InetAddress的成员方法:

       public static InetAddress getByName(String host):根据主机名或者IP地址的字符串表示得到

IP地址。

package cn.itcast_01;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class InetAddressDemo {
            public static void main(String[] args) throws UnknownHostException {   	 
            	                 //根据主机名
				 //InetAddress address = InetAddress.getByName("DESKTOP-0257CLH");
				 
				//根据IP地址
				 InetAddress address = InetAddress.getByName("10.141.25.35");
				 
				 //获得两个东西 主机名,Ip地址
				 
				 String name = address.getHostName();
				 String ip = address.getHostAddress();
				 
				 System.out.println(name+"------"+ip);
				   //DESKTOP-0257CLH------10.141.25.35
			}
}











猜你喜欢

转载自blog.csdn.net/u013116760/article/details/80194827