第三课:方法
1、定义:
方法就是一段可重复调用的代码段。但是,现在的方法是使用主方法直接调用的,所以定义格式为:
public static 返回值类型 | void 方法名称([参数列表]){
[return 返回内容;]
}
void 表示此方法没有任何的返回值类型,不需要使用 return 语句。如果要定义有返回值的方法,直接在返回值类型上写上数据类型即可。
2、方法的重载
在方法的使用中也存在一种概念,叫做重载。
重载:指的是方法的名称相同,但是参数的类型或个数不同。
public class SimpleDemo{
public static void main(String args[]){
System.out.println(add(10,20)) ;
System.out.println(add(30,30,30)) ;
System.out.println(add(30.01f,30.15f)) ;
}
public static int add(int x ,int y){
int temp = x + y ;
return temp ;
}
public static int add(int x ,int y ,int z){
int temp = x + y + z ;
return temp ;
}
public static float add(float x ,float y){
float temp = x + y ;
return temp ;
}
};
实际上重载的概念一直在接触,System.out.println()可以接受任意类型的参数,所以此方法也属于重载的方法,但是在使用重载的时候有以下一种情况,一定要注意。
public class SimpleDemo{
public static void main(String args[]){
}
public static int add(int x ,int y){
int temp = x + y ;
return temp ;
}
public static float add(int x ,int y){
float temp = x + y ;
return temp ;
}
};
会出现已在SimpleDemo中定义add(int ,int)方法的错误。以上两个方法的返回值根本不一样,此种操作不是重载,因为重载的时候看的不是方法发返回值类型,而是参数的类型或个数。
3、return的作用
在方法的使用中还可以使用return来结束一个方法的操作,当执行到return的时候会直接返回到方法调用处继续执行。
public class SimpleDemo{
public static void main(String args[]){
fun(10) ;
fun(3) ;
}
public static void fun(int x){
System.out.println("进入方法。") ;
if(x == 3){
return ; //返回方法的调用处
}
System.out.println("结束方法。") ;
}
};
这也是结束方法的一个手段。
4、递归调用
在方法的操作中存在一种成为递归调用的形式,所谓的递归调用就是指,自己调用自己。
例如:一个累加的操作
public class SimpleDemo{
public static void main(String args[]){
int sum = 0 ;
sum = fun(100) ;
System.out.println("计算结果:" + sum) ;
}
public static int fun(int temp){
if(temp == 1){
return 1 ;
}else{
return temp + fun(temp - 1) ;
}
}
};
注意:在使用递归操作的时候一定要有明确的截止条件,否则会出现异常。
5、方法与数组
一个方法可以接受一个数组或者返回一个数组,但是在接收数组的时候,一定要注意,数组是引用数据类型,所以方法中对数组所做的一切修改,最终都会被保留下来。
public class SimpleDemo{
public static void main(String args[]){
int temp[] = {1,3,5,7,9} ; // 声明数组
fun(temp) ;
print(temp) ;
}
public static void fun(int [] x){
x[0] = 6 ; //修改第一个元素
}
public static void print(int [] x){
for(int i = 0; i<x.length; i++){
System.out.println(x[i]) ;
}
}
};
实际上数组的引用传递,引用传递就是堆和栈内存的地址空间。
使用方法还可以返回一个数组,只要在返回值类型上加入数组类型即可。
public class SimpleDemo{
public static void main(String args[]){
int temp[] = fun() ; // 声明数组
print(temp) ;
}
public static int [] fun(){
int x[] = {1,3,5,7,9} ;
return x ; //返回一个数组
}
public static void print(int [] x){
for(int i = 0; i<x.length; i++){
System.out.println(x[i]) ;
}
}
};
不管是接收数组还是返回数组,实际上最终都是要以数组的形式接收和返回:int[] x
在Java中提供了很多与数组操作相关的方法
例如:排序操作 java.util.Arrays.sort(temp) ;
6、foreach输出
语法格式:
for(数据类型 变量 : 数组){
//操作
}
例:用foreach输出数组
public class SimpleDemo{
public static void main(String args[]){
int t1[] = {1,2,3,4,5,6,7,8,9} ;
for(int x:t1){
System.out.println(x) ;
}
}
};
7、可变参数
正常情况下,一个方法在调用时,必须明确的指定传入的参数,而且参数的个数必须统一,但是在JDK1.5之后,在声明方法参数的时候加入了可变参数:
public static 返回值类型 数组名称(数据类型 ... 参数名称)
这样一来,在传入参数的时候就可以传递任意多个了,而且全部的参数是以数组的形式接收的。
public class SimpleDemo{
public static void main(String args[]){
int temp[] = {1,2,3,4,5,6,7,8,9} ;
fun() ; //没有参数
fun(1) ; //一个参数
fun(1,3,5,7,9) ; //多个参数
fun(temp) ; //一个数组
}
public static void fun(int ... arg){
for(int x:arg){
System.out.print(x + "、") ;
}
System.out.println() ;
}
};
【2011-12-22】
浙公网安备 33010602011771号