Java引用类型作为形参和返回值
一、什么是引用类型
在Java中引用类型包括三种:类、抽象类、接口。
二、引用类型作为形参使用
1、类作为形参
/** * 类作为形参,实际传递的是该类的对象 */ class Student { public void study() { System.out.println("Good Good Study, Day Day Up"); } } class StudentDemo { public void show(Student s) { s.study(); } } public class StudentTest { public static void main(String[] args) { StudentDemo sd = new StudentDemo(); Student s = new Student(); sd.show(s); } }
2、抽象类作为形参
/** * 抽象类作为形参,传递的是实现该抽象类的子类对象 */ abstract class Person { public abstract void eat(); } class XiaoMing extends Person { @Override public void eat() { // TODO Auto-generated method stub System.out.println("小明爱吃米饭"); } } class PersonDemo { public void show(Person p) { p.eat(); } } public class PersonTest { public static void main(String[] args) { PersonDemo pd = new PersonDemo(); // 方式一、传递的是实现该抽象类的子类对象 Person p = new XiaoMing(); // 多态 pd.show(p); System.out.println("------------------"); // 方式二、直接实现抽象类,传递匿名子类对象 pd.show(new Person() { @Override public void eat() { // TODO Auto-generated method stub System.out.println("小明爱上了吃面条"); } }); } }
3、接口作为形参
/** * 接口作为形参,传递的是实现该接口的子类对象 */ interface Teacher { // 接口 like a 的关系,并不是所有 Teacher 都抽烟 public abstract void smoke(); } class TeacherZhang implements Teacher { @Override public void smoke() { // TODO Auto-generated method stub System.out.println("老师爱抽中华"); } } class TeacherDemo { public void show(Teacher t) { t.smoke(); } } public class TeacherTest { public static void main(String[] args) { TeacherDemo td = new TeacherDemo(); // 方式一、传递的是实现该接口的子类对象 Teacher t = new TeacherZhang(); // 多态 td.show(t); System.out.println("------------------"); // 方式二、直接实现接口,传递匿名子类对象 td.show(new Teacher() { @Override public void smoke() { // TODO Auto-generated method stub System.out.println("老师不抽中华了,爱上了雪茄"); } }); } }
三、返回引用类型值
1、返回类类型值,实际返回的是该类的对象
/** * 类作为返回值,实际返回的是该类的对象 */ class Student { public void study() { System.out.println("Good Good Study, Day Day Up"); } } class StudentDemo { public Student getInstance() { return new Student(); } } public class StudentTest2 { public static void main(String[] args) { StudentDemo sd = new StudentDemo(); Student s = sd.getInstance(); s.study(); } }
2、返回抽象类类型值,实际返回的是实现该抽象类子类的对象
/** * 抽象类作为返回值,实际返回的是实现该抽象类的子类对象 */ abstract class Person { public abstract void eat(); } class XiaoMing extends Person { @Override public void eat() { // TODO Auto-generated method stub System.out.println("小明爱吃米饭"); } } class PersonDemo { public Person getIntance() { return new XiaoMing(); } } public class PersonTest2 { public static void main(String[] args) { PersonDemo pd = new PersonDemo(); Person p = pd.getIntance(); p.eat(); } }
3、返回接口类型值,实际返回的是实现该接口子类的对象
/** * 接口作为返回值,实际返回的是实现该接口的子类对象 */ interface Teacher { // 接口 like a 的关系,并不是所有 Teacher 都抽烟 public abstract void smoke(); } class TeacherZhang implements Teacher { @Override public void smoke() { // TODO Auto-generated method stub System.out.println("老师爱抽中华"); } } class TeacherDemo { public Teacher getIntance() { return new TeacherZhang(); } } public class TeacherTest2 { public static void main(String[] args) { TeacherDemo td = new TeacherDemo(); Teacher t = td.getIntance(); t.smoke(); } }