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;
	}
	
}
发布了98 篇原创文章 · 获赞 8 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43472877/article/details/104146002