我正在尝试理解本教程的代码: Java serial 特别是在:
"if ( commPort instanceof SerialPort )"
返回 true
"commPort" is CommPort class object
和 “SerialPort”是继承CommPort类的类
commPort 怎么可能是 SerialPort 类的实例。
正确的例子是:
串行端口串行端口;
"if ( serialPort instanceof CommPort )"
还是我错了? 谢谢...
最佳答案
how is possible that commPort could be instance of SerialPort class.
你说
"SerialPort" is a class that inherits of CommPort class
所以你可以这样做
CommPort commPort = new SerialPort();
if (commPort instanceof SerialPort) // true.
但是如果你写这样的东西
CommPort commPort = new ParallelPort();
if (commPort instanceof SerialPort) // false
关于java - instanceof 的罕见行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37091121/