2 Java方法

**

2.1方法定义和格式解释

**
方法简单来讲就是一段完成特定功能的代码片段,在其他语言里面方法被称为函数。

1)方法各个参数的定义和格式
修饰符:public static

返回值类型:用于限定返回值的数据类型,没有返回值则使用void

方法名:为了方便我们调用的方法的名称

参数类型:用于限定调用方法时传入的数据类型

参数名:用于接受调用传入数据的变量

方法体:完成功能的代码

return:结束方法,并且把返回值带给调用方法者

public static 返回值类型 方法名(参数类型 参数名1,参数类型 参数名2...{
	方法体;
	return 返回值;
}

2)有返回值的示例:调用自定义方法sum()并手动录入两个数据求和

package com.hay;

import java.util.Scanner;

public class Method {
	public static int sum(int a , int b){
		int c = a + b;
		return c ;
	}
	
	public static void main(String[] args){
		
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入第一个数据:");
		int one = sc.nextInt();
		System.out.println("请输入第二个数据:");
		int two = sc.nextInt();
		
		int c = sum(one  , two);
		
		System.out.println("两个数的和为:"+c);
	}		
}

3)无返回值的示例

public class Method {
	public static void HelloWorld(){
		for(int i=0;i<10;i++){
			System.out.print("第"+i+"次HelloWorld!!!");
		}
		
	}
	
	public static void main(String[] args){
		HelloWorld();
		System.out.println();//换行
	}		
}

**

2.2方法定义可变长度参数

**
写一个方法,给这个方法传递一些字符串,数量不定,在控制台输出传递过来字符串。

public class Method02 {
	public static void print(String...a){
		for(int i=0;i<a.length;i++){
			System.out.println(a[i]);
		}
	}
	
	public static void main(String[] args){
		print("asd","sds","sdfsd","2133");
	}
}

**

2.3方法重载概述

**
在同一个类中出现相同方法名的情况,其特征为:

A 参数列表个数不一样

B 参数的数据类型不一样

方法名相同时,java在调用方法的时候,java虚拟机会通过传入参数列表个数不同和数据类型不同来区分同名方法。

示例:

public class Method03 {
	public static int num(int a,int b){
		int c = a+b;
		return a+b;
	}
	
	public static int num(int a,int b,int c){
		return a+b+c;
	}
	
	public static void main(String[] args){
		int a = 10;
		int b = 20;
		int result = num(a,b);
		System.out.println("a+b的和为:"+result);
		
		int c =30;
		int result2 = num(a,b,c);
		System.out.println("a+b+c的和为:"+result);
	}
}

**

2.4方法的参数传递

**
方法的参数如果是基本数据类型,形式参数的改变不影响实际参数。此处有两个参数的概念。
1)实际参数:实际参与运算的变量。

2)形式参数:用于接受实际参数接收的变量

A方法形参是基本数据类型,并不会影响实际参数的值

public class Canshu {
	public static void change(int a,int b){
		a = b;
		b = a+b;
		System.out.println("a:"+a +",b:"+b);
	}
	
	public static void main(String[] args){
		int a = 10;
		int b = 20;
		System.out.println("a:"+a +",b:"+b);
		System.out.println("=========");//a:10,b:20
		change(a,b);//a:20,b:40
		System.out.println("=========");
		System.out.println("a:"+a +",b:"+b);//a:10,b:20		
	}
}

B方法形参是引用数据类型,形参的改变会直接影响实际参数;因为形式参数的变量和实际参数的变量共享的是一块堆内存。

public class Canshu84 {
	public static void change(int[]arr){
		for(int x = 0 ; x < arr.length ; x++){
			if(arr[x]%2==0){
				arr[x] *=2;
			}
		}
	}
	public static void main(String[] args){
		int []arr = {1,2,3,4,5};
		for(int x=0;x<arr.length;x++){
			System.out.println(arr[x]);
		}	
		change(arr);
		System.out.println("=============");
		for(int x=0;x<arr.length;x++){
			System.out.println(arr[x]);
		}
	}
}

**

2.5方法的应用

**
示例1:方法遍历数组

public class Work86 {
	public static void printArray(int []arry){
		System.out.print("[ ");
		for(int i=0;i<arry.length;i++){
			if(i==arry.length-1){
				System.out.print(arry[i]+"]");
			}else{
			System.out.print(arry[i]+",");
			}
		}
	}
	public static void main(String[] args){
		int []arry = {1,2,3,4,5,6,7,8,9,10};
		printArray(arry);
	}
}

示例2:数组求和

public class Work88 {
	public static int sum(int[]array){
		int sum = 0;
		for(int i=0;i<array.length;i++){
			sum += array[i];
		}
		return sum;
	}
	
	public static void main(String[] args){
		int []array = new int[5];
		array[0] = 10;
		array[1] = 20;
		array[2] = 30;
		array[3] = 40;
		array[4] = 50;
		int sum = sum(array);
		System.out.println("数组元素之和为:"+sum);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小鎮後生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值