对一些java语法感到困惑
我正在从头开始(慢慢地)学习 Java,但时不时地,我会在一些“真实”代码中达到顶峰,然后发现一些让我困惑的东西。
以下行和类似的行:
JPanel panel = (JPanel) container.getContentPane();
我的问题是 (JPanel)
和 container.getContentPane()
之间发生了什么?它们不像是在倍增吧?
我知道这是该语言的基本部分,随着我继续学习,我将进入这一部分,但我真的很好奇,想立即知道它是什么。我真的不知道该用谷歌搜索什么才能得到答案。
I am learning Java (slowly) from the ground up, but every now and again I peak at some "real" code and I found something that confused me.
The following line and ones like it:
JPanel panel = (JPanel) container.getContentPane();
My question is what is happening between (JPanel)
and container.getContentPane()
? Its not like they are being multiplied right?
I know this is a basic part of the language and as I continue learning I'll get to this part, but I got really curious and wanted to know what it was right away. I didn't really know what to google to get the answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不。它的意思是“获取这个东西并将其视为 JPanel”。这称为类型转换,该语法用于 C++、C# 和许多其他语言。
您必须确保类之间的相互关联方式允许进行强制转换。例如,这是行不通的:
JPanel
是JComponent
,JButton
也是,但JButton
不是从JPanel
下降,因此您无法在这些对象之间进行转换。您还可以从子级转换回父级,例如从JSpinner.DefaultEditor
转换回JPanel
,但不能从JPanel
转换为JSpinner.DefaultEditor
。No. It means "get this thing and treat it as a JPanel." It's called type casting and that syntax is used in C++, C# and many other languages.
You have to make sure that the way that the classes are related to each other allows for casting. For example, this wouldn't work:
JPanel
is aJComponent
and so isJButton
, butJButton
does not descend fromJPanel
thus you cannot cast between these objects. You can also cast from a child back to a parent, such as fromJSpinner.DefaultEditor
back toJPanel
, but not fromJPanel
toJSpinner.DefaultEditor
.这称为类型转换。
getContentPane()
方法通常返回一个 Container,但我们希望将其获取到 JPanel 中,以便我们能够使用 JPanel 方法。当然,这两种类型必须兼容(例如,JPanel 是 Container 的特定实现(通过 JComponent))This is called a type cast. The method
getContentPane()
normally return a Container, but we want to get it in a JPanel so we'll be able to use JPanel methods. Of course the two types have to be compatible (JPanel is a specific implementation of Container (through JComponent) for example)它是一个强制转换表达式(请参阅 JLS 15.16,强制转换表达式)。
它的意思是“将
getContentPane()
调用的结果视为JPanel
”。转换可能会失败,导致
ClassCastException
(请参阅 JLS 5.5,转换转换)。It's a cast expression (see JLS 15.16, Cast Expressions).
It means "treat the results of the
getContentPane()
call as aJPanel
".Casts can fail, causing a
ClassCastException
(see JLS 5.5, Casting Conversion).是的,这就是 java 中类型转换的方式。如您所知,java 使用引用。在您的情况下,您有一个 JPanel 类型引用,并且 container.getContentPane() 返回(如果我错了,请纠正我,我的 Swing 有点生锈)容器,因此由于这两个不兼容,您需要进行类型转换以使它们兼容。希望有帮助
Yes this is how type casting is made in java. As you know java works with references. In your case you have a JPanel type reference and container.getContentPane() returns (correct me if I am wrong my Swing is a bit rusty) container, so since those two are incompatible you need to type cast to make them compatible. Hope that helps