本文介绍了typesafe ints ---如何????的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 嗨 我想(或者至少我想我想)为一个int定义一个包装类 以使其类型化安全。我知道这意味着将一个 值类型转换为引用类型 - 它对性能不敏感。 如何编写一个很好地包装整数的类除了Object之外还有什么东西??例如,我写了: 公共类DBSchemaID { private int schemaID = 0; public N2KDBSchemaID(int schemaIDIn) { schemaID = schemaIDIn; } public int SchemaID { get {return schemaID;} set {schemaID = value;} } } 现在我怎样才能让它在用作数组时充当int? 条目?例如: DBSchemaID schemaID = new DBSchemaID(4); schemaObj = SchemaArray [schemaID]; 是否可以将某种形式的转换转换为int(如ToString())。 也可以覆盖运算符==和> =等等。 可以这样做吗?想要这样做我疯了吗?你怎么可能 确保方法调用的参数是类型安全的。在其他 字样中,如果方法是GetSchemaDBName(int schemaID),则任何int都可以是 交给。如果方法是GetSchemaDBName(DBSchemaID schemaID)那么 我更加严格和安全。想要这样做是否合理 这个? 只要被允许从Int32派生就可以做所有必要的事情。 做这种事的最好办法是什么?任何建议赞赏 - 我是新来的。 干杯 OldNewbieHiI would like (or a least think I would like to) define a wrapper classfor an int so as to make it type safe. I know this means converting avalue type to a reference type - its not that performance sensitive.How do I write a class that wraps the integer nicely and is somethingother than Object?. For example, I have written:public class DBSchemaID{private int schemaID=0;public N2KDBSchemaID(int schemaIDIn){schemaID=schemaIDIn;}public int SchemaID{get{return schemaID;}set{schemaID=value;}}}So now how can I make it act as an int in the when used as an arrayentry? For example:DBSchemaID schemaID=new DBSchemaID(4);schemaObj=SchemaArray[schemaID];Is some sort of implict conversion to int (like ToString()) possible.Also can one override the operators == and >= etc.Can this be done? Am I crazy for wanting to do it? How else could youmake sure that the parameters of a method call are type safe. In otherwords, if a method is GetSchemaDBName(int schemaID) any int could behanded in. If the method is GetSchemaDBName(DBSchemaID schemaID) thenI am being a bit stricter and safer. Is it reasonable to want to dothis?Just being allowed to derive from Int32 would do everything necessary.What the best way of doing this sort of thing? Any advice appreciated- I am new to this.CheersOldNewbie推荐答案 这篇关于typesafe ints ---如何????的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-12 12:26