如何确保类型参数是不可变的 [英] How to ensure that type parameters are immutable
本文介绍了如何确保类型参数是不可变的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
class Pair< A,B> {
final A first;
final B sec;
对(A f,B s){
this.first = f;
this.sec = s;
$ b 有没有办法确保A和B是不可变的类型?
解决方案没有。你可以创建一个标记界面(一个没有方法的标记界面,比如> ),可序列化
),用于项目中识别不可变类,并让您的团队坚持它。
例如:
接口不可变{}
类对< A扩展不可变,B扩展不可变> {
}
class Pair<A,B> {
final A first;
final B sec;
Pair(A f, B s){
this.first = f;
this.sec = s;
}
}
Is there a way to ensure that A and B are immutable types ?
解决方案 No. There is no way to identify, let alone bound, a type as being immutable.
You could create a marker interface (one without methods, like Serializable
) that is used in your project to identify immutable classes, and have your team adhere to it.
For example:
interface Immutable {}
class Pair<A extends Immutable, B extends Immutable> {
}
这篇关于如何确保类型参数是不可变的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文