我可以在一个类中使用多个run方法吗? [英] Can I have multiple run methods in a class?

查看:128
本文介绍了我可以在一个类中使用多个run方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果是这样,怎么样?我需要在我将实例化的每个对象中有2个run方法。



我的意思是线程的run方法。



<我需要的更像是两辆车的比赛。每个汽车都应该在一个扩展Thread的类中有一个名称和一个run()方法。我将不得不打电话给我的主要两辆车,看看哪辆会赢。第一个或第二个。我需要帮助来启动我的程序

解决方案

一个类不能包含两个具有相同签名的方法。方法的签名是其名称后跟其参数。



因此,你可能有 run(int)在同一个类中运行(String) run()。这称为方法重载。你不能有两个 run()方法,因为你(和编译器)在调用 obj.run时无法知道要执行哪一个( )


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. each car should have a name and a run() method in a class which extends Thread. I will have to call on the two cars in my main to see which will win. The first or the second. I need help in starting my program

解决方案

A class can't contain two methods with the same signature. The signature of a method is its name followed by its arguments.

You may thus have run(int) and run(String) and run() in the same class. This is called method overloading. You can't have two run() methods, because you (and the compiler) could not know which one to execute when calling obj.run().

这篇关于我可以在一个类中使用多个run方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆