Socket 学习笔记——InetAdress 最简单的体验

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");
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值