/*方法返回值问题:
* 基本数据类型:需要什么数据类型,在调用方法的时候,就使用该具体类型去接收!
* 引用数据类型:
* 返回值如果是:
* 具体类:需要返回该具体类的对象!
* 抽象类:需要返回该抽象类的子类对象
* 接口:需要返回该接口的子实现类对象
* */
//定义一个接口(接口不能实例化,它需要通过子实现类实例化)
interface Inter{
public abstract void work() ;
}
//定义接口的子实现类
class InterImpl implements Inter{
@Override
public void work() {
System.out.println("hello world");
}
}
//定义一个类
class InterDemo{
public Inter method() {
// 分步走
/*Inter inter = new InterImpl();//接口多态
return inter;*/
// 一步走
return new InterImpl();
}
}
//测试类
public class ReturnDemo3 {
public static void main(String[] args) {
//调用InterDemo类中的method()
InterDemo id=new InterDemo();
Inter method = id.method();//方法完成了:new InterImpl();
method.work();
}
}