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

1,用接口实现多态
 

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)