springbot项目中使用继承
package com.example.demo.controller; import com.sun.org.apache.bcel.internal.generic.NEW; import org.springframework.web.bind.annotation.*; /** * @program: demo->DemoController * @description: demo * @author: cxy * @create: 2019-11-13 16:47 **/ @RestController public class DemoController { @RequestMapping(value = "/", method = RequestMethod.POST) public RspResult detail(@RequestBody TraceReqCancelOrder traceReqCancelOrder) { TraceRspResult rsp = new TraceRspResult(); String traceId = traceReqCancelOrder.getTraceId(); System.out.println(traceId); System.out.println(traceReqCancelOrder.toString()); check(traceReqCancelOrder); rsp.setTraceId("11111111111111111"); rsp.setBody(1); rsp.setMsg("success"); rsp.setResult(1); return rsp; } public static void check(ReqCancelOrder order){ String traceId = order.getTeacherId(); System.out.println(traceId); } @RequestMapping(value = "/2", method = RequestMethod.POST) public RspResult detail1(@PathVariable Integer id) { TraceRspResult rsp = new TraceRspResult(); System.out.println(id); rsp.setTraceId("11111111111111111"); rsp.setBody(1); rsp.setMsg("success"); rsp.setResult(1); return rsp; } @RequestMapping(value = "/3", method = RequestMethod.POST) public RspResult detail2(@PathVariable Integer id) { RspResult rsp = new TraceRspResult(); System.out.println(id); rsp.setTraceId("11111111111111111"); rsp.setBody(1); rsp.setMsg("success"); rsp.setResult(1); return rsp; } }
package com.example.demo.controller; import com.fasterxml.jackson.annotation.JsonProperty; public class ReqCancelOrder { @JsonProperty("SerialNo") private String serialNo; @JsonProperty("TeacherId") private String teacherId; @JsonProperty("TeamId") private String teamId; @JsonProperty("LocalOrderNo") private long localOrderNo; public String getSerialNo() { return serialNo; } public void setSerialNo(String serialNo) { this.serialNo = serialNo; } public String getTeacherId() { return teacherId; } public void setTeacherId(String teacherId) { this.teacherId = teacherId; } public String getTeamId() { return teamId; } public void setTeamId(String teamId) { this.teamId = teamId; } public long getLocalOrderNo() { return localOrderNo; } public void setLocalOrderNo(long localOrderNo) { this.localOrderNo = localOrderNo; } @Override public String toString() { return "ReqCancelOrder{" + "serialNo='" + serialNo + '\'' + ", teacherId='" + teacherId + '\'' + ", teamId='" + teamId + '\'' + ", localOrderNo=" + localOrderNo + '}'; } }
package com.example.demo.controller; import com.fasterxml.jackson.annotation.JsonProperty; import java.util.List; public class ReqOrderStatusQuery { // 序列号 @JsonProperty("SerialNo") private String serialNo; // 订单请求列表 @JsonProperty("Orders") private List<Order> orders ; public List<Order> getOrders() { return orders; } public void setOrders(List<Order> orders) { this.orders = orders; } public static class Order { // 老师ID @JsonProperty("TeacherId") private String teacherId; // 委托单号 @JsonProperty("LocalOrderNo") private long localOrderNo; public String getTeacherId() { return teacherId; } public void setTeacherId(String teacherId) { this.teacherId = teacherId; } public long getLocalOrderNo() { return localOrderNo; } public void setLocalOrderNo(long localOrderNo) { this.localOrderNo = localOrderNo; } @Override public String toString() { return "Order{" + "teacherId='" + teacherId + '\'' + ", localOrderNo=" + localOrderNo + '}'; } } @Override public String toString() { return "ReqOrderStatusQuery{" + "serialNo='" + serialNo + '\'' + ", orders=" + orders + '}'; } }
package com.example.demo.controller; import com.fasterxml.jackson.annotation.JsonProperty; public class RspResult { public static final String SUCCESS_MSG = "success"; public static final String FAIL_MSG = "fail"; @JsonProperty("Result") private int result; @JsonProperty("Msg") private String msg; @JsonProperty("Body") private Object body; public static RspResult genSucess(Object object) { RspResult rspResult = new RspResult(); rspResult.setResult(1); rspResult.setMsg(SUCCESS_MSG); rspResult.setBody(object); return rspResult; } public static RspResult genFail(String msg) { RspResult rspResult = new RspResult(); rspResult.setResult(-1); rspResult.setMsg(msg); rspResult.setBody(null); return rspResult; } public int getResult() { return result; } public void setResult(int result) { this.result = result; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public Object getBody() { return body; } public void setBody(Object body) { this.body = body; } }
package com.example.demo.controller; import com.fasterxml.jackson.annotation.JsonProperty; /** * @program: tdsimulate->TraceReqCancelOrder * @description: 接入监控平台的取消委托参数 * @author: cxy * @create: 2019-11-13 16:24 **/ public class TraceReqCancelOrder extends ReqCancelOrder{ //traceId 链路追踪id @JsonProperty("TraceId") private String traceId; public String getTraceId() { return traceId; } public void setTraceId(String traceId) { this.traceId = traceId; } @Override public String toString() { return "TraceReqCancelOrder{" + "traceId='" + traceId + '\'' + "} " + super.toString(); } }
package com.example.demo.controller; import com.fasterxml.jackson.annotation.JsonProperty; /** * @program: tdsimulate->TraceRspResult * @description: 接入业务系统接入监控平台的返回Dto * @author: cxy * @create: 2019-11-13 10:53 **/ public class TraceRspResult extends RspResult { //traceId ---首节点生成的traceid @JsonProperty("TraceId") private String traceId; public String getTraceId() { return traceId; } public void setTraceId(String traceId) { this.traceId = traceId; } }
可以看主题:今天是继承,所以那么就要采用继承:
这个地方很明显看出问题,就是创建对象时候用子父类去接受子类,是可以的,但是在后面如果去操作子类,是不可以的,编译器直接报错,,如果不操作子类属性是可以的,
那么得出结论:就是父类可以接收子类,但是接受的父类,不可以操作子类的属性
同理:
如果是父类接受,那么也可以进行,但是只要不操作,子类的属性,
笔记转移,由于在有道云的笔记转移,写的时间可能有点久,如果有错误的地方,请指正