java “.class”属性如何工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10076629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does a '.class' property work?
提问by LongInt
First time developing in Java, and first time developing for Android, so it's quite a newbie question.
第一次用 Java 开发,也是第一次为 Android 开发,所以这是个新手问题。
I currently have this code:
我目前有这个代码:
public void onBtnClicked(View v){
/** Handles button navigation */
@SuppressWarnings("rawtypes")
Class c;
int viewId = v.getId();
switch (viewId) {
case R.id.btnNewTourny :
c = NewTourny.class;
break;
case R.id.btnTeamEditor :
c = EditTeam.class;
break;
case R.id.btnCatEditor :
c = EditCat.class;
break;
case R.id.btnLoadTourny :
c = EditCat.class;
break;
case R.id.btnNewCategory :
c = NewCat.class;
break;
default :
c = Main.class;
}
Intent myIntent = new Intent(v.getContext(), c);
startActivityForResult(myIntent, 0);
}
Short question:
简短的问题:
What does the .classproperty do, f.ex. in 'c = NewTourny.class'?
什么是的.class属性来完成,f.ex. 在' c = NewTourny .class' 中?
Why can't I cast c as Tourny (which is the parent of all these classes)?
为什么我不能将 c 转换为 Tourny(它是所有这些类的父类)?
Long question:
长问题:
This currently handles all button navigations throughout my app, and works fine. However, as you can see, I've suppressed a 'rawtype' warning, that comes when I cast c as a Class . Now, I really want a code without warnings, so I thought 'well, all these classes are a child of 'Tourny'', so I thought it would work, casting c as Tourny - but that does not work. Then I realised, that I didn't really know what the code "NewTourny.class" actually did, and that may be the clue to why I couldn't cast c as Tourny. So, what does .class do?
这当前处理整个应用程序中的所有按钮导航,并且工作正常。但是,正如您所看到的,我抑制了“原始类型”警告,当我将 c 转换为 Class 时就会出现这种警告。现在,我真的想要一个没有警告的代码,所以我想'好吧,所有这些类都是'Tourny'的子类'',所以我认为它会起作用,将c转换为Tourny - 但这不起作用。然后我意识到,我真的不知道代码“NewTourny.class”实际上做了什么,这可能是为什么我不能将 c 转换为 Tourny 的线索。那么,.class 有什么作用呢?
采纳答案by Edwin Buck
It doesn't really domuch. NewTourney.class
is more of a language specific way to write "the object which is the Class of NewTourney
". This shorthand allows one to refer to the class ofa NewTourney
, as opposed to dealing with specific already existinginstances of NewTourney
.
它真的没有太大作用。 NewTourney.class
更像是一种语言特定的方式来编写“作为类的对象NewTourney
”。这速记允许一个指的是类的一个NewTourney
,而不是处理具体现有的实例NewTourney
。
In your specific case, I would hazard to guess that the Activity
eventually creates an instance of the classto handle the action, or at least to hold the action's context sensitive data.
在您的特定情况下,我会猜测Activity
最终会创建类的一个实例来处理操作,或者至少保存操作的上下文敏感数据。
回答by Nasif Md. Tanjim
Instances of the class where Classrepresent classes and interfaces in a running Java application. This is part of java Reflection API. From Oracle tutorial:
类的实例,其中Class代表正在运行的 Java 应用程序中的类和接口。这是 Java 反射 API 的一部分。来自 Oracle教程:
If the type is available but there is no instance then it is possible to obtain a Class by appending ".class" to the name of the type. This is also the easiest way to obtain the Class for a primitive type.
如果类型可用但没有实例,则可以通过将“.class”附加到类型名称来获取类。这也是获取原始类型的 Class 的最简单方法。
Here are some references:
以下是一些参考:
回答by horbags
Class is a class that represents classes. :)
类是代表类的类。:)
It is not "Object". It is not a superclass or even in your "NewTourny" hierarchy. That is why you can't cast from a NewTourney to a Class.
它不是“对象”。它不是超类,甚至不是您的“NewTourny”层次结构。这就是为什么您不能从 NewTourney 转换为 Class 的原因。
The .class property is an instance of Class that represents type information.
.class 属性是 Class 的一个实例,表示类型信息。
You can use it to do reflective style calls to instantiate something you don't know the type of at compile time.
您可以使用它来进行反射样式调用,以实例化您在编译时不知道的类型。
回答by phlogratos
To get rid of the warning, you can change Class c
with Class<? extends Tourny>
. This means c is not an object, that represents one of all possible classes, but more specifically represents a class derived by Tourny
.
要摆脱警告,您可以更改Class c
为Class<? extends Tourny>
。这意味着 c 不是一个对象,它表示所有可能的类之一,而是更具体地表示由 派生的类Tourny
。
回答by Michael
The .class method is one all objets have which allow you to reference the original nature of that object from before you even had a chance to initialize it. It's not the parent, it's the original unadulterated template of itself. It's called ".class" because it's a direct representation of the class that only the class definition can influence. It saves you from having to make a new object in order to obtain information all members of that class originally have in common.
.class 方法是所有对象都拥有的方法,它允许您在有机会初始化它之前就引用该对象的原始性质。它不是父对象,而是它自己原始的纯正模板。它被称为“.class”,因为它是类的直接表示,只有类定义可以影响。它使您不必创建一个新对象来获取该类的所有成员最初共有的信息。