假设我有这个代码
Map<String, String> list = new HashMap<String, String>();
list.put("number1", "one");
list.put("number2", "two");
我怎样才能制作一些“别名”类型
Map<String, String>
更容易重写的东西,比如
// may be something like this
theNewType = HashMap<String, String>;
theNewType list = new theNewType();
list.put("number1", "one");
list.put("number2", "two");
基本上我的问题是,如何为某种“类型”创建“别名”,这样我就可以使它更容易编写,并且在需要更改整个程序代码时更容易。
谢谢,如果这是个愚蠢的问题,我很抱歉。我对 Java 有点陌生。
Java 中没有别名。您可以延长HashMap
像这样与你的班级一起上课:
public class TheNewType extends HashMap<String, String> {
// default constructor
public TheNewType() {
super();
}
// you need to implement the other constructors if you need
}
但请记住,这将是一个类,它不会与您键入的内容相同HashMap<String, String>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)