简体   繁体   English

对某些Java语法感到困惑

[英]Confused about some java syntax

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. 我从头开始(慢慢地)学习Java,但是我时不时地遇到一些“真实的”代码,我发现有些东西使我感到困惑。

The following line and ones like it: 以下行和类似的行:

JPanel panel = (JPanel) container.getContentPane();

My question is what is happening between (JPanel) and container.getContentPane() ? 我的问题是(JPanel)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. 我真的不知道该怎么用谷歌获得答案。

Its not like they are being multiplied right? 它不是像他们被成倍增加吗?

No. It means "get this thing and treat it as a JPanel." 否。它的意思是“得到该东西并将其视为JPanel。” It's called type casting and that syntax is used in C++, C# and many other languages. 这称为类型转换,该语法在C ++,C#和许多其他语言中使用。

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. JPanel是一个JComponentJButton也是一个JComponent ,但JButton并非派生自JPanel因此您无法在这些对象之间进行转换。 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 . 您还可以从子级转换回父级,例如从JSpinner.DefaultEditorJPanel ,但不能从JPanelJSpinner.DefaultEditor

It's a cast expression (see JLS 15.16, Cast Expressions ). 这是一个强制转换表达式(请参见JLS 15.16,强制转换表达式 )。

It means "treat the results of the getContentPane() call as a JPanel ". 它的意思是“将getContentPane()调用的结果作为JPanel ”。

Casts can fail, causing a ClassCastException (see JLS 5.5, Casting Conversion ). 强制转换可能会失败,导致ClassCastException (请参见JLS 5.5,强制转换 )。

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. 方法getContentPane()通常返回一个Container,但是我们希望在JPanel中获取它,因此我们将能够使用JPanel方法。 Of course the two types have to be compatible (JPanel is a specific implementation of Container (through JComponent) for example) 当然,这两种类型必须兼容(例如,JPanel是Container的特定实现(例如,通过JComponent))

Yes this is how type casting is made in java. 是的,这就是用Java进行类型转换的方式。 As you know java works with references. 如您所知,java可与引用一起使用。 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. 在您的情况下,您有一个JPanel类型引用,并且container.getContentPane()返回(如果我错了,请纠正我,我的Swing有点生锈)容器,因此,由于这两个不兼容,您需要键入强制转换以使其兼容。 Hope that helps 希望能有所帮助

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

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