首页 >> 方法大全

Java方法

2022-05-14 方法大全 218 作者:考证青年

Java 中的方法是一组语句,它们组合在一起以执行各种操作。例如,当调用 .out.() 方法时,系统实际上会执行多个语句,以便在控制台上显示消息。

了解如何创建自己的带或不带返回值的方法,调用带或不带参数的方法,以及在编程中应用方法抽象。

1. 创建方法

让我们看一下该方法的语法 -

public static int methodName(int a, int b) {
   // body
}

在上面的语法中,

方法定义由方法头和方法体组成。以下语法中显示了相同的内容 -

modifier returnType nameOfMethod (Parameter List) {
   // method body
}

上面显示的语法包括 -

例子

min() 方法在以下代码中定义。此方法接受两个 int 类型的参数:num1 和 num2,并返回两者之间的最大值 -

/** 返回两个数字之间的最小值 */
public static int minFunction(int n1, int n2) {
   int min;
   if (n1 > n2)
      min = n2;
   else
      min = n1;
   return min; 
}

2. 方法调用

方法可以通过调用方法来使用,可以通过两种方式调用,即方法有返回值或不返回任何值。

方法调用的过程很简单。当程序调用一个方法时,程序控制转移到被调用的方法。这个被调用的方法然后在两种情况下将控制权返回给调用者,即 -

调用返回 void 的方法 -

System.out.println("This is Yiibai.com!");

调用具有返回值的方法 -

int result = sum(6, 9);

以下是显示如何定义方法以及如何调用它的示例 -

public class ExampleMinNumber {
   public static void main(String[] args) {
      int a = 111;
      int b = 125;
      int c = getMin(a, b);
      System.out.println("最小值 = " + c);
   }
   /** 返回两个 int 数值的最小值 */
   public static int getMin(int n1, int n2) {
      int min;
      if (n1 > n2)
         min = n2;
      else
         min = n1;
      return min; 
   }
}

执行上面的示例代码,得到如下结果:

最小值 = 111

3. 无效关键字

void 关键字允许创建不返回值的方法。在下面的示例中,有一个返回 void 的方法,它不返回任何值。调用 void 方法必须是一个语句方法,即 (245.67);。它是一个以分号结尾的 Java 语句,如下例所示 -

public class ExampleVoid {
   public static void main(String[] args) {
      methodRankPoints(245.67);
   }
   public static void methodRankPoints(double points) {
      if (points >= 202.5) {
         System.out.println("Rank:A1");
      }else if (points >= 122.4) {
         System.out.println("Rank:A2");
      }else {
         System.out.println("Rank:A3");
      }
   }
}

方法

执行上面的示例代码,得到如下结果:

Rank:A1

4. 按值传递参数

按值传递参数时需要传递参数。它们应该与方法规范中的参数顺序相同。参数可以通过值或引用传递。

传值传参就是调用带参数的方法。通过这样做,参数值将传递给参数。

例子

以下程序显示了按值传递参数的示例。即使在方法调用之后,参数的值也保持不变。

public class swappingExample {
   public static void main(String[] args) {
      int a = 30;
      int b = 45;
      System.out.println("Before swapping, a = " + a + " and b = " + b);
      // 调用交换方法
      swapFunction(a, b);
      System.out.println("Now, Before and After swapping values will be same here:");
      System.out.println("After swapping, a = " + a + " and b is " + b);
   }
   public static void swapFunction(int a, int b) {
      System.out.println("Before swapping(Inside), a = " + a + " b = " + b);
      // 交换 n1 和 n2
      int c = a;
      a = b;
      b = c;
      System.out.println("After swapping(Inside), a = " + a + " b = " + b);
   }
}

执行上面的示例代码,得到如下结果:

Before swapping, a = 30 and b = 45
Before swapping(Inside), a = 30 b = 45
After swapping(Inside), a = 45 b = 30
Now, Before and After swapping values will be same here:
After swapping, a = 30 and b is 45

5. 方法重载

当一个类有两个或多个同名但参数不同的方法时,称为方法重载。这和重写不一样。在覆盖中,方法具有相同的方法名称、类型、参数数量等。

在前面讨论的查找最小整数类型的示例中,假设您要查找两种类型中的最小数值。可以引入重载的概念来创建两个或多个同名但参数不同的方法。

请参阅以下示例代码 -

public class ExampleOverloading {
   public static void main(String[] args) {
      int a = 11;
      int b = 6;
      double c = 7.3;
      double d = 9.4;
      int result1 = getMin(a, b);
      // 具有相同函数名称,但数字不同参数
      double result2 = getMin(c, d);
      System.out.println("Minimum Value = " + result1);
      System.out.println("Minimum Value = " + result2);
   }
   // 处理 int 类型的数值(方法重载)
   public static int getMin(int n1, int n2) {
      int min;
      if (n1 > n2)
         min = n2;
      else
         min = n1;
      return min; 
   }
   //  处理 double 类型的数值(方法重载)
   public static double getMin(double n1, double n2) {
     double min;
      if (n1 > n2)
         min = n2;
      else
         min = n1;
      return min; 
   }
}

执行上面的示例代码,得到如下结果:

Minimum Value = 6
Minimum Value = 7.3

重载的方法使程序可读。在这里,这两个方法以相同的名称给出,但具有不同的参数类型。结果是找到最少数量的类型 int 和 type。

6. 使用命令行参数

有时希望在程序运行时将一些信息传递给程序。它通过将命令行参数传递给 main() 来做到这一点。

命令行参数是执行时直接在命令行上跟随程序名称的信息。在 Java 程序中访问命令行参数非常简单。它们作为字符串存储在传递给 main() 的类型化数组中。

例子

以下程序显示传递给程序的所有命令行参数 -

public class CommandLine {
   public static void main(String args[]) { 
      for(int i = 0; i

使用以下方法执行此过程 -

方法

C:/> java CommandLine this is a command line 200 -100

然后你会得到以下结果:

args[0]: this
args[1]: is
args[2]: a
args[3]: command
args[4]: line
args[5]: 200
args[6]: -100

7. 这个关键字

this 是 Java 中的一个关键字,在实例方法或构造函数中用作对当前类对象的引用。用它来引用类的成员,例如:构造函数、变量和方法。

注意 - 此关键字仅用于实例方法或构造函数。

通常方法, this 关键字用于 -

以下是使用此关键字访问类成员的示例 -

public class ThisExample {
   // 实例变量:num
   int num = 10;
   ThisExample() {
      System.out.println("This is an example program on keyword this");    
   }
   ThisExample(int num) {
      // 调用默认构造方法
      this();
      // 将局部变量 num 分配给实例变量 num 
      this.num = num;
   }
   public void greet() {
      System.out.println("Hi Welcome to Yiibai");
   }
   public void print() {
      // 局部变量:num
      int num = 20;
      // 打印局部变量
      System.out.println("value of local variable num is : "+num);
      // 打印实例变量
      System.out.println("value of instance variable num is : "+this.num);
      // 调用类方法 
      this.greet();     
   }
   public static void main(String[] args) {
      // 实例化该类
      ThisExample obj1 = new ThisExample();
      // 调用 print 方法
      obj1.print();
      //通过参数化构造函数将新值传递给 num 变量
      ThisExample obj2 = new ThisExample(30);
      // 再次调用 print 方法
      obj2.print(); 
   }
}

执行上面的示例代码,我们得到以下结果 -

This is an example program on keyword this 
value of local variable num is : 20
value of instance variable num is : 10
Hi Welcome to Yiibai
This is an example program on keyword this 
value of local variable num is : 20
value of instance variable num is : 30
Hi Welcome to Yiibai

8. 变量参数 (var-args)

JDK 1.5 允许将可变数量的相同类型的参数传递给方法。该方法中的参数声明如下 -

typeName... parameterName

在方法声明中,指定类型,后跟省略号 (...)。一个方法中只能指定一个变长参数,并且该参数必须是最后一个参数。

public class VarargsDemo {
   public static void main(String args[]) {
       // 使用变量参数调用方法
       printMax(314, 321, 213, 212, 356.5);
       printMax(new double[]{1, 2, 3});
   }
   public static void printMax( double... numbers) {
      if (numbers.length == 0) {
         System.out.println("No argument passed");
         return;
      }
      double result = numbers[0];
      for (int i = 1; i <  numbers.length; i++)
      if (numbers[i] >  result)
      result = numbers[i];
      System.out.println("参数列表中的最大值是:" + result);
   }
}

执行上面的示例代码,我们得到以下结果 -

参数列表中的最大值是:356.5
参数列表中的最大值是:3.0

9. () 方法

() 方法在垃圾收集器对象最终被销毁之前调用,它可以用来确保对象被完全终止。例如,() 可用于确保关闭此对象拥有的打开文件。

要将终结器添加到类中,只需定义 () 方法。每当 Java 方法想要回收该类的对象时,都会调用此方法。

在 () 方法中,您将指定在销毁对象之前必须执行的操作。() 方法具有这种一般形式 -

protected void finalize( ) {
   // finalization code here
}

在这里,关键字是一个修辞字符,它阻止通过类外部定义的代码访问 ()。

我们无法知道 Java 何时甚至是否会执行 the() 方法。如果程序在垃圾回收发生之前结束,() 将不会执行。

关于我们

最火推荐

小编推荐

联系我们


版权声明:本站内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 88@qq.com 举报,一经查实,本站将立刻删除。备案号:桂ICP备2021009421号
Powered By Z-BlogPHP.
复制成功
微信号:
我知道了