简体   繁体   English

尝试创建新控制台时为什么会出现异常?

[英]Why do I get an exception when trying to create a new Console?

import java.io.*;

public class Talk {
    public static void main(String[] args) {
        Console c = new Console();
        String pw;
        System.out.print("password: ");
        pw = c.readLine();
        System.out.println("got " + pw);
    }
}

Please explain to me this compiler exception at console 请在控制台向我解释此编译器异常

java:5: error: Console() has private access in Console Console c = new Console(); java:5:错误:Console()在控制台中具有私有访问权限控制台c = new Console(); ^ 1 error ^ 1个错误

The Console class is obviously not meant to be instantiated directly. Console类显然不是要直接实例化的。 You are supposed to use System.console instead: 您应该使用System.console代替:

If this virtual machine has a console then it is represented by a unique instance of this class which can be obtained by invoking the System.console() method. 如果此虚拟机具有控制台,那么它将由此类的唯一实例表示,可以通过调用System.console()方法获得该实例。 If no console device is available then an invocation of that method will return null. 如果没有控制台设备可用,则对该方法的调用将返回null。

See the docs . 请参阅文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM