使用Java轻松访问对象Getter
方法1:传统的getter/setter 丰田级:使用Java轻松访问对象Getter,java,getter,setter,Java,Getter,Setter,方法1:传统的getter/setter 丰田级: public class ToyotaCar implements Serializable { private static final long serialVersionUID = 2011932556974180375L; private int miles; public void addMiles(int miles){ this.miles = miles; } public int get
public class ToyotaCar implements Serializable {
private static final long serialVersionUID = 2011932556974180375L;
private int miles;
public void addMiles(int miles){
this.miles = miles;
}
public int getMiles(){
return miles;
}
}
人类阶级:
public class Human implements Serializable {
private static final long serialVersionUID = 1748193556974180375L;
private ToyotaCar car;
public void setCar(ToyotaCar car){
this.car = car;
}
public int getCar(){
return car;
}
public void addCarMiles(int num){
getCar().addMiles(num);
}
}
public class Human implements Serializable {
private static final long serialVersionUID = 1748193556974180375L;
private HashMap<HumanContentsContainer, Object> contents;
public void setContents(){
for (HumanContentsContainer c : HumanContentsContainer.values()){
contents.put(c, c.getContainer());
}
}
public HashMap<HumanContentsContainer, Object> getContents(){
return contents;
}
public void addCarMiles(int num){
//TODO how to replicate this: getCar().addMiles(num);???
}
//TODO i dont want to use the below method because whats the point of creating a whole container handler if im just going to use a traditional getter again?
//public ToyotaCar getCar(){
// return (ToyotaCar) contents.get(HumanContentsContainer.CAR);
// }
}
方法2:“其他”
丰田等级:-与上述丰田等级相同-
附加集装箱装卸工等级:
public enum HumanContentsContainer {
CAR{
@Override public Object getContainer(){
return new ToyotaCar();
}
},
HOUSE;
public Object getContainer(){ //because cannot be static enum constant as every human has different items
return null;
}
}
人类阶级:
public class Human implements Serializable {
private static final long serialVersionUID = 1748193556974180375L;
private ToyotaCar car;
public void setCar(ToyotaCar car){
this.car = car;
}
public int getCar(){
return car;
}
public void addCarMiles(int num){
getCar().addMiles(num);
}
}
public class Human implements Serializable {
private static final long serialVersionUID = 1748193556974180375L;
private HashMap<HumanContentsContainer, Object> contents;
public void setContents(){
for (HumanContentsContainer c : HumanContentsContainer.values()){
contents.put(c, c.getContainer());
}
}
public HashMap<HumanContentsContainer, Object> getContents(){
return contents;
}
public void addCarMiles(int num){
//TODO how to replicate this: getCar().addMiles(num);???
}
//TODO i dont want to use the below method because whats the point of creating a whole container handler if im just going to use a traditional getter again?
//public ToyotaCar getCar(){
// return (ToyotaCar) contents.get(HumanContentsContainer.CAR);
// }
}
寻找一些简单的用法,如:
human.getContentsThatIsIntanceOf(ToyotaCar).addMiles(1);
但我不知道GetContentsInstanceof会是什么样子我会选择:
public class Human implements Serializable {
private static final long serialVersionUID = 1748193556974180375L;
private ToyotaCar car;
public void setCar(ToyotaCar car){
this.car = car;
}
public int getCar(){
return car;
}
public void addCarMiles(int num){
getCar().addMiles(num);
}
public Map<HumanContentsContainer, Object> getContents(){
Map<HumanContentsContainer, Object>map = new HashMap();
map.put(CAR,this.car );
//same for all the shoes and clothes and whatever the Human has
}
public void setContents(){
for (HumanContentsContainer c : HumanContentsContainer.values()){
switch (c){
case CAR:{
this.car=c.getContainer();
}
}
//and so on
}
}
公共类人工实现可序列化{
私有静态最终长serialVersionUID=1748193556974180375L;
私人丰田汽车;
公共汽车(丰田汽车){
这辆车;
}
公共int getCar(){
返回车;
}
公共无效地址(整数){
getCar().addMiles(num);
}
公共地图getContents(){
Mapmap=新的HashMap();
放(车,这辆车);
//所有的鞋子、衣服和人类所有的东西都是一样的
}
公共内容{
对于(HumanContentsContainer c:HumanContentsContainer.values()){
开关(c){
箱子车:{
this.car=c.getContainer();
}
}
//等等
}
}
}
编辑
如果你需要一套动态的功能,我建议你确实要保留对象的地图,并摆脱“addCarMiles”方法,因为它意味着每个人都有一辆汽车
我将在“performCommand(CapabilityType,CapabilityCommand)”上实现public方法,命令将在其中接收能力并对其执行操作。您可以查看命令模式教程
编辑2:
如果您只想创建一个返回动态类型的getter,那么可以使用泛型
import java.io.Serializable;
import java.util.HashMap;
import java.util.NoSuchElementException;
public class Human implements Serializable {
private static final long serialVersionUID = 1748193556974180375L;
private HashMap<Class, Object> contents;
public void setContents(){
for (HumanContentsContainer c : HumanContentsContainer.values()){
contents.put(c.getContainer().getClass(), c.getContainer());
}
}
public HashMap<Class, Object> getContents(){
return contents;
}
public <T> T getContentsThatIsIntanceOf(Class<T> type){
Object object = contents.get(type);
if (object==null){
throw new NoSuchElementException("No such element: "+type.getName());
}
return type.cast(object);
}
public void usageExample(){
this.getContentsThatIsIntanceOf(ToyotaCar.class).addMiles(10);
}
}
import java.io.Serializable;
导入java.util.HashMap;
导入java.util.NoSuchElementException;
公共类人工实现可序列化{
私有静态最终长serialVersionUID=1748193556974180375L;
私有HashMap内容;
公共内容{
对于(HumanContentsContainer c:HumanContentsContainer.values()){
put(c.getContainer().getClass(),c.getContainer());
}
}
公共HashMap getContents(){
返回内容;
}
公共T GetContentsHatIsInstanceof(类类型){
Object=contents.get(类型);
if(object==null){
抛出新的NoSuchElementException(“没有这样的元素:+type.getName());
}
返回类型.cast(对象);
}
public void usageExample(){
this.getContentsThatIsIntanceOf(ToyotaCar.class).addMiles(10);
}
}
如果您有一个公共方法“addCarMiles”,为什么不像方法a那样有一个私有的强类型字段“car”。?你已经假设你的人类可以为汽车增加里程。我也会放开私人的接受者/接受者,除非你做一些懒散的工作。因为不仅仅是一辆车,我会添加更多的内容,比如房子、汽车、鞋子、衣服,以及我在原始帖子预编辑中忘记添加的内容;hashmap组件有一个getter。因此,我希望它的使用与getContents()一样简单。addMiles(x)您已经将这些对象的大部分功能代理给了用户。如果我需要返回一个映射,我会与成员一起初始化映射,并在需要时返回它,但也会与一流成员一起工作。对不起,我最初将getter和setter示例编写为private,它们是公开的,需要时返回映射是什么意思?因为地图会返回一个对象,而不是丰田汽车,所以你不能这样做。addMiles方法我想你没有抓住重点,我需要的是一个更好的getter而不是setter。我怎样才能创建一个通用的getter,在我需要时返回instanceof Car或instanceof House?@JackSmith查看编辑2