JAVA 方法的参数类型解析

方法的参数类型解析:

1.基本类型
2.数组
3.字符串
4.自定义的类

结论:所有数据类型都可以作为参数类型

public class MethodParam {
	public static void main(String[] args) {
		method1(100);
		
		int[] array = {10,20,30};
		method2(array);
		
		String str = "aaa";
		method3(str);
		
		Student stu1 = new Student("姚明",12);
		method4(stu1);
	}

	//使用基本类型作为方法的参数
	public static void method1(int num) {
		num += 20;
		System.out.println(num); //120
	}
	
	//使用数组作为方法的参数
	public static void method2(int[] array) {
		System.out.println(array[0]);  //10
		System.out.println(array[1]);  //20
		System.out.println(array[2]);  //30
	}
	
	//使用字符串作为方法的参数
	public static void method3(String str) {
		String s = str.replace("a", "b");
		System.out.println(s);  //bbb
	}
	
	//使用自定义的类作为方法的参数
	public static void method4(Student stu) {
		System.out.println("姓名"+stu.getName()+"年龄"+stu.getAge());
	}
}
public class Student {
	
	private String name;
	private int age;
	
	public Student() {}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Student(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值