在Java中,如何在一个类中引用整个对象?
我在一个叫做Pokemon的类中有这个,我想做的是引用当前对象作为一个整体,并通过int.use传递它。(我知道您可以根据使用它的对象引用特定的数据片段,因此我假设您可以为整个对象这样做)任何帮助都将不胜感激在Java中,如何在一个类中引用整个对象?,java,object,Java,Object,我在一个叫做Pokemon的类中有这个,我想做的是引用当前对象作为一个整体,并通过int.use传递它。(我知道您可以根据使用它的对象引用特定的数据片段,因此我假设您可以为整个对象这样做)任何帮助都将不胜感激 public boolean attack(int m,Pokemon p) { boolean hit = true; //check what move the use and take away a pp and return it
public boolean attack(int m,Pokemon p) {
boolean hit = true;
//check what move the use and take away a pp and return it
if (m == 1) {
hit = m1.use(p.getType1(), p.getType2(),**this is where I want to pass the object**);
if (hit == true) {
return true;
} else {
return false;
}
} else if (m == 2) {
hit = m2.use(p.getType1(), p.getType2(),**this is where I want to pass the object**);
if (hit == true) {
return true;
} else {
return false;
}
} else if (m == 3) {
hit = m3.use(p.getType1(), p.getType2(),**this is where I want to pass the object**);
if (hit == true) {
return true;
} else {
return false;
}
} else if (m == 4) {
hit = m4.use(p.getType1(), p.getType2(),**this is where I want to pass the object**);
if (hit == true) {
return true;
} else {
return false;
}
} else {
return true;
}
}
您可以使用关键字
此
来引用(整个)当前对象
正如政府所说:
关键字此
表示引用实例方法或默认方法的对象的值
请参阅
公共布尔攻击(int m,Pokemon p)
的定义-这就是传递整个对象的方式。但m1究竟是什么呢?它是什么类型的对象以及如何定义use
呢?仅供参考:if(hit==true){return true;}否则{return false;}
应该是return hit代码>。if
语句是多余的。如果您有一个m?
对象数组,所有这些代码都可以在几行内完成