java 泛型 假 为什么,Java泛型愚蠢的事情(为什么我不能推断这种类型?)

I´ll try to be short as the question has not been very answered. For the long explanation, go after this briefing.

I will show what Im trying to do. Something like this (infering the incoming type from the constructor in order to use it in another method getLeaderHerd as a return type)... :

public class ZooCage{

private CageFamily inhabitants;

public ZooCage(CageFamily herd) {

this.inhabitants=herd;

}

public T getHerdLeader() {

return inhabitants.getLeader();

}

}

or this

public class ZooCage{

private (Type) T;

public ZooCage(CageFamily herd) {

this.T=T;

}

public T getHerdLeader() {

return inhabitants.getLeader();

}

}

so I could call from Main something like:

ZooCage cage = new ZooCage(new CageFamily()); //Imagine new CageFamily its not empty

Lion leader = cage.getHerdLeader();

Even if its not possible, why should I think that that is not reasonable to feature? Its type safe if the compiler is intelligent and less redundant that typifiing the class ZooCage which is not necessary

I,m assessing using Generics for a particular behavior. I managed to get it work but I don,t understand why cant I infer the type from the arg. So I created this example which runs ok without warnings with the purpose of simplifying the actual architecture.

( Look directly the last 2-lines snippet for a fast briefing )

Suppose I got these two classes. The target one:

package Zoo;

import Zoo.Main.CageFamily;

import Zoo.Main.Vertebrate;

public class ZooCage{

private CageFamily inhabitants;

public ZooCage(CageFamily herd) {

this.inhabitants=herd;

}

public T getHerdLeader() {

return inhabitants.getLeader();

}

}

Imagine that in cages there can only be Vertebrates (Insects/aracnids are not to big ang giant quids/octopus needs aquatic medium)

The other one class, Main.java

package Zoo;

import java.util.ArrayList;

public class Main {

public static void main(String[] args){

new Main().test();

}

public void test(){

CageFamily lionsHerd = new CageFamily();

lionsHerd.add(new Lion("Simba"));

lionsHerd.add(new Lion("Nala"));

CageFamily bearsHerd = new CageFamily();

bearsHerd.add(new Bear("Yogi"));

bearsHerd.add(new Bear("Boo-boo"));

ZooCage cageLions = new ZooCage(lionsHerd);

ZooCage cageBears = new ZooCage(bearsHerd);

for (ZooCage> cage : new ZooCage[]{cageLions,cageBears} )

System.out.println("The leader is "+ cage.getHerdLeader());

}

public interface Vertebrate{

public String toString();

public int numBones();

}

public class Lion implements Vertebrate{

private String name;

public Lion (String name){this.name=name;}

public String toString(){return name + " (who has "+numBones()+" bones)";}

public int numBones(){return 345;}

}

public class Bear implements Vertebrate{

private String name;

public Bear (String name){this.name=name;}

public String toString(){return name + " (who has "+numBones()+" bones)";}

public int numBones(){return 658;}

}

public class CageFamily extends ArrayList{

final static long serialVersionUID = 1L;

public E getLeader(){

return get(0); //Let,s assume the first added is the leader

}

}

}

This compiles OK and prints

The leader is Simba (who has bones)

The leader is Yogi (who has bones)

What Im wondering is: Is there any way (only using types/generics and without supressingWarnings nor castings) to avoid the typification of the class ZooCage as a whole? I tried thousand ways to get the inference of the type from ZooCage,s constructor arg to the return value for getHerdLeader. It should be not necessary typifying ZooCage when his constructor comes with the expected type. Seems redundant and makes you having to know beforehand the type!

Thank you very much to all who can help!

解决方案

Java 7 lets you do ZooCage = new ZooCage<>(argument). That feature is new in Java 7, though, and isn't available in earlier versions of Java.

Alternately, a traditional way of getting around Java 6's lack of type inference is to write a factory method

public static ZooCage newZooCage() {

return new ZooCage();

}

and then newZooCage() gets its type automatically inferred, since even Java 5 had type inference for methods -- just not for constructors.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值