简体   繁体   English

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

[英]In Java how would I reference a whole object within a its own class?

I have this in a class called Pokemon and what I would like to do is reference the current object as a whole and pass it through int .use.我在一个名为 Pokemon 的类中有这个,我想做的是将当前对象作为一个整体引用并通过 int .use 传递它。 (I know that you can reference specific pieces of data based on the object using it so I would assume you could do it for the object as a whole) Any help would be greatly appreciated. (我知道您可以根据使用它的对象引用特定的数据片段,因此我假设您可以为整个对象执行此操作)任何帮助将不胜感激。


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;
        }

    }

You use keyword this to refer to (the whole of) the current object.您使用关键字this来引用(整个)当前对象。

As the JLS says it:正如JLS所说:

Keyword this denotes a value that is a reference to the object for which the instance method or default method was invoked.关键字this表示一个值,该值是对其调用实例方法或默认方法的对象的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM