android-context问题

117 发布于 2013/05/07 09:15
阅读 215
收藏 0
public class gameView extends View{
	private static final String TAG = "shudu";
	
	
    private static final String SELX = "selX"; 
	private static final String SELY = "selY";
	private static final String VIEW_STATE = "viewState";
	private static final int ID = 42; 
	
	private float width;
	private float height;
	private int selX;
	private int selY;
	

	private final Rect selRect = new Rect();
	private final game shuduGame;
	

	public gameView(Context context){

		super(context);
		this.shuduGame = (game)context; /*?*/
		setFocusable(true);/*允许获得焦点,可以用鼠标选择*/
		setFocusableInTouchMode(true);/*允许触摸控制获取焦点时,可以用手指选择*/
		setId(ID); 
	}
	


this.shuduGame = (game)context; /*?*/  这段该怎么理解?   context可以直接赋值给类的实例?
 

加载中
0
Jerikc
Jerikc

Context 定义:

Interface to global information about an application environment. This is an abstract class whose implementation

  is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls 

  for application-level operations such as launching activities, broadcasting and receiving intents, etc

 从上可知一下三点,即:

        1、它描述的是一个应用程序环境的信息,即上下文。

        2、该类是一个抽象(abstract class)类,Android提供了该抽象类的具体实现类(后面我们会讲到是ContextIml类)。

        3、通过它我们可以获取应用程序的资源和类,也包括一些应用级别操作,例如:启动一个Activity,发送广播,接受Intent

      信息 等。。



Context相关类的继承关系:


你的game类应该属于Activity,service或application中的一种吧,你强制转换Context对象为game类型。


另外附加一点个人建议,希望能规范你的编码。比如类名定义的规范等。个人愚见。

返回顶部
顶部