如何在Java中创建一些变量类型别名 [英] How do I create some variable type alias in Java

查看:170
本文介绍了如何在Java中创建一些变量类型别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我说我有这个代码

let say I have this code

Map<String, String> list = new HashMap<String, String>();
list.put("number1", "one");
list.put("number2", "two");

我该如何制作一些别名类型?

how can I make some "alias" the type

Map<String, String>

更容易被重写,像

// may be something like this
theNewType = HashMap<String, String>;

theNewType list = new theNewType();
list.put("number1", "one");
list.put("number2", "two");

基本上我的问题是,如何为某些类型创建别名,以便我可以创建它更容易编写,并且在需要更改整个程序代码时更加轻松。

basically my question is, how to create "alias" to some "type", so i can make it easier to write and easier when need to change the whole program code.

谢谢,并且很抱歉如果这是愚蠢的问题。我在Java中有点新鲜感。

Thanks, and sorry if this is silly question. I'm kinda new in Java.

推荐答案

Java中没有别名。您可以像这样扩展 HashMap 类:

There are no aliases in Java. You can extend the HashMap class with your class like this:

public class TheNewType extends HashMap<String, String> {
    // default constructor
    public TheNewType() {
        super();
    }
    // you need to implement the other constructors if you need
}


$ b,则需要实现其他构造函数$ b

但请记住,这将是一个类,它不会与您键入 HashMap< String,String>

这篇关于如何在Java中创建一些变量类型别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆