开发者

How can you make a java object override its methods?

开发者 https://www.devze.com 2023-04-07 04:23 出处:网络
Here\'s what I\'m thinking. So lets say I have a class called intro and I want to do something when it starts and finishes. I\'m wondering how could I do something like this:

Here's what I'm thinking. So lets say I have a class called intro and I want to do something when it starts and finishes. I'm wondering how could I do something like this:

开发者_开发技巧public Main(){
    //Calls the object that overrides its own methods
    new Intro(){
        @Override
        public void onStartIntro(){
        }

        @Override
        public void onFinishIntro(){
        }
    }

What would need to happen in the Intro class to enable something like this?


just create an abstract class to force overriding of the methods (but a non-abstract class with non-final methods will also do)

abstract class Intro
{
    abstract void onStartIntro();
    abstract void onFinishIntro();
}


Maybe Something like this:

public class Main(){
    public Main(){
        new Intro();
    }


    private class Intro extends SomeOtherClass{
        @Override
        public void onStartIntro(){ /*...Code...*/ }

        @Override
        public void onFinishIntro(){ /*...Code...*/ }
    }

}

Intro would only be available inside your "Main" class...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号