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

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

问题描述

假设我有这个代码

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
}

但请记住,这将是一个类,它与您键入的内容不同 HashMap

But keep in mind that this will be a class it won't be the same as you type HashMap<String, String>

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

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