【Java基础语法】多个对象合并成一个对象返回案例(57)

直接上代码:

package com.itheima2;

public class Cat {
	private String name;
	private String gender;
	private String color;
	private String variety;  // 品种
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
	public String getVariety() {
		return variety;
	}
	public void setVariety(String variety) {
		this.variety = variety;
	}
}
package com.itheima2;

public class Dog {
	private String name;
	private String gender;
	private String color;
	private String variety;  // 品种
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
	public String getVariety() {
		return variety;
	}
	public void setVariety(String variety) {
		this.variety = variety;
	}
}
package com.itheima2;

public class Animal {
	private Dog dog;
	private Cat cat;
	
	public Dog getDog() {
		return dog;
	}
	public void setDog(Dog dog) {
		this.dog = dog;
	}
	public Cat getCat() {
		return cat;
	}
	public void setCat(Cat cat) {
		this.cat = cat;
	}
}

测试返回输出:

package com.itheima2;

public class TestDemo {
	public static void main(String[] args) {
		
		Animal a = AnimalTest();
		System.out.println(a.getCat().getColor());
		System.out.println(a.getCat().getGender());
		System.out.println(a.getCat().getName());
		
	}
	
	private static Animal AnimalTest() {
		Animal animal = new Animal();
		Dog dog = new Dog();
		Cat cat = new Cat();
		
		dog.setColor("土黄色");
		dog.setName("小黄狗");
		dog.setGender("公的");
		cat.setColor("白色");
		cat.setName("咪咪");
		cat.setGender("公的");
		animal.setCat(cat);
		animal.setDog(dog);
		return animal;
	}
}

大对象包含小对象返回数据:
最终返回的这个大对象也可以放入Rest封装的工具类返回前台;

一、工具类一RestResponse
RestResponse(对内Rest接口-推荐)
package com.example.demo.result;import java.io.Serializable;/**

  • REST接口统一返回数据工具类封装RestResponse
  • @param
    /publicclassRestResponseimplementsSerializable{privatestaticfinallong serialVersionUID=3728877563912075885L;privateint code;private String msg;private T data;publicRestResponse(){}publicRestResponse(int code, String message, T data){this.code= code;this.setMsg(message);this.data= data;}publicRestResponse(int code, T data){this.code= code;this.data= data;}publicRestResponse(int code, String message){this.code= code;this.setMsg(message);}/*
    • 成功时-返回data
    • @param
    • @return
      /publicstatic RestResponsesuccess(T data){returnnewRestResponse(200, null, data);}/*
    • 成功-不返回data
    • @param
    • @return
      /publicstatic RestResponsesuccess(String msg){returnnewRestResponse(200, msg);}/*
    • 成功-返回data+msg
    • @param
    • @return
      /publicstatic RestResponsesuccess(String msg, T data){returnnewRestResponse(200, msg, data);}/*
    • 失败
    • @param
    • @return
      /publicstatic RestResponsefail(String msg){returnnewRestResponse(500, msg,null);}/*
    • 失败-code
    • @param
    • @return
      /publicstatic RestResponsefail(int code, String msg){returnnewRestResponse(code, msg,null);}publicintgetCode(){return code;}public StringgetMsg(){return msg;}public TgetData(){return data;}publicvoidsetCode(int code){this.code= code;}publicvoidsetMsg(String msg){this.msg= msg;}publicvoidsetData(T data){this.data= data;}@Overridepublic StringtoString(){return"RestResponse{“+“code=”+ code+”, msg=‘"+ msg+’‘’+“, data=”+ data+‘}’;}}
      二、工具类二RpcResponse
      RpcResponse(对外Rpc接口)
      package com.example.oss.result;import lombok.Builder;import lombok.Data;import lombok.experimental.Accessors;import java.io.Serializable;/
      *
  • @desc:
  • @author: cao_wencao
  • @date: 2020-11-06 17:04
    /@Data@Accessors(chain=true)@BuilderpublicclassRpcResponseimplementsSerializable{publicstaticfinalint CODE_200=200;publicstaticfinalint CODE_500=500;privatestaticfinallong serialVersionUID=-1559957698621135646L;/*
    • 消息 CODE_200
      /privateint code= CODE_500;/*
    • 信息
      /private String message;/*
    • 数据
      /private Object data;/*
    • Instantiates a new Api result.
      /publicRestResponse(){super();}/*
    • Instantiates a new Api result.
    • @param code the code
    • @param message the message
    • @param data the data
      /publicRpcResponse(int code, String message, Object data){this.code= code;this.message= message;this.data= data;}/*
    • Instantiates a new Api result.
    • @param code the code
    • @param message the message
      /publicRpcResponse(int code, String message){this(code, message, null);}/*
    • 错误
    • @param message the message
    • @return api result
      /publicstatic RpcResponseerror(String message){returnnewRpcResponse(CODE_500, message, null);}/*
    • 错误
    • @param code the code
    • @param message the message
    • @return api result
      /publicstatic RpcResponseerror(int code, String message){returnnewRpcResponse(code, message);}/*
    • 成功
    • @param message the message
    • @return api result
      /publicstatic RpcResponsesuccee(String message){returnnewRpcResponse(CODE_200, message);}/*
    • 成功
    • @param data the data
    • @param message the message
    • @return api result
      /publicstatic RpcResponsesuccee(Object data, String message){returnnewRpcResponse(CODE_200, message, data);}/*
    • 成功
    • @param data the data
    • @return api result
      /public RpcResponsesuccess(Object data){returnnewRpcResponse(CODE_200, null, data);}}
      三、工具类三RestResponse
      RestResponse(对内Rest接口)
      package com.example.oss.result;import lombok.Builder;import lombok.Data;import lombok.experimental.Accessors;import java.io.Serializable;/
      *
  • @desc:
  • @author: cao_wencao
  • @date: 2020-11-06 17:04
    /@Data@Accessors(chain=true)@BuilderpublicclassRestResponseimplementsSerializable{publicstaticfinalint CODE_200=200;publicstaticfinalint CODE_500=500;privatestaticfinallong serialVersionUID=-1559957698621135646L;/*
    • 消息 CODE_200
      /privateint code= CODE_500;/*
    • 信息
      /private String message;/*
    • 数据
      /private Object data;/*
    • Instantiates a new Api result.
      /publicRestResponse(){super();}/*
    • Instantiates a new Api result.
    • @param code the code
    • @param message the message
    • @param data the data
      /publicRestResponse(int code, String message, Object data){this.code= code;this.message= message;this.data= data;}/*
    • Instantiates a new Api result.
    • @param code the code
    • @param message the message
      /publicRestResponse(int code, String message){this(code, message, null);}/*
    • 错误
    • @param message the message
    • @return api result
      /publicstatic RestResponseerror(String message){returnnewRestResponse(CODE_500, message, null);}/*
    • 错误
    • @param code the code
    • @param message the message
    • @return api result
      /publicstatic RestResponseerror(int code, String message){returnnewRestResponse(code, message);}/*
    • 成功
    • @param message the message
    • @return api result
      /publicstatic RestResponsesuccee(String message){returnnewRestResponse(CODE_200, message);}/*
    • 成功
    • @param data the data
    • @param message the message
    • @return api result
      /publicstatic RestResponsesuccee(Object data, String message){returnnewRestResponse(CODE_200, message, data);}/*
    • 成功
    • @param data the data
    • @return api result
      */public RestResponsesuccess(Object data){returnnewRestResponse(CODE_200, null, data);}}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

KevinDuc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值