[解决]你有一个具有泛型属性的泛型类 [英] [Solved] can you have a generic class with generic properties

查看:126
本文介绍了[解决]你有一个具有泛型属性的泛型类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Vb.net的新手,想知道你是否可以制作一个通用课程。我在这里阅读了通用类文章,但它们并不是我想要的。我正在寻找与课程一起使用的通用属性



现在我有一个名为Item(或_item)的属性。我需要的是泛型类的两个属性示例以及该类的至少一个方法。



最后......我是一个老家伙,所以这不是一个学校项目。这只是为了帮助我学习OOP。



所以提前感谢您的帮助



FOUND我的答案。试图删除但不能

感谢寻找



我尝试了什么:



询问其他人和其他论坛。

解决方案

是的,您可以:如何在VB.NET中创建通用属性? - 堆栈溢出 [ ^ ]


您可以通过在类声明后添加(Of TBase)将类声明为动态(您可以使用任何名称而不是TBase - 这是我平常的事情)



例如: -



 公开 声明 少数(< span class =code-keyword>  TBase)

结束





现在 - 如果你想拥有一个类型来自该泛型的属性,你可以使用TBase作为其类型来声明它例如



 公开 声明 少数( of  TBase)

公共 属性 Thumb()作为 TBase

结束 Class





现在,您可以通过指定要使用的特定类型来声明泛型类的实例它将强制执行您期望的类型安全编码。 e,g,



  Dim  MyVar 作为 少数(  整数

MyVar.Thumb = 1

Dim MyOtherVar As 少数( 字符串

MyOtherVar.Thumb = First Digit


请参阅我对该问题的评论。



从形式上讲,没有通用属性;我看不出他们有什么意义。我不确定你是不是混淆了两个不同的东西,比如泛型属性和泛型参数类型的属性。泛型参数类型的属性非常有意义:

  class  MyClass< T> {
内部 T MyProperty { get ; set ; }
内部 静态 void DoSomething< T2>(T2 myParameter){ / * ... * / }
< span class =code-comment> // 注意:T2与泛型类参数T
}
无关
// ...

MyClass< string> myInstance = new MyClass< string>();
myInstance.MyProperty = some string;
myInstance.DoSomething< int>( 42 );

MyClass< int> myOtherInstance = new MyClass< int>();
myOtherInstance.MyProperty = 13 ;



属性与函数的语义不同,是通用的。为什么你认为我做了一个示例泛型函数static?是的,它可能是一个实例函数,但它会做什么?如果它对类实例做了一些事情会有一些意义,但问题是:类实例没有T2的知识。 T和T2是无关的。







为什么不是通用属性?因为有关于通用参数的注意事项。什么是T型的财产?这本质上是一些函数,getter返回T,setter接受T,加上例如属性this,即对实例的引用。没有空间可以使用任何其他类型。



[结束编辑]



如果这个解释不清楚你的主题,你必须在那些不存在的通用属性背后解释你的想法。



-SA

I'm new to Vb.net and would like to know if you can make a generic class. I've read the generic class articles here, but they are not exactly what I want. I'm looking for generic properties to go along with the class

Right now I have one property called "Item" (or _item). What I need is two examples of properties for the generic class and at least one method of the class.

And for the last... I'm an old guy, so this is not a school project. It's only for helping me to learn OOP.

So thank you in advance for any help

FOUND MY ANSWER. TRIED TO DELETE BUT CAN'T
THANKS FOR LOOKING

What I have tried:

Asking other people and other forums.

解决方案

Yes, you can: How do I create a generic property in VB.NET? - Stack Overflow[^]


You can declare a class as being dynamic by adding (Of TBase) after the class declaration (You can use any name instead of TBase - that is just my usual)

for example:-

Public Declare Class Handful(Of TBase)

End Class



Now - if you want to have a property whose type comes from that generic you declare it using TBase as its type e.g.

Public Declare Class Handful(Of TBase)

   Public Property Thumb() As TBase 

End Class



Now you can declare instances of the generic class by specifying the specific type to use and it will enforce the type-safe coding you would expect. e,g,

Dim MyVar As New Handful(Of Integer)

MyVar.Thumb = 1

Dim MyOtherVar As Handful(Of String)

MyOtherVar.Thumb = "First Digit"


Please see my comment to the question.

Formally speaking, there are no generic properties; and I don't see how they can make any sense. I'm not sure that you are not confusing two different things, such as generic properties and properties of generic-parameter type. The properties of generic-parameter type makes perfect sense:

class MyClass<T> {
   internal T MyProperty { get; set; }
   internal static void DoSomething<T2>(T2 myParameter) { /* ... */ }
   // note: T2 has nothing to do with generic class parameter T
}

//...

MyClass<string> myInstance = new MyClass<string>();
myInstance.MyProperty = "some string";
myInstance.DoSomething<int>(42);

MyClass<int> myOtherInstance = new MyClass<int>();
myOtherInstance.MyProperty = 13;


Property does not have the same semantics as function, to be generic. Why do you think I made an example generic function static? Yes, it could be an instance function, but what would it do? It would make some sense if it did something to the class instance, but the trouble is: the class instance has no "knowledge" of T2. T and T2 are unrelated.

[EDIT]

Why not generic properties? Because there is noting to do with generic parameters. What is property of type T? This is, essentially, a couple of function, getter returning T, and setter accepting T, plus, for instance properties, "this", a reference to the instance. There is no a room to use any other type.

[END EDIT]

If this explanation does not clear the subject for you, you have to explain your idea behind those non-existing "generic properties".

—SA


这篇关于[解决]你有一个具有泛型属性的泛型类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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