方法的返回值类型解析
1.基本类型
2.数组
3.字符串
4.自定义的类
public class MethodReturn {
public static void main(String[] args) {
int result1= method1();
System.out.println(result1);//100
int[] result2=method2();
System.out.println(result2[0]);//10
System.out.println(result2[1]);//20
String result3 = method3();
System.out.println(result3);//Hello
Student result4 = method4();
System.out.println(result4.getAge()+result4.getName());//26姚明
}
//使用基本类型作为方法的返回值
public static int method1() {
int num = 100;
return num;
}
//使用数组类型作为方法的返回值
public static int[] method2() {
int[] array = {10,20};
return array;
}
//使用字符串作为方法的返回值
public static String method3() {
String str = "Hello";
return str;
}
//使用自定义的类作为方法的返回值
public static Student method4() {
Student stu = new Student("姚明",26);
return stu;
}
}
public class Student {
private String name;
private int age;
public Student() {}
public String getName() {
return name;
}
public void setName(String name) {
= name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Student(String name, int age) {
super();
= name;
this.age = age;
}
}