目录
一、形参和返回值都是基本类型
这里比较简单,放一个不确定参数数量的例子玩玩:
package Demo;
public class Sum {
public static void main(String[] args) {
int s =sum(1,2,3,4);
System.out.println(s);
}
public static int sum(int...nums){
int ans=0; //局部变量必须手动初始化
for(int i=0;i<nums.length;i++){
ans+=nums[i];
}
return ans;
}
}
对于可变参数的传递,其实传递的是一个数组。
二、类名作为形参和返回值
形参是类名时,参数要传递类的对象,返回值是类名时,返回的是该类的对象。
这里直接把类当做一个数据类型就可以,其实没什么区别。看一个小栗子:
-
创建一个学生类
package Demo; /* 成员变量 使用private修饰 构造方法 提供一个无参构造方法 提供一个带多个参数的构造方法 成员方法 提供每一个成员变量对应的set方法和get方法 提供一个显示对象信息的show方法 */ public class Student { //成员变量 private String name; private int age; //构造方法 public Student() { } //无参数构造方法 public Student(String name, int age) { this.age = age; this.name = name; } //提供age的get和set方法 public int getAge() { return age; } public void setAge(int age) { if (age < 0) { System.out.println("你输入的年龄有误"); } else { this.age = age; } } //提供name的get和set方法 public String getName() { return name; } public void setName(String name) { this.name = name; } //提供一个显示成员信息的show方法 public void show() { System.out.println("name:" + name + ", age:" + age); } }
-
创建一个测试类,该测试类中包含一个学生初始化方法,它的形参是学生类,再定义一个创建学生对象的方法,它的返回值是学生类。
package Demo; public class StudentDemo { public static void main(String[] args) { //创建一个学生对象 Student s1 = new Student(); //调用学生初始化方法 studentInitialize(s1); //调用学生创建方法 Student s2 = studentNew(); //调用Student对象的show()方法 s1.show(); //name:张楚岚, age:20 s2.show(); //name:宝儿姐, age:18 } public static void studentInitialize(Student s){ s.setAge(20); s.setName("张楚岚"); } public static Student studentNew(){ Student s = new Student(); s.setAge(18); s.setName("宝儿姐"); return s; } }
三、抽象类作为形参和返回值
形参是抽象类时,参数要传递该类的子类(具体类)的对象,返回值是抽象类时,返回的是该类的子类(具体类)的对象。
这里其实就是因为抽象类不能直接实例化,只能参照抽象类多态的形式实例化其子类(具体类)。再来一个小栗子:
- 定义一个抽象人类:
package Demo; public abstract class People { private int age; private String name; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public People() { } public People(int age, String name) { this.age = age; this.name = name; } }
- 定义一个学生类,继承自人类,包含一个show()方法:
package Demo; public class Student extends People{ public Student() { } public Student(int age, String name) { super(age, name); } //提供一个显示成员信息的show方法 public void show() { System.out.println("name:" + super.getName() + ", age:" + super.getAge()); } }
- 创建一个测试类,该测试类中包含一个人类初始化方法,它的形参是人类,再定义一个创建人类对象的方法,它的返回值是人类:
package Demo; public class StudentDemo { public static void main(String[] args) { //创建一个学生对象 People p1 = new Student(); //调用学生初始化方法 peopleInitialize(p1); //调用学生创建方法 People p2 = peopleNew(); //向下转型,然后调用Student对象的show()方法 Student s1 =(Student)p1; s1.show(); //name:张楚岚, age:20 Student s2 = (Student)p2; s2.show(); //name:宝儿姐, age:18 } public static void peopleInitialize(People p){ p.setAge(20); p.setName("张楚岚"); } public static People peopleNew(){ People P = new Student(); P.setAge(18); P.setName("宝儿姐"); return P; } }
四、接口作为形参和返回值
形参是接口时,参数要传递该接口的实现类(具体类)的对象,返回值是接口时,返回的是该接口的实现类(具体类)的对象。
这里其实就是因为接口不能直接实例化,只能参照接口多态的形式实例化其实现类(具体类)。欢迎大家在来一个小栗子。
五、不确定数量的类名作为形参
这里直接改写了上面的例子中peopleInitialize()方法,并且一次初始化了三个对象。
package Demo;
public class StudentDemo {
public static void main(String[] args) {
//创建一个学生对象
People p1 = new Student();
People p3 = new Student();
People p4 = new Student();
//调用学生初始化方法
peopleInitialize(p1,p3,p4);
//调用学生创建方法
People p2 = peopleNew();
//向下转型,然后调用Student对象的show()方法
Student s1 =(Student)p1;
s1.show(); //name:张楚岚, age:20
Student s2 = (Student)p2;
s2.show(); //name:宝儿姐, age:18
Student s3 = (Student)p3;
s2.show(); //name:宝儿姐, age:18
Student s4 = (Student)p4;
s2.show(); //name:宝儿姐, age:18
}
public static void peopleInitialize(People...p){
for(int i = 0;i<p.length;i++){
People pp=p[i];
pp.setAge(20);
pp.setName("张楚岚");
}
}
public static People peopleNew(){
People P = new Student();
P.setAge(18);
P.setName("宝儿姐");
return P;
}
}