- 前言
- 一、类型
前言
return有两个作用,①返回方法指定类型的值,②结束方法,终止“return;”后面代码的执行。
一、类型
- 基本类型:八大基本类型
- 数组:int[ ]等
- 字符串:String
- 自定义的类
//使用基本类型作为方法的返回值
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) {
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;
}
}



