对一些java语法感到困惑

发布于 2024-12-27 04:36:30 字数 336 浏览 1 评论 0原文

我正在从头开始(慢慢地)学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

衣神在巴黎 2025-01-03 04:36:30

这不像是它们正在成倍增加吧?

不。它的意思是“获取这个东西并将其视为 JPanel”。这称为类型转换,该语法用于 C++、C# 和许多其他语言。

您必须确保类之间的相互关联方式允许进行强制转换。例如,这是行不通的:

JPanel p = new JPanel();
JComponent c = (JComponent)p;
JButton b = (JButton)c;

JPanelJComponentJButton 也是,但 JButton 不是从 JPanel 下降,因此您无法在这些对象之间进行转换。您还可以从子级转换回父级,例如从 JSpinner.DefaultEditor 转换回 JPanel,但不能从 JPanel 转换为 JSpinner.DefaultEditor

Its not like they are being multiplied right?

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 p = new JPanel();
JComponent c = (JComponent)p;
JButton b = (JButton)c;

JPanel is a JComponent and so is JButton, but JButton does not descend from JPanel thus you cannot cast between these objects. You can also cast from a child back to a parent, such as from JSpinner.DefaultEditor back to JPanel, but not from JPanel to JSpinner.DefaultEditor.

不…忘初心 2025-01-03 04:36:30

这称为类型转换。 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)

幻想少年梦 2025-01-03 04:36:30

它是一个强制转换表达式(请参阅 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 a JPanel".

Casts can fail, causing a ClassCastException (see JLS 5.5, Casting Conversion).

缱绻入梦 2025-01-03 04:36:30

是的,这就是 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文