首页 > Java > java教程 > 正文

使用方法重载来查找矩形面积的Java程序

王林
发布: 2023-08-19 20:45:17
转载
1416人浏览过

使用方法重载来查找矩形面积的java程序

我们可以使用方法重载在Java中计算矩形的面积。 "方法重载"是Java中的一个特性,它允许在同一个类中使用相同的方法名编写多个方法。这将使我们能够声明多个具有相同名称但具有不同签名的方法,即方法中的参数数量可能不同或参数的数据类型可能不同。方法重载帮助我们增加代码的可读性,以便我们可以以不同的方式使用相同的方法。

Now, let us achieve Method Overloading in Java by considering the “area of a rectangle” as an example.

矩形的面积

Area of a rectangle is defined region occupied by ait in a 2-d plane. We can find the area of rectangle by performing the product of length and breadth of rectangle.

Area of Rectangle = lb
where	 
   l: length of rectangle.
   b: breadth of rectangle
登录后复制

In the below example, we will achieve Method Overloading in Java using the area of a rectangle as an example by changing the data types of parameters.

立即学习Java免费学习笔记(深入)”;

Algorithm

STEP 1 − Write a custom class to find the area of the rectangle.

STEP 2 − Initialize a pair of two variables of different data types in the main method of the public class.

STEP 3 − Create an object of a custom class in the main method of the public class.

STEP 4 − Call the specific method to find the area of the rectangle using the custom object created.

Example

在这个例子中,我们使用一个基本公式计算矩形的面积,并在Java中实现了方法重载。

方法重载是通过改变“areaOfRectangle”方法中参数的类型来实现的。现在,当用户将整数类型的参数值作为输入传递给areaOfRectangle方法时,Area类的第一个areaOfRectangle方法被调用并输出结果。如果用户输入的是双精度类型的参数,则调用并执行第二个areaOfRectangle方法。

//Java Code to achieve Method Overloading in Java by Area of Rectangle.
import java.io.*;
class Area {
   // In this example area method is overloaded by changing the type of parameters.
   public void areaOfRectangle(int length, int breadth) {
      int area = 0;
      area = length *breadth;
      System.out.println("Area of the rectangle is :" + area);
   }
   public void areaOfRectangle(double  length, double breadth) {
      double area= 0;
      area = length *breadth;
      System.out.println("Area of the rectangle is:" + area);
   }
}
public class Main {
   public static void main(String args[]) {
      Area Object  = new Area();
      int length_1 = 3;
      int  breadth_1 = 4;
      Object.areaOfRectangle(length_1, breadth_1);
      double length_2 = 4.5;
      double  breadth_2 = 5.5;
      Object.areaOfRectangle(length_2, breadth_2);
   }
}
登录后复制

Output

Area of the rectangle is :12
Area of the rectangle is:24.75
登录后复制

Time Complexity: O(1) Auxiliary Space: O(1)

Thus, in this article, we have learned how to implement Method Overloading in Java by changing the datatype of parameters using the example of finding the area of a rectangle.

以上就是使用方法重载来查找矩形面积的Java程序的详细内容,更多请关注php中文网其它相关文章!

java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号