在Java中,我们简单学习过方法中带返回值类型,我们必须用return给出返回值,有形式参数,我们学习一下在内部类中以类和接口形式参数与返回值问题;
一.形式参数
- 形式参数为具体类时,调用该方法,实际参数要传递该具体类的对象;
- 形式参数为抽象类时,调用该方法,实际参数传递该抽象类的子类对象;
- 形式参数为接口时,调用该方法,实际参数需要传递该接口的子实现类的对象;
//形式参数位具体类
public class Teacher {
public void method() {
System.out.println("生活要潇洒!");
}
}
public class Student {
public void learn(Teacher s) {
s.method();
}
}
public class ConcreteTest {
public static void main(String[] args) {
//调用Student的learn()方法
Student t=new Student();
//创建老师对象
Teacher tc=new Teacher();
t.learn(tc);
}
}
形参为具体类的运行结果
//形式参数为抽象类
public abstract class Boy {
public void say() {
System.out.println("I like you only!");
}
}
public class Girl {
public void show(Boy b) { //形参为抽象类Boy
b.say();
}
}
public class Aboy extends Boy {
public void say() { //方法重写(会覆盖父类的功能)
System.out.println("No,that`is love!");
}
}
//测试类
public class LoverTest {
public static void main(String[] args) {
//需求是调用Girl类中show方法
Girl g=new Girl();
//抽象类多态:抽象类父类指向子类对象
//创建抽象类Boy的子类对象
Boy b=new Aboy();
g.show(b);
}
}
在抽象类中子类继承父类,子类方法重写,子类会覆盖父类方法功能
//形参为接口的调用
public interface Father {
public abstract void way();
}
public class Son implements Father {
public void way() {
System.out.println("老子英雄儿好汉");
}
}
public class Other {
public void show(Father f) {
f.way();
}
}
public class Test {
public static void main(String[] args) {
//需求是调用Other类中的show()
Other o=new Other();
//创建接口Father的子实现类对象
Son s=new Son();
Father f=(Father)s;
o.show(f);
}
}
二.返回值问题
-
返回值为具体类时,需要返回该具体类的对象
-
返回值为抽象类,需要返回该抽象类的子类
-
返回值为接口时,需要返回该接口的子实现类对象
-
如果利用匿名对象的话,如果是具体类,创建具体类对象,如果是抽象类,创建抽象类子类的对象,如果是接口,创建接口的子实现类对象
//返回类型为具体类
public class Student {
public void learn() {
System.out.println("为自己而学习");
}
}
public class StudentDemo {
//当前StudentDemo类调用该方法
//方法一:分步走
public Student study() {
//该方法的返回值类型为具体类Student
//return需要返回该具体类的对象
Student s=new Student();
return s;
}
//方法二:创建匿名对象
public Student way(){
return new Student();
}
}
public class Test {
public static void main(String[] args) {
//调用StudentDemo中的stuty()方法
//方法二:匿名对象的调用
StudentDemo sd=new StudentDemo();
Student s = sd.way(); //等号左边study为引用类型Student的局部变量
s.learn();
//方法一的调用
Student m=sd.study();
m.learn();
}
}
//实现结果:为自己而学习
//为自己而学习
public abstract class Person {
public void eat() {
System.out.println("你好才是真的好");
}
}
public class Worker extends Person {
public void eat() {
System.out.println("劳动创造价值");
}
}
public class Student {
//方法返回值为Person类型
//方法一:分步走
public Person function() {
Person p=new Worker();
return p;
}
//方法二:创建匿名对象
public Person way() {
return new Worker();
}
}
public class Test {
public static void main(String[] args) {
//调用Student中function()
//方式二:匿名对象
Student s=new Student();
Person p = s.function();
p.eat();
//方法一:分步走
Person n=s.way(); //s是Student的对象
n.eat();
}
}
//返回结果:劳动创造价值
//劳动创造价值
public interface USB {
public abstract void show();
}
public class Computer implements USB {
@Override
public void show() {
System.out.println("传递数据或者连接其他设备");
}
}
public class Tel {
//分步走
public USB function() {
USB u=new Computer();
return u;
}
//匿名对象
public USB way() {
return new Computer();
}
}
public class Test {
public static void main(String[] args) {
Tel t=new Tel();
//方法一:分步走
USB f= t.function();
f.show();
//匿名对象
USB w = t.way();
w.show();
}
}