Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inner class modifying owning class's attribute

I have a code like this:

class Foo() {
    time_to_play = 0
    class Bar() {
        void change_player() {
            //I need something HERE
        }
    }

}

And I need to change the attribute time_to_play from class Foo, but make this change from inside the method change_player(), that is under class Bar.

I cannot declare class Bar outside class Foo, and make an 'extend', and call super. ..., because it'd break the OO in my case.

Also, I don't want to make time_to_play a static variable, calling Foo.time_to_play

How I can do this?

like image 425
Gabriel L. Oliveira Avatar asked Jun 10 '26 23:06

Gabriel L. Oliveira


1 Answers

What you want is:

void change_player() {
    Foo.this.time_to_play = // something
}
like image 176
jjnguy Avatar answered Jun 13 '26 13:06

jjnguy