在Java中,我如何在其自己的类中引用整个对象?

问题描述 投票:0回答:1

我在名为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 
        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;
        }

    }
java object
1个回答
0
投票

您使用关键字this来引用(整个)当前对象。

[JLS说:

关键字this表示一个值,该值是对为其调用实例方法或默认方法的对象的引用。

© www.soinside.com 2019 - 2024. All rights reserved.