如何做一个'的.class“物业工作? [英] How does a '.class' property work?

查看:137
本文介绍了如何做一个'的.class“物业工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一次开发在Java中,和第一次开发的机器人,所以这是一个相当新手问题。

First time developing in Java, and first time developing for Android, so it's quite a newbie question.

目前,我有这个code:

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);
}

短的问题:

什么的的的.class 的属性在做,f.ex.在 C = NewTourny 的.class

What does the .class property do, f.ex. in 'c = NewTourny.class'?

为什么我不能投c以图尔尼(这是所有这些类的父类)?

Why can't I cast c as Tourny (which is the parent of all these classes)?

长的问题:

本目前处理所有的导航按钮在我的应用程序,并能正常工作。然而,正如你所看到的,我晚饭pressed一个'rawtype的警告,那来当我投C作为类。 现在,我真的想不警告,一个code,所以我想'哦,这些类是孩子的图尔尼',所以我认为这是可行的,铸件C的图尔尼 - 但是,这并不正常工作。这时我才意识到,我真的不知道是什么codeNewTourny.class其实做了,这可能是线索,为什么我不能投c以图尔尼。那么,什么是的.class办?

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?

推荐答案

它并没有真正的执行的多。 NewTourney.class 更多的是一种特定语言的方式来写,这是类 NewTourney 的对象。这速记允许一个参考的类的的一个 NewTourney ,系统相对于处理特定的现有的实例 NewTourney

It doesn't really do much. 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 of a NewTourney, as opposed to dealing with specific already existing instances of NewTourney.

在特定的情况下,我大胆猜测,该活动最终产生的的的处理动作,或者在一个实例至少要保持动作的上下文敏感的数据。

In your specific case, I would hazard to guess that the Activity eventually creates an instance of the class to handle the action, or at least to hold the action's context sensitive data.

这篇关于如何做一个'的.class“物业工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆