Can I have multiple run methods in a class?

前端 未结 5 836
眼角桃花
眼角桃花 2021-01-03 03:21

If so, how? I need to have 2 run methods in each object I will instantiate.

I meant a run method for threading.

What i need is more like a race of two cars.

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-03 03:59

    What for? I assume you're talking about implementing the runnable interface, so you want two methods with the signature: public void run();?

    Having two methods with the same signature makes no sense; how would you distinguish between them when calling the method from elsewhere in your code?

    If you want two different things to happen based on a certain condition when run() is invoked, then you need to add a conditional statement at the start of the method:

    public void run() {
    
        if (some_condition) {
    
            // code for the first scenario
    
        } else {
    
            // code for the second
    
        }
    
    }
    

提交回复
热议问题