今天刚好看到这个地方,所以学习了一下,虽然说不完全是继承关系,但是这个例子有助于我们对“父类对象调用子类方法”的理解。新建一个springboot,结构目录如下:
package com.example.demo.service.interFace;
public interface TaskService {
String create();
}
package com.example.demo.service.impl;
import com.example.demo.service.interFace.TaskService;
import org.springframework.stereotype.Service;
@Service
public class TaskImpl implements TaskService {
@Override
public String create(){
return "create a task!";
}
}
package com.example.demo.controller;
import com.example.demo.service.interFace.TaskService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
@Controller
public class TaskController {
@Resource
private TaskService taskService;
@RequestMapping("/task")
@ResponseBody
public String create(){
return taskService.create();
}
}
最后在浏览器输入:http://localhost:8080/task