从Java方法返回抽象类的实例
我有一种需要返回抽象类类型“交易”对象的方法。类“ Premiumiumiumiumiunsaction”和“非属性交流”都扩展了“交易”。附加的代码引发了“丢失的返回语句”错误。而且我不确定在此方法中返回什么。
private Transaction myMethod( String jsonString ,String transactionType){
switch(transactionType){
case "PREMIUM":
return mapper.readValue(jsonString, PremiumTransaction.class);
case "NON-PREMIUM":
return mapper.readValue(jsonString, NonPremiumTransaction.class);
case "UNKNOWN":
throw new IllegalArgumentException("Unable to parse JSON string : " + json String);
}
}
我尝试初始化一个类型“事务”对象喜欢的对象
Transaction transaction = null;
,并在开关情况下分配了避免的对象,
transaction = mapper.readValue(jsonString , PremiumTransaction.class);
但是如果交易类型的参数==“未知”,我不想返回null对象。
I have a method that needs to return an object of abstract class type 'Transaction'. The classes 'PremiumTransaction' and 'NonPremiumTransaction' both extend 'Transaction'. The code attached here throws 'missing return statement' error. And I am not sure what to return in this method.
private Transaction myMethod( String jsonString ,String transactionType){
switch(transactionType){
case "PREMIUM":
return mapper.readValue(jsonString, PremiumTransaction.class);
case "NON-PREMIUM":
return mapper.readValue(jsonString, NonPremiumTransaction.class);
case "UNKNOWN":
throw new IllegalArgumentException("Unable to parse JSON string : " + json String);
}
}
I tried initializing an object of type 'Transaction' like
Transaction transaction = null;
and assigning the deserialized object inside the switch cases like
transaction = mapper.readValue(jsonString , PremiumTransaction.class);
But I don't want to return a null object if the argument for transaction type == "UNKNOWN"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论