Generic method return type
Here's the Animal class:
public class Animal{
private Map<String,Animal> friends =new HashMap<String,Animal>();
public void addFriend(String name,Animal animal){
friends.put(name,animal);
}
public Animal callFriend(String name)
{return friends.get(name);}}
And here's some code snippet with lots of typecasting:
Mouse jerry =newMouse();
jerry.addFriend("spike",newDog());
jerry.addFriend("quacker",newDuck());
((Dog) jerry.callFriend("spike")).bark();
((Duck) jerry.callFriend("quacker")).quack();
Is there any way I can use generics for the return type to get rid of the typecasting, so that I can say
jerry.callFriend("spike").bark();
jerry.callFriend("quacker").quack();
Here's some initial code with return type conveyed to the method as a parameter that's never used.
public<T extendsAnimal> T callFriend(String name, T unusedTypeObj){return(T)friends.get(name);}
Answer==
public<T extendsAnimal> T callFriend(String name,Class<T> type){return type.cast(friends.get(name));}
Generic method return type的更多相关文章
- 一种封装Retrofit的方法,可以自动解析Gson,回避Method return type must not include a type variable or wildcard: retrofit2.Call<T>的问题
封装目的:屏蔽底层实现,提供统一接口,并支持Gson自动转化 最初封装: //请求方法 interface RequestListener { interface PostListener { @PO ...
- delete attempted to return null from a method with a primitive return type (int)
今天被自己给蠢死了 今天在代码中遇到这个错误, 百度翻译一下:映射方法,从一org.system.mapper.child.chmorganizationexaminationmapper.delet ...
- rg.apache.ibatis.binding.BindingException: Mapper method 'com.dao.Cameao.getOnlineDayRation attempted to return null from a method with a primitive return type (float)
本文为博主原创,未经允许不得转载: 异常展示如下: org.apache.ibatis.binding.BindingException: Mapper method 'com.dao.Cameao. ...
- mysql查询null异常:attempted to return null from a method with a primitive return type
select sum(deposit_amount)from tb_commission_ib_day mysql查询时报异常: attempted to return null from a met ...
- org.apache.ibatis.binding.BindingException: Mapper method 'attempted to return null from a method with a primitive return type (long).
一.问题描述 今天发现测试环境报出来一个数据库相关的错误 org.apache.ibatis.binding.BindingException: Mapper method 'attempted to ...
- [TypeScript] Infer the Return Type of a Generic Function Type Parameter
When working with conditionals types, within the “extends” expression, we can use the “infer” keywor ...
- attempted to return null from a method with a primitive return type (int).
java接口文件 package com.cyb.ms.mapper; import org.apache.ibatis.annotations.Param; public interface Acc ...
- Bean property ‘mapperHelper’ is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
spring boot 报错: Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property ...
- Mapper method 'com.xxxx.other.dao.MyDao.getNo attempted to return null from a method with a primitive return type (int)
使用myBatis调用存储过程的返回值,提示错误信息: org.apache.ibatis.binding.BindingException: Mapper method 'com.xxxx.othe ...
随机推荐
- AngularJS理论基础
AngularJS理论基础 AngularJs是一个用于设计动态web应用的结构框架. 它是一个框架,不是类库,是像EXT一样提供一整套方案用于设计web应用.它不仅仅是一个javascript框架, ...
- eclipse 项目修改和更新项目,回退版本,解决分支的冲突的办法
一个关于git的图 1.我在github建立了3个分支. 2.把其中一个分支拉到本地. 项目修改提交到远程库 3.修改完代码以后commit项目,点击项目右击->team->commit ...
- Ubuntu 下Eclipse 安装SVN
如果尚未安装Eclipse,先安装:也可以直接下载Google提供的ADT Bundle. sudo apt-get install eclipse 安装Subversion sudo apt-get ...
- 【vmware vcp 5.1】安装及配置及笔记散记
ESXi的几个命令技巧: ------------------------------------------------- alt-f1: 进入console alt-f2: 返回DCUI alt- ...
- spark 学习
三种编译方式 1. 编译文档:more—>buiding spark 2. 三种编译方式:SBT,Maven,打包编译 make-distribution.sh 运行方式 local,stand ...
- 喜讯!Ubuntu 16.10(Yakkety Yak) Final Beta发布喽!!!
上月三十日,代号为"Yakkety Yak"的Ubuntu 16.10发行版本的Final Beta正式上线.Canonical的开发者Steve Langasek说道:" ...
- poj题目必做
OJ上的一些水题(可用来练手和增加自信) (poj3299T,poj2159T,poj2739T,poj1083T,poj2262T,poj1503T,poj3006T,poj2255T,poj309 ...
- LCA-倍增法(在线)
原文:http://www.tuicool.com/articles/N7jQV32 1. DFS预处理出所有节点的深度和父节点 inline void dfs(int u) { int i; for ...
- UITableViewCell Property “icon” cannot be found in forward class object “DJWeiBo”
UITableViewCell 自定义表格 实体属性不显示错误 Property “icon” cannot be found in forward class object “DJWeiBo”引入实 ...
- “更高效率:标准化+简约风+移动化”--K2 BPM老客户交流会
主题:工作流主数据标准化和移动工作流带来的企业沟通建设机会 嘉宾:李瑞延(盛大网络IT总监) 公司管理需要更好的工作流 -为决策提供依据 通过对各级业务公司各类流程数据的获取与分析,为管理决策提供必要 ...