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. Populating Next Right Pointers in Each Node [LeetCode]

    Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...

  2. 131. 132. Palindrome Partitioning *HARD* -- 分割回文字符串

    131. Palindrome Partitioning Given a string s, partition s such that every substring of the partitio ...

  3. java 分析方法调用过程

    StackTraceElement[] s = new Exception().getStackTrace(); for(int i=0;i<s.length;i++) System.out.p ...

  4. 转载,javascript 设计模式

    了解JavaScript设计模式我们需要知道的一些必要知识点:(内容相对基础,高手请跳过) 闭包:关于闭包这个月在园子里有几篇不错的分享了,在这我也从最实际的地方出发,说说我的理解. 1.闭包最常用的 ...

  5. 各种浏览器hack

    Hack是针对不同的浏览器去写不同的CSS样式,从而让各浏览器能达到一致的渲染效果,那么针对不同的浏览器写不同的CSS CODE的过程,就叫CSS HACK,同时也叫写CSS Hack.然后将Hack ...

  6. GO工程和包

    转载:http://blog.csdn.net/achelloworld/article/details/43956831 在Go语言中,包是函数与数据的集合,用package定义一个包,包的名字不一 ...

  7. 生成guid

    http://jingyan.baidu.com/article/84b4f565eebb9d60f6da3293.html

  8. mongodb 和 mysql 的对照

    In addition to the charts that follow, you might want to consider the Frequently Asked Questions sec ...

  9. php中curl和fsockopen发送远程数据的应用

    最近要用到通过post上传文件,网上盛传的有curl的post提交和fsockopen,其中curl最简单,于是从最简单的说起. 这是简单的将一个变量post到另外一个页面 $url = ''; $d ...

  10. iOS 登陆的实现四种方式

    iOS 登陆的实现四种方式 一. 网页加载: http://www.cnblogs.com/tekkaman/archive/2013/02/21/2920218.ht ml [iOS登陆的实现] A ...