Java方法

Java方法

一、方法概述

1.为什么要用方法?

​ 1.解决代码的重复性问题

​ 2.如果把代码都放在程序的入口main中,程序会非常臃肿,不便于维护

​ 3.提高代码的可重用性

2.什么是方法?

​ 定义在类中的具有特定功能的一段独立程序,称之为方法。

image-20210311145017848

二、使用步骤

1.定义

	/**
	 * 	自定义方法
	 */
	public static void fun1() {
		System.out.println("刘媛婷唱的xxx歌");
	}

2.在main方法中调用

	/**
	 * 	主方法,程序的入口
	 */
	public static void main(String[] args) {
		//自定义方法需要在主方法中调用
		fun1();
		fun1();
		fun1();
	}

三、方法的形参和实参

image-20220329112351388

四、数组类型的参数

	public static void main(String[] args) {
		int[] array = {3,5,1,7,6};
		//方法调用
		fun(array);
	}
	
	/**
	 * 	如何在自定义方法中遍历主方法中定义的数组
	 */
	public static void fun(int[] arr) {
		for (int i = 0; i < arr.length; i++) {
			System.out.println(arr[i]);
		}
	}

五、带返回值的方法

public class Lesson4 {

	public static void main(String[] args) {
		int n = fun();
		System.out.println(n);
	}
	
	/**
	 * 	如何在主方法中输出自定义方法中某个变量的值
	 * 	使用方法的返回值值实现
	 */
	public static int fun() {
		
		return 100;
	}

}

六、方法的分类

/**
 * 方法的分类
 * @author Administrator
 *
 */
public class Lesson5 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}
	
	/**
	 * 	无参无返回值
	 */
	public static void fun1() {
		
	}
	
	/**
	 * 	有参无返回值
	 */
	public static void fun2(int n) {
		
	}
	
	/**
	 * 	无参有返回值
	 */
	public static int fun3() {
		return 100;
	}
	
	/**
	 * 	有参有返回值
	 */
	public static int fun4(int m) {
		return 200;
	}

}

七、方法的重载

/**
 * 方法的重载
 * 	多个方法,方法名称一样,参数列表不一样(参数个数,或者数据类型不一样),就叫方法的重载
 * 	方法的重载跟参数的名称没有关系
 * 方法重载的调用,根据调用时传入的实参个数和类型,自动选择需要调用的方法
 */
public class Lesson6 {

	public static void main(String[] args) {
		sum("hello",2);

	}
	
	public static void sum(int a,int b) {
		System.out.println("有2个参数的方法");
		int result = a + b;
		System.out.println(result);
	}
	
	public static void sum(int a,int b,int c) {
		System.out.println("有3个参数的方法");
		int result = a + b + c;
		System.out.println(result);
	}
	
	public static  void sum(String a,int b) {
		System.out.println("有字符串参数的方法");
		String result = a + b;
		System.out.println(result);
	}
}

八、任意个数参数的方法

实现方法在调用的时候,可以传递任意的参数个数

public class Lesson7 {

	public static void main(String[] args) {
		fun(1,2,3,4,5,6,7,8);
	}
	
	/**
	 * 	任意个数参数的方法
	 * 	方法以伪数组的形式接收实参
	 */
	public static void fun(int ...a) {
		System.out.println(a.length);
		for (int i = 0; i < a.length; i++) {
			System.out.println(a[i]);
		}
	}
}

注:

我们在使用的时候,优先考虑固定参数个数的方法

九、static

//a.被static修饰的方法,只能调用被static修饰的语法
//b.非static方法,可以调用static语法,也可以调用非static语法
//c.static方法,也称为全局方法;可以在整个项目的任何类中,通过 类名.方法名 调用
//d.被public static修饰的变量叫全局变量,可以在整个项目的任何类中,通过类名.变量名 访问
public class Lesson9 {
	public static int n = 100;
	
	
	public static void main(String[] args) {
		//fun1();
//		System.out.println(n);
	}
	
	public static void fun2() {
		System.out.println("我是lesson9中的方法");
	}
	
	public void fun1() {
		fun2();
		System.out.println(n);
	}
}

十、变量的作用域

1.局部变量

把在方法里面定义的变量称为局部变量,只能在方法内部使用

2.成员变量

在方法外面类里面定义的变量,在类中的所有方法里面都可以使用

成员变量有初始值

3.全局变量

在成员变量的基础上加public static修饰,全局变量在整个项目里面都可以直接使用

在其它类中使用类名.全局变量名访问

注:

如果局部变量跟成员变量或者全局变量的名称一致,在使用的时候遵循就近原则 
在方法里面要使用某一个变量时,先在方法里面找这个变量,找到就使用,找不到再去方法外面找
/**
 * 	变量的作用域
 *		1.局部变量--定义在方法中--只能在该方法中使用
 *		2.成员变量--定义在方法外面,类里面--可以在该类中所有的非静态方法中使用
 *		3.全局变量--在成员变量的前面加上public static--可以在项目的所有类中使用
 */
public class Exam4 {
	//成员变量
	String name = "赵日天";
	//全局变量
	public static int age = 100;
	public static void main(String[] args) {
		//局部变量
		int n = 100;
		if(true) {
			System.out.println(n);
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值