简体   繁体   中英

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. (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.

As the JLS says it:

Keyword this denotes a value that is a reference to the object for which the instance method or default method was invoked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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