我正在学习java中的泛型并遇到此问题。将参考类型作为参数传递并使用泛型
我有一个天气界面。 EarthQuake
和VirginiaWeather
是实现Weather
接口的两个类。这两个类都有一个静态方法 - "parseData"
,它解析来自原子提要的数据。 Main类中的getFeeds
方法有一个参数"String type"
,我用它来找出应该调用哪个类的方法。 任何人都可以请帮助我了解泛型是否可以用来使代码更清洁。我可以将类类型作为参数传递给方法,并使用该类类型在适当的类中调用parseData
方法。 我试图做 list.add(T.parseData((Element) nl.item(i)));
但得到:
的方法parseData(元)是未定义的类型T
public class Main {
public static void main(String[] args) {
List<EarthQuake> quakes = getFeeds("http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.atom", "quakes");
List<VirginiaWeather> weather = getFeeds("http://alerts.weather.gov/cap/va.php?x=0", "weather");
//Get Earthquake data
System.out.println("Earthquake data");
for (EarthQuake e: quakes)
System.out.printf("%s\t%s\t%f\t%s\n",(e.getDate()),e.getLocation(),e.getMagnitude(),e.getDetails());
//Get Virginia weather data
System.out.println("Virginia Weather");
for (VirginiaWeather vw: weather)
System.out.printf("%s\t%s\t%s\t%s\t%s\n",vw.getUpdated(),vw.getTitle(),vw.getEvent(),vw.getEffective(),vw.getExpires());
}
private static <T> List<T> getFeeds(String url, String type) {
List<T> list = new ArrayList<>();
try {
URL usgsUrl = new URL(url);
URLConnection urlConnection = usgsUrl.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection) urlConnection;
int response = httpConnection.getResponseCode();
if(response == HttpURLConnection.HTTP_OK)
{
InputStream in = httpConnection.getInputStream();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(in);
Element element = document.getDocumentElement();
NodeList nl = element.getElementsByTagName("entry");
if(nl != null && nl.getLength() > 0)
for(int i =0 ; i<nl.getLength() ; i++)
{
if(type.equals("quakes"))
{
list.add((T) EarthQuake.parseData((Element) nl.item(i)));
}
if(type.equals("weather"))
{
list.add((T) VirginiaWeather.parseData((Element) nl.item(i)));
}
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
finally{
}
return list;
}
}
'类型==“地震”'EUH这不是好办法 – 2016-01-21 17:30:10
什么RC是,你应该比较使用'等于()'对象 - 和字符串对象。 – Thomas
Java泛型有点奇怪,因为类型参数实际上并未绑定到类上,这就是为什么您会看到像JAXB这样的东西需要java.lang.Class以及Generic类型。实用的方法是定义一个将文本转换为元素的接口;深奥的是使用反射从类中获取类方法并调用它。 – Mark