Java 如何在android中从另一个类调用方法?
我在android studio上了两门课,学习一个游戏。板条箱级和玩家级。我想知道:我如何在板条箱类中调用player类?我试图通过Player thePlayer=newplayer(getContext())来实现这一点;但这只是不断给我错误的获取上下文部分Java 如何在android中从另一个类调用方法?,java,android,Java,Android,我在android studio上了两门课,学习一个游戏。板条箱级和玩家级。我想知道:我如何在板条箱类中调用player类?我试图通过Player thePlayer=newplayer(getContext())来实现这一点;但这只是不断给我错误的获取上下文部分 public class Crate { public static int acrossCrate; public int upDownCrate; Player thePlayer = new Player(getContext(
public class Crate {
public static int acrossCrate;
public int upDownCrate;
Player thePlayer = new Player(getContext()); //<--GIVES ERROR
public Crate(Context context) {
int rect = 1000;
int height = context.getResources().getDisplayMetrics().heightPixels;
int width = context.getResources().getDisplayMetrics().widthPixels;
int startPosX = (width/2)-(rect/2);
int startPosY = (height/2)-(rect/2);
acrossCrate = startPosX +300;
upDownCrate = startPosY +300;
}
public class Player {
public static int across;
public int upDown;
int boardWidth;
int boardHeight;
int startPosX;
int startPosY;
int stopPosX;
int stopPosY;
public Player(Context context) {
int rect = 1000;
boardHeight = context.getResources().getDisplayMetrics().heightPixels;
boardWidth = context.getResources().getDisplayMetrics().widthPixels;
startPosX = (boardWidth/2)-(rect/2);
startPosY = (boardHeight/2)-(rect/2);
stopPosX = (boardWidth/2)+(rect/2);
stopPosY = (boardHeight/2)+(rect/2);
across = startPosX+500;
upDown = startPosY+500;
}
公共级板条箱{
公共静态利率;
公共集装箱;
Player thePlayer=new Player(getContext());//将代码的第一部分转换为以下内容:
public class Crate {
public static int acrossCrate;
public int upDownCrate;
Player thePlayer;
public Crate(Context context) {
int rect = 1000;
int height = context.getResources().getDisplayMetrics().heightPixels;
int width = context.getResources().getDisplayMetrics().widthPixels;
int startPosX = (width/2)-(rect/2);
int startPosY = (height/2)-(rect/2);
acrossCrate = startPosX +300;
upDownCrate = startPosY +300;
//define your player here
thePlayer = new Player(context);
}
将Player-thePlayer=new-Player(getContext());
更改为Player-thePlayer;
并使用thePlayer=new-Player(context);
在crater
类构造中你应该真正阅读一本关于Java基础知识的书。
public class Crate {
public static int acrossCrate;
public int upDownCrate;
Player thePlayer ;
public Crate(Context context) {
thePlayer = new Player(context);
int rect = 1000;
int height = context.getResources().getDisplayMetrics().heightPixels;
int width = context.getResources().getDisplayMetrics().widthPixels;
int startPosX = (width/2)-(rect/2);
int startPosY = (height/2)-(rect/2);
acrossCrate = startPosX +300;
upDownCrate = startPosY +300;
}
public class Player {
public static int across;
public int upDown;
int boardWidth;
int boardHeight;
int startPosX;
int startPosY;
int stopPosX;
int stopPosY;
public Player(Context context) {
int rect = 1000;
boardHeight = context.getResources().getDisplayMetrics().heightPixels;
boardWidth = context.getResources().getDisplayMetrics().widthPixels;
startPosX = (boardWidth/2)-(rect/2);
startPosY = (boardHeight/2)-(rect/2);
stopPosX = (boardWidth/2)+(rect/2);
stopPosY = (boardHeight/2)+(rect/2);
across = startPosX+500;
upDown = startPosY+500;
}