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的更多相关文章

  1. 一种封装Retrofit的方法,可以自动解析Gson,回避Method return type must not include a type variable or wildcard: retrofit2.Call<T>的问题

    封装目的:屏蔽底层实现,提供统一接口,并支持Gson自动转化 最初封装: //请求方法 interface RequestListener { interface PostListener { @PO ...

  2. delete attempted to return null from a method with a primitive return type (int)

    今天被自己给蠢死了 今天在代码中遇到这个错误, 百度翻译一下:映射方法,从一org.system.mapper.child.chmorganizationexaminationmapper.delet ...

  3. 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. ...

  4. 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 ...

  5. 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 ...

  6. [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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. nyoj------203三国志

    三国志 时间限制:3000 ms  |  内存限制:65535 KB 难度:5  描述 <三国志>是一款很经典的经营策略类游戏.我们的小白同学是这款游戏的忠实玩家.现在他把游戏简化一下,地 ...

  2. jdbc 配置properties实现

    package com.web.study; import java.io.InputStream; import java.sql.Connection; import java.sql.Drive ...

  3. IO流--字节流

    import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import ...

  4. Matlab boxplot for Multiple Groups(多组数据的箱线图)

    在画之前首先介绍一下Matlab boxplot,下面这段说明内容来自http://www.plob.org/2012/06/10/2153.html   由于matlab具有强大的计算功能,用其统计 ...

  5. web自定义控件UserControl

    今天做了两个自定义控件,之前用WPF也做过,但是感觉跟今天的不太一样.首先是在项目中建了一个UserContral的控件界面,把需要的控件拖到里面,再给按钮添加事件.我们公司的控件都是买的Dev Ex ...

  6. CentOS 常用命令大全(2)

    系统 # uname -a               # 查看内核/操作系统/CPU信息 # head -n 1 /etc/issue   # 查看操作系统版本 # cat /proc/cpuinf ...

  7. Oozie简介

    在Hadoop中执行的任务有时候需要把多个Map/Reduce作业连接到一起,这样才能够达到目的.[1]在Hadoop生态圈中,有一种相对比较新的组件叫做Oozie[2],它让我们可以把多个Map/R ...

  8. js中Array自定义contains, indexOf, delete方法.

    Array.prototype.contains = function (elem) { for (var i = 0; i < this.length; i++) { if (this[i] ...

  9. PostgreSQL数据库系统优点

    PostgreSQL 是世界上可以获得的最先进的开放源码的数据库系统, 它提供了多版本并行控制,支持几乎所有 SQL 构件(包括子查询,事务和用户定 义类型和函数), 并且可以获得非常广阔范围的(开发 ...

  10. Spring学习笔记之初始化和销毁方法的调用次序

    Multiple lifecycle mechanisms configured for the same bean, with different initialization methods, a ...