对.net 2.0失望啊失望啊失望啊失望啊失望啊
wylcy 2005-11-10 04:45:56 刚听到泛型啊(自以为能用上C++的模版功能了),操作符重载啊,类型转换重载啊,我是那个激动啊兴奋啊,心想.net的语法终于拨云见日可以和c++媲美啦。哪知道 一用才发现~~~~~~
写个类叫TestClass
Public Shared Widening Operator CType(ByVal tc As TestClass) As String
return "爽爽爽"
End Operator
--------------------------------------------------------------------------------------
做个测试
dim s as string
dim tc as new TestClass
s=tc
编译pass,执行pass
爽了一把
--------------------------------------------------------------------------------------
dim s as string
dim obj as object
dim tc as new new TestClass
obj=tc
s=obj
编译通过
执行
Unhandled Exception: System.InvalidCastException: Conversion from type 'TestClas
s' to type 'String' is not valid.
at Microsoft.VisualBasic.CompilerServices.Conversions.ToString(Object Value)
操!!Conversion from type 'TestClass' to type 'String' is not valid
明明可以转换,却说无法从TestClass转为String,睁眼说瞎话啊
--------------------------------------------------------------------------------------
受了一个小打击,算了,去那个被宣传的神乎其神的泛型上找点安慰吧
创建泛型函数
public sub ShowItem(of ItemType)(byval Item as ItemType)
dim s as string
s=item
console.writeline(s)
end sub
---------------------------------------------------------------------------------------
dim tc as new TestClass
ShowItem(tc)
编译:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\a.vb(21) : error BC30311: Value of
type 'ItemType' cannot be converted to 'String'.
s=item
~~~~
晕,第一次玩就受打击,Value of type 'ItemType' cannot be converted to 'String'.看来想把不知类型的东东转化为某个类型是不可能的了,即使你重载了装换。
----------------------------------------------------------------------------------------
还是玩玩操作符重载吧
Public Shared Operator >(byval a as TestClass,byval b as TestClass) as Boolean
return true
end Operator
Public Shared Operator <(byval a as TestClass,byval b as TestClass) as Boolean
return false
end Operator
------------------------------------------------------------------------------------
因为受了刚才的打击,不敢随便玩泛型了
写个简单的
sub Test(byval a as object,b as object)
console.writeline(ctype(a,string))
end sub
dim a as new TestClass
dim b as new TestClass
Test(a,b)
编译pass,运行pass,不容易,那个操作重载的功能,看来还是有用的
--------------------------------------------------------------------------------------
被鼓励了一次,还是不能忘了那个泛型老大啊
public sub Test(of ItemType)(byval a as ItemType,byval b as ItemType)
console.writeline(a>b)
end sub
这种写法充分体现了模版语法的优势,你可以写个容器,里面放各种类型的东西,同时还可以对其进行排序,比较大小的操作…………(题外话能不能少一点啊~~~~)
OKOK,编译!!!!!!!!!!!!!!
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\a.vb(35) : error BC30452: Operator
'>' is not defined for types 'ItemType' and 'ItemType'.
我靠,这都通不过,本人当时顿时傻了5秒钟
Object都可以过,泛型居然就不行,微软该不会这么弱吧~~~
为了证明我的想法,把泛型函数改了一下
public sub Test(of ItemType)(byval a as ItemType,byval b as ItemType)
dim oa ,ob as object
oa=a
ob=b
console.writeline(a>b)
end sub
本函数简直就是弱智写的,不过我就当一次弱智好了
编译pass,执行pass
晕泛型本来就是用来提高效率,避免值类型转换为引用类型的装箱操作,如果要这么写,那还有P个优势啊~~~~~
我就测试了这几项,这个net2.0的框架还是从windows update上更新下来的,我只能说好伤心啊,难道是我期望太高了嘛???~~~~