java实现重载()(附完整源码)
时间: 2023-10-15 07:02:04 浏览: 42
下面是一个简单的 Java 重载 () 实现的示例代码:
```
public class OverloadingExample {
public void print() {
System.out.println("无参 print 方法");
}
public void print(int num) {
System.out.println("带参 print 方法,参数为:" + num);
}
public void print(String str) {
System.out.println("带参 print 方法,参数为:" + str);
}
public static void main(String[] args) {
OverloadingExample oe = new OverloadingExample();
oe.print();
oe.print(10);
oe.print("Hello, world!");
}
}
```
该代码定义了一个名为 OverloadingExample 的类,其中包含三个名为 print 的方法,分别接受不同类型的参数。在 main 方法中,我们创建了 OverloadingExample 的一个实例,并调用了每个 print 方法。由于这些方法都具有相同的名称,但参数不同,因此我们可以称之为重载方法。输出结果如下:
```
无参 print 方法
带参 print 方法,参数为:10
带参 print 方法,参数为:Hello, world!
```
以上就是一个简单的 Java 重载 () 实现的示例代码。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)