java中多态三种实现方式之一_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java中多态三种实现方式之一

java中多态三种实现方式之一

 2014/9/3 21:45:18  jkisss  程序员俱乐部  我要评论(0)
  • 摘要:1,用接口实现多态packagecom.tarena.framework;importandroid.graphics.Canvas;importandroid.graphics.Paint;publicinterfaceBaseState{publicvoiddraw(Canvascanvas,Paintpaint);publicvoidEventTouch(intx,inty);}packagecom.tarena.framework;importcom.tarena.state
  • 标签:实现 Java 方式
1,用接口实现多态
class="java" name="code">

package com.tarena.framework;

import android.graphics.Canvas;
import android.graphics.Paint;

public interface BaseState {
public void draw(Canvas canvas,Paint paint);
public void EventTouch(int x,int y);

}


package com.tarena.framework;

import com.tarena.state.GameState;
import com.tarena.state.HelpState;
import com.tarena.state.MainMenuState;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.View;

public class Main extends View {
public static int currentState;
private HelpState helpstate;
private MainMenuState mainmenustate;
private GameState gamestate;
public Main(Context context){
   super(context);
   currentState=1;
   helpstate=new HelpState();
   mainmenustate=new MainMenuState();
   gamestate=new GameState();
}
@Override
protected void onDraw(Canvas canvas) {
Paint paint=new Paint();
paint.setTextSize(30);
paint.setColor(0xFFFF0000);
switch(currentState){
case 1:
mainmenustate.draw(canvas, paint);
break;
case 2:
gamestate.draw(canvas, paint);
break;
case 3:
helpstate.draw(canvas, paint);
break;


}
super.onDraw(canvas);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
int x=(int) event.getX();
int y=(int) event.getY();
switch(currentState){
case 1:
mainmenustate.EventTouch(x, y);
break;
case 2:
gamestate.EventTouch(x, y);
break;
case 3:
helpstate.EventTouch(x, y);
break;
}

invalidate();
return super.onTouchEvent(event);
}

}


package com.tarena.state;

import android.graphics.Canvas;
import android.graphics.Paint;

import com.tarena.framework.BaseState;
import com.tarena.framework.Main;

public class GameState implements BaseState {

@Override
public void draw(Canvas canvas, Paint paint) {
canvas.drawText("game", 150, 200, paint);
}

@Override
public void EventTouch(int x, int y) {
if(x>200&&y>300){
Main.currentState=1;
}

}

}


package com.tarena.state;

import android.graphics.Canvas;
import android.graphics.Paint;

import com.tarena.framework.BaseState;
import com.tarena.framework.Main;

public class HelpState implements BaseState{

@Override
public void draw(Canvas canvas, Paint paint) {
        canvas.drawText("help", 150, 200, paint);
}

@Override
public void EventTouch(int x, int y) {
          if(x>200){
          Main.currentState=1;
          }
}


}


package com.tarena.state;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;

import com.tarena.framework.Main;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Main main=new Main(this);
setContentView(main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}


package com.tarena.state;

import android.graphics.Canvas;
import android.graphics.Paint;

import com.tarena.framework.BaseState;
import com.tarena.framework.Main;

public class MainMenuState implements BaseState{

@Override
public void draw(Canvas canvas, Paint paint) {
canvas.drawText("game", 100,50, paint);
canvas.drawText("help", 100, 250, paint);

}

@Override
public void EventTouch(int x, int y) {
if(y>50){
Main.currentState=3;
}else{
Main.currentState=2;
}

}

}
上一篇: Java生成二维码(案例源码) 下一篇: 没有下一篇了!
发表评论
用户名: 匿名