C++ 中的 typedef 关键字是否有 Java 等效项或方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1195206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a Java equivalent or methodology for the typedef keyword in C++?
提问by bn.
Coming from a C and C++ background, I found judicious use of typedef
to be incredibly helpful. Do you know of a way to achieve similar functionality in Java, whether that be a Java mechanism, pattern, or some other effective way you have used?
来自 C 和 C++ 背景,我发现明智地使用typedef
是非常有帮助的。您是否知道在 Java 中实现类似功能的方法,无论是 Java 机制、模式还是您使用过的其他一些有效方法?
采纳答案by cletus
Java has primitive types, objects and arrays and that's it. No typedefs.
Java 有原始类型、对象和数组,仅此而已。没有类型定义。
回答by z -
There is no typedef in java as of 1.6, what you can do is make a wrapper class for what you want since you can't subclass final classes (Integer, Double, etc)
从 1.6 开始,java 中没有 typedef,你可以做的是为你想要的东西创建一个包装类,因为你不能子类化最终类(整数、双精度等)
回答by Sam Harwell
Typedef allows items to be implicitly assigned to types they are not. Some people try to get around this with extensions; read here at IBM for an explanation of why this is a bad idea.
Typedef 允许将项隐式分配给它们不是的类型。有些人试图通过扩展来解决这个问题;请在 IBM 此处阅读以了解为什么这是一个坏主意。
Edit: While strong type inference is a useful thing, I don't think (and hope we won't) see typedef
rearing it's ugly head in managed languages (ever?).
编辑:虽然强类型推断是一件有用的事情,但我不认为(并且希望我们不会)看到typedef
它在托管语言中的丑陋(曾经?)。
Edit 2: In C#, you can use a using statement like this at the top of a source file. It's used so you don't have to do the second item shown. The only time you see the name change is when a scope introduces a name collision between two types. The renaming is limited to one file, outside of which every variable/parameter type which used it is known by its full name.
编辑 2:在 C# 中,您可以在源文件的顶部使用这样的 using 语句。使用它,因此您不必执行显示的第二项。您看到名称更改的唯一时间是作用域在两种类型之间引入名称冲突。重命名仅限于一个文件,在该文件之外,使用它的每个变量/参数类型都以其全名知道。
using Path = System.IO.Path;
using System.IO;
回答by Markus Koivisto
There is no need for typedef in Java. Everything is an Object except for the primitives. There are no pointers, only references. The scenarios where you normally would use typedefs are instances in which you create objects instead.
Java 中不需要 typedef。除了基元之外,一切都是对象。没有指针,只有引用。您通常会使用 typedef 的场景是您创建对象的实例。
回答by Steve B.
You could use an Enum, although that's semantically a bit different than a typedef in that it only allows a restricted set of values. Another possible solution is a named wrapper class, e.g.
您可以使用 Enum,尽管它在语义上与 typedef 有点不同,因为它只允许一组受限制的值。另一种可能的解决方案是命名包装类,例如
public class Apple {
public Apple(Integer i){this.i=i; }
}
but that seems way more clunky, especially given that it's not clear from the code that the class has no other function than as an alias.
但这似乎更笨拙,特别是考虑到从代码中不清楚该类除了作为别名之外没有其他功能。
回答by Zed
If this is what you mean, you can simply extend the class you would like to typedef, e.g.:
如果这就是你的意思,你可以简单地扩展你想要 typedef 的类,例如:
public class MyMap extends HashMap<String, String> {}
回答by Zack Yezek
Really, the only use of typedef that carries over to Javaland is aliasing- that is, giving the same class multiple names. That is, you've got a class "A" and you want "B" to refer to the same thing. In C++, you'd be doing "typedef B A;"
实际上,继承到 Javaland 的 typedef 的唯一用途是别名——即给同一个类多个名称。也就是说,您有一个“A”类,而您希望“B”指代相同的事物。在 C++ 中,您将执行“typedef BA;”
Unfortunately, they just don't support it. However, if you control all the types involved you CAN pull a nasty hack at the library level- you either extend B from A or have B implement A.
不幸的是,他们只是不支持它。但是,如果您控制所有涉及的类型,您可以在库级别进行令人讨厌的黑客攻击 - 您可以从 A 扩展 B 或让 B 实现 A。
回答by Yair Zaslavsky
As others have mentioned before,
There is no typedef mechanism in Java.
I also do not support "fake classes" in general, but there should not be a general strict rule of thumb here:
If your code for example uses over and over and over a "generic based type" for example:
正如其他人之前提到的,
Java 中没有 typedef 机制。
我也不支持一般的“假类”,但这里不应该有一般严格的经验法则:
例如,如果您的代码一遍又一遍地使用“基于泛型的类型”,例如:
Map<String, List<Integer>>
You should definitely consider having a subclass for that purpose.
Another approach one can consider, is for example to have in your code a deceleration like:
您绝对应该考虑为此目的创建一个子类。
可以考虑的另一种方法是,例如在您的代码中使用如下减速:
//@Alias Map<String, List<Integer>> NameToNumbers;
And then use in your code NameToNumbers and have a pre compiler task (ANT/Gradle/Maven) to process and generate relevant java code.
I know that to some of the readers of this answer this might sound strange, but this is how many frameworks implemented "annotations" prior to JDK 5, this is what project lombokis doing and other frameworks.
然后在你的代码中使用 NameToNumbers 并有一个预编译器任务(ANT/Gradle/Maven)来处理和生成相关的 java 代码。
我知道对于这个答案的一些读者来说,这可能听起来很奇怪,但这是在 JDK 5 之前实现“注释”的框架有多少,这就是lombok项目和其他框架正在做的事情。
回答by shrewquest
Perhaps this could be another possible replace :
也许这可能是另一种可能的替代品:
@Data
public class MyMap {
@Delegate //lombok
private HashMap<String, String> value;
}
回答by dzs0000
In some cases, a binding annotation may be just what you're looking for:
在某些情况下,绑定注释可能正是您要寻找的:
https://github.com/google/guice/wiki/BindingAnnotations
https://github.com/google/guice/wiki/BindingAnnotations
Or if you don't want to depend on Guice, just a regular annotation might do.
或者,如果您不想依赖 Guice,只需常规注释即可。