Java中的方法

A. Java中的方法 或者Java方法是执行特定任务并将结果返回给调用方的语句集合。Java方法可以执行某些特定任务而不返回任何内容。Java中的方法允许我们 重新使用 无需重新键入代码即可删除代码。在java中,每种方法都必须是与C++、C++和Python等语言不同的类的一部分。

null

注: 方法可以节省时间,并帮助我们在不重新键入代码的情况下重用代码。

方法声明

通常,方法声明有六个组成部分:

1.修饰语: 它定义了 访问类型 方法的定义,即从应用程序中可以访问的位置。在Java中,有4种类型的访问说明符。

  • 公众: 它是 可在应用程序中的所有类中访问。
  • 受保护的: 它可以在定义它的类及其子类中访问
  • 私人: 它是 只能在定义它的类中访问。
  • 违约: 它是在不使用任何修饰符的情况下声明/定义的。它可以在定义其类的同一类和包中访问。

2.退货类型: 方法返回的值的数据类型,如果不返回值,则为void。

3.方法名称: 字段名的规则也适用于方法名,但约定有点不同。

4.参数列表: 以逗号分隔的输入参数列表在括号内定义,前面是它们的数据类型。如果没有参数,则必须使用空括号()。

5.例外情况清单: 方法所期望的异常可以抛出,您可以指定这些异常。

6.方法主体: 它被封闭在支架之间。执行预期操作所需执行的代码。

methods in java

Java中的方法类型

Java中有两种方法:

1.预定义方法: 在Java中,预定义方法是已经在Java类库中定义的方法,称为预定义方法。它也被称为 标准库方法 内置方法 .我们可以直接使用这些方法,只需在程序中随时调用它们。

2.用户定义方法: 由用户或程序员编写的方法称为 用户定义的 方法这些方法根据要求进行了修改。

方法签名

它由方法名和参数列表(参数数量、参数类型和参数顺序)组成。返回类型和异常不被视为它的一部分。

上述函数的方法签名:

 max(int x, int y) Number of parameters is 2, Type of parameter is int.

如何命名方法?

方法名通常是一个单词,应该是 动词 在小写或多个单词中,以 动词 小写字母,后跟 形容词,名词…。。 在第一个单词之后,每个单词的第一个字母都应该大写。

命名方法的规则

  • 定义方法时,请记住方法名称必须是 动词 从一个 小写字母
  • 如果方法名称包含两个以上的单词,则第一个名称必须是动词,后跟形容词或名词。
  • 在多单词方法名称中,每个单词的第一个字母必须在 大写字母 除了第一个词。例如,findSum、computeMax、setX和getX。

通常,一个方法在其定义的类中具有唯一的名称,但有时一个方法可能与同一类中的其他方法名称具有相同的名称 Java中允许方法重载 .

方法调用

需要调用该方法才能使用其功能。调用方法时可能有三种情况: 在以下情况下,方法返回调用它的代码:

  • 它完成了方法中的所有语句
  • 它到达一个返回语句
  • 抛出异常

例子:

JAVA

// Java Program to Illustrate Methods
// Importing required classes
import java.io.*;
// Class 1
// Helper class
class Addition {
// Initially taking sum as 0
// as we have not started computation
int sum = 0 ;
// Method
// To add two numbers
public int addTwoInt( int a, int b)
{
// Adding two integer value
sum = a + b;
// Returning summation of two values
return sum;
}
}
// Class 2
// Helper class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating object of class 1 inside main() method
Addition add = new Addition();
// Calling method of above class
// to add two integer
// using instance created
int s = add.addTwoInt( 1 , 2 );
// Printing the sum of two numbers
System.out.println( "Sum of two integer values :"
+ s);
}
}


输出

Sum of two integer values :3

例2:

JAVA

// Java Program to Illustrate Method Calling
// Via Different Ways of Calling a Method
// Importing required classes
import java.io.*;
// Class 1
// Helper class
class Test {
public static int i = 0 ;
// Constructor of class
Test()
{
// Counts the number of the objects of the class
i++;
}
// Method 1
// To access static members of the class and
// and for getting total no of objects
// of the same class created so far
public static int get()
{
// statements to be executed....
return i;
}
// Method 2
// Instance method calling object directly
// that is created inside another class 'GFG'.
// Can also be called by object directly created in the
// same class and from another method defined in the
// same class and return integer value as return type is
// int.
public int m1()
{
// Display message only
System.out.println(
"Inside the method m1 by object of GFG class" );
// Calling m2() method within the same class.
this .m2();
// Statements to be executed if any
return 1 ;
}
// Method 3
// Returns nothing
public void m2()
{
// Print statement
System.out.println(
"In method m2 came from method m1" );
}
}
// Class 2
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating object of above class inside thi class
Test obj = new Test();
// Calling method 2 inside main() method
int i = obj.m1();
// Display message only
System.out.println(
"Control returned after method m1 :" + i);
// Call m2() method
// obj.m2();
int no_of_objects = Test.get();
// Print statement
System.out.print(
"No of instances created till now : " );
System.out.println(no_of_objects);
}
}


输出

Inside the method m1 by object of GFG class
In method m2 came from method m1
Control returned after method m1 :1
No of instances created till now : 1

上述程序的控制流程如下:

methods in java

方法调用的内存分配

方法调用通过堆栈实现。无论何时调用一个方法,都会在堆栈区域内创建一个堆栈帧,之后,传递给该方法的参数、该被调用方法要返回的局部变量和值都会存储在该堆栈帧中,当被调用方法的执行完成时,分配的堆栈帧将被删除。有一个堆栈指针寄存器,它跟踪堆栈的顶部,并相应地进行调整。

相关文章:

本文由 尼茨海伦德拉 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享