信息

【Java系列教程二十】方法part2

 

2. 方法调用

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

通过值传递参数是使用参数调用方法。 通过这样将参数值将传递给参数。

示例

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

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. 方法重载

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

在前面讨论的用于查找最小整数类型数的示例中,假设想要查找两个double类型的最小数值。 可引入重载的概念以创建具有相同名称但不同参数的两个或更多方法。

参考以下示例代码 -

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类型和double类型的最小数。

6. 使用命令行参数

有时希望在运行程序时将一些信息传递给程序。它是通过将命令行参数传递给main()来实现的。

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

示例

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

public class CommandLine {

public static void main(String args[]) {

for(int i = 0; i<args.length; i ) {

System.out.println("args[" i "]: " args[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

3. void关键字

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

注 - this关键字仅在实例方法或构造函数中使用。

通常,this关键字用于 -

如果实例变量在构造函数或方法中具有相同的名称,则将它们与局部变量区分开来。

class Student {

private int age;

Student(int age) {

this.age = age;

从类中的其他方法调用一种类型的构造函数(参数化构造函数或默认值),称为显式构造函数调用。

class Student {

int age

Student() {

this(20);

Student(int age) {

this.age = age;

以下是使用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

阅读剩余
文章版权声明 1、本网站名称:安鹿轩
2、本站永久网址:https://www.anlu1314.com
3、本网站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系在本站私信站长进行删除处理。
4、本站一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
5、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
6、本站资源大多存储在云盘,如发现链接失效,请联系我们我们会第一时间更新。

THE END
    协助本站(安鹿轩)seo优化一下,谢谢!
    关键词不能为空