迷失网络

如果你误读成“迷失公园”或“迷失侏罗纪”,那你可能真的迷失网络了。
随笔 - 88, 评论 - 1932, 引用 - 106

导航

关于

lostinet@lostinet.com这个油箱不能用了。因为空间没了,lostinet.com指向为127.0.0.1 。。。

标签

每月存档

最新留言

广告

 
通常一个类的constructor是protected的时候,是不希望用户直接调用它.
但是我发现可以用这么一个方法来实现调用:
 
关于protected成员的调用,以前有类似的blog的.关于protected的Quiz
protected的成员是不能变相被子类调用的,例如

是不能编译通过的,但constructor却能通过

当然,如果该类是sealed就不行了,那么protected和private也没有区别了.

打印 | 张贴于 2004-12-12 23:50:00 | Tag:DotNet

留言反馈

#re:发现 protected constructor 的无聊的地方. 编辑
发现 protected constructor 的无聊的地方.ooeess
2005-06-08 16:32:00 | [匿名:超声波测距仪]
#re:发现 protected constructor 的无聊的地方. 编辑
^_~,pretty good!csharpsseeoo
2005-05-13 16:16:00 | [匿名:直流电阻测试仪]
#re:发现 protected constructor 的无聊的地方. 编辑
^_~,pretty good!18showsseeoo
2005-04-26 12:44:00 | [匿名:温湿度表]
#re:发现 protected constructor 的无聊的地方. 编辑
^_^,Pretty Good!
2005-04-15 19:25:00 | [匿名:移液系统]
#re:发现 protected constructor 的无聊的地方. 编辑
^_^,Pretty Good!
2005-04-10 19:44:00 | [匿名:物位变送器]
#re: 发现 protected constructor 的无聊的地方. 编辑
lostinet
你好:问你一个问题,问什么你的js加密混淆器,加密后,怎么调用那些代码

帮个忙解决一下这个问题,
2005-01-20 11:37:00 | [匿名:litsnake1]
#re: 发现 protected constructor 的无聊的地方. 编辑
@baryon

这是那段代码在C#2.0中的编译错误
Cannot access protected member 'ClassA.ClassA()' via a qualifier of type 'ClassA'; the qualifier must be of type 'ClassX' (or derived from it)

我们可以很好的理解这样设计的意图,我非常认同这一修改。不过这个修改对于C#来说是"Break"的,即会导致旧的代码无法在C#2.0编译。
2004-12-15 08:59:00 | [匿名:Ninputer]
#re: 发现 protected constructor 的无聊的地方. 编辑
>ClassX必须要继承自ClassA吧
>关于这个,前一段我看到了一篇Blog。说这是C#的Bug,将在2.0中
>修正,但在IL级别这是允许的。VB也有一个有趣的Bug,那就是类
>可以继承自它里面的嵌套类。

本来无端倪,何处惹疑猜
2004-12-14 23:15:00 | [匿名:baryon]
#re: 发现 protected constructor 的无聊的地方. 编辑
我看到的时候是可以的。并且没什么奇怪的。没有理解作者的思路。

比如这样的用法应该很多吧!

class C{
private C(){}
public static C getC(){
reutrn new C();
}
}
2004-12-13 23:55:00 | [匿名:javafaq2004]
#re: 发现 protected constructor 的无聊的地方. 编辑
是啊
2004-12-13 15:20:00 | [匿名:beautyispower]
#re: 发现 protected constructor 的无聊的地方. 编辑
呵呵,我写博主笔误时还没有看到博主修改,好快!
这样的代码自然可以访问,请看 关于protected的Quiz 中,我给的回复,protect 控制的是直接访问和 访问者的类型
举例:你电脑里面秘密只有你或者你的朋友可以看,但如果你的朋友出卖你,那不是你的朋友也能间接知道你的秘密的
2004-12-13 09:23:00 | [匿名:大坏蛋]
#re: 发现 protected constructor 的无聊的地方. 编辑
我想博主笔误
2004-12-13 09:15:00 | [匿名:大坏蛋]
#re: 发现 protected constructor 的无聊的地方. 编辑
不好意思,
发上来的代码写错了.
2004-12-13 08:58:00 | [匿名:Lostinet]
#re: 发现 protected constructor 的无聊的地方. 编辑
ClassX必须要继承自ClassA吧
关于这个,前一段我看到了一篇Blog。说这是C#的Bug,将在2.0中修正,但在IL级别这是允许的。VB也有一个有趣的Bug,那就是类可以继承自它里面的嵌套类。
2004-12-13 08:40:00 | [匿名:Ninputer]
#re: 发现 protected constructor 的无聊的地方. 编辑
which version of .NET? what is the relationship between ClassA and ClassX? if as shown in the above code

in 1.1.4322,

TestCS.cs(12,10): error CS0122: 'ClassA.ClassA()' is inaccessible due to its
protection level

in 2.0.40607,

TestCS.cs(12,10): error CS0122: 'ClassA.ClassA()' is inaccessible due to its
protection level
2004-12-13 03:51:00 | [匿名:saucer]
对不起,目前本随笔不允许发表新评论.

Powered by: Joycode.MVC引擎 0.5.2.0