受保护的静态成员,抽象类,对象组合与子类 [英] Protected static members, abstract classes, object composition vs. subclassing

查看:76
本文介绍了受保护的静态成员,抽象类,对象组合与子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我最近一直在关注这里讨论的面向对象技术

并且一直在测试它们在网络中使用应用。我想与你的专家讨论

问题。


我想生成可以子类化的Javascript类

,在子类时定义了某些行为。通过原型设计和其他技术有很多方法可以做到这一点,但这些

行为需要是静态的和受保护的。通过保护,这意味着它们可以在子类中被覆盖(或重新分配),并且

特权子类方法可以访问它们,但是
$ b外部的调用者$ b,班级不能。


我一直在尝试定义一个工厂函数,将其

参数传递给匿名对象,同时定义构造函数a

-with-block。 (这就是我松散地称之为子类化

- 它真的是对象组合。)这是有效的,但不适用于

外部 - 定义的功能。他们看不到受保护的静态

成员。


//要传递给工厂函数的辅助函数,保护

静态使用

var ProtStaticMethod1 = function(that){//调用者传递''this''

//调用特权桥函数来访问受保护的静态值

alert(" PSM1:" + that.GetPSField());

};


var ProtStaticMethod2 = function(那个){

alert(" PSM2:" + ProtStaticField);

};


//帮助函数,作为特权类方法访问

var candidateStaticMethod = function(){

alert(" PrivOutsiderSM:" +

(typeof) (ProtStaticField)==undefined?

局外人静态方法找不到ProtStaticField:ProtStaticField)

);

};


//工厂功能;参数:1)函数,2)任何数据

var BaseFactory = function(psmMethod,psmField,privilegedMeth){

with({

ProtStaticField :psmField,

ProtStaticMethod:psmMethod

})

{

函数构造函数(){//实例成员只是为了好玩

var privInstMember =" privateMember";

function privInstMethod(){alert(privInstMember); };

};

Constructor.prototype.ShowPSField = function(){

alert(" ShowPSField:" + ProtStaticField); }

Constructor.prototype.InvokePSMethod = function(){

ProtStaticMethod(this); }

Constructor.prototype.SetPSField = function(val){

ProtStaticField = val; }

Constructor.prototype.GetPSField = function(){return

ProtStaticField; }

Constructor.PrivOutsideMethod = privilegedMeth;

Constructor.PrivInsideMethod = function(){

alert(" PrivInsiderSM:" +

(typeof(ProtStaticField)==" undefined"?

无法找到ProtStaticField":ProtStaticField));

};

};

返回构造函数;

};

SubClass1 = BaseFactory(ProtStaticMethod1," ProtStaticField_Class1",

candidateStaticMethod);

//一个函数定义了构造函数的范围,看看特权

静态字段?

SubClass1.PrivOutsideMethod();


SubClass2 = BaseFactory(ProtStaticMethod2," ProtStaticField_Class2",

candidateStaticMethod);

//特权静态构造函数方法可以看到protected static

字段吗?

SubClass1.PrivInsideMethod();


obj1 = new SubClass1();

obj2 = new SubClas s2();

obj1.ShowPSField(); //应显示" ProtStaticField_Class1"

obj2.ShowPSField(); //应显示" ProtStaticField_Class2"

obj1.InvokePSMethod(); //应显示" ProtStaticField_Class1"

obj2.InvokePSMethod(); // CRASH - ProtStaticMethod2看不到

ProtStaticField

这个用于生成子类,其受保护的静态字段

由几乎组成任何价值观。


但是,我的主要抱怨是,在工厂的带块的范围之外定义的功能是不能的看到受保护的静态

成员。这仅仅是这种技术的局限,还是我需要改变什么?b $ b?唯一可行的是,

,你可以看到,将特权桥函数添加到构造函数的原型中./ b $ b原型。这些作为存储的外部定义的

函数的中间人,例如, GetPSField由外部函数访问

ProtStaticMethod1。


是否有更好的方法来生成子类其受保护的

静态方法可以从外部变化,但仍然可以访问受保护的

成员?


提前感谢任何反馈,


Kevin Prichard

Hi all,

I''ve recently been following the object-oriented techiques discussed
here and have been testing them for use in a web application. There is
problem that I''d like to discuss with you experts.

I would like to produce Javascript classes that can be "subclassed"
with certain behaviors defined at subclass time. There are plenty of
ways to do this through prototyping and other techniques, but these
behaviors need to be static and protected. By protected this means
that they can be overridden (or reassigned) in subclasses, and
privileged subclass methods can access them, but callers external to
the class cannot.

I''ve been attempting to define a factory function that transfers its
parameters to an anonymous object while defining the constructor in a
-with- block. (This is what I''m loosely referring to as "subclassing"
- it''s really object composition.) This works, but not for
externally-defined functions. They can''t see the protected static
members.

// helper functions to be passed into factory function, for protected
static use
var ProtStaticMethod1 = function(that) { // caller passes in ''this''
// call privileged bridge function to access protected static value
alert("PSM1: "+that.GetPSField());
};

var ProtStaticMethod2 = function(that) {
alert("PSM2: "+ProtStaticField);
};

// helper function, to be accessed as a privileged class method
var candidateStaticMethod = function() {
alert("PrivOutsiderSM: "+
(typeof(ProtStaticField)=="undefined" ?
"Outsider static method can''t find ProtStaticField" : ProtStaticField)
);
};

// factory function; params: 1) function, 2) any data
var BaseFactory = function(psmMethod,psmField,privilegedMeth) {
with ({
ProtStaticField : psmField,
ProtStaticMethod : psmMethod
})
{
function Constructor() { // instance members just for fun
var privInstMember = "privateMember";
function privInstMethod() { alert(privInstMember); };
};
Constructor.prototype.ShowPSField = function() {
alert("ShowPSField: "+ProtStaticField); }
Constructor.prototype.InvokePSMethod = function() {
ProtStaticMethod(this); }
Constructor.prototype.SetPSField = function(val) {
ProtStaticField=val; }
Constructor.prototype.GetPSField = function() { return
ProtStaticField; }
Constructor.PrivOutsideMethod = privilegedMeth;
Constructor.PrivInsideMethod = function() {
alert("PrivInsiderSM: "+
(typeof(ProtStaticField)=="undefined" ?
"Can''t find ProtStaticField" : ProtStaticField) );
};
};
return Constructor;
};
SubClass1 = BaseFactory( ProtStaticMethod1, "ProtStaticField_Class1",
candidateStaticMethod );
// can a function defined outide the constructor''s scope see privileged
static fields?
SubClass1.PrivOutsideMethod();

SubClass2 = BaseFactory( ProtStaticMethod2, "ProtStaticField_Class2",
candidateStaticMethod );
// can a privileged static constructor method see protected static
fields?
SubClass1.PrivInsideMethod();

obj1 = new SubClass1();
obj2 = new SubClass2();
obj1.ShowPSField(); // should display "ProtStaticField_Class1"
obj2.ShowPSField(); // should display "ProtStaticField_Class2"
obj1.InvokePSMethod(); // should display "ProtStaticField_Class1"
obj2.InvokePSMethod(); // CRASH - ProtStaticMethod2 can''t see
ProtStaticField
This has worked for producing subclasses whose protected static fields
are composed of just about any values.

BUT, my chief complaint is that functions defined outside the scope of
the factory''s -with- block aren''t able to see the protected static
members. Is that just a limitation of this technique, or is there
something I need to change? The only thing that has worked at all is,
as you can see, to add privileged bridge functions to the constructor''s
prototype. These act as a go-between for the externally defined
functions stored, e.g. GetPSField as accessed by external function
ProtStaticMethod1.

Is there a better approach for producing "subclasses" whose protected
static methods can be varied from outside, yet still access protected
members?

Thanks in advance for any feedback,

Kevin Prichard

推荐答案



Kevin Prichard写道:

Kevin Prichard wrote:
大家好,

我最近一直在关注这里讨论的面向对象技术,并且已经测试它们在Web应用程序中的使用。有一个问题,我想和你的专家讨论。
Hi all,

I''ve recently been following the object-oriented techiques discussed
here and have been testing them for use in a web application. There is
problem that I''d like to discuss with you experts.




< snip>

$ b $哇 - 你真的试图从JavaScript对象模型中获得100%:-)


您可能还想查看我的游戏记录:


< html>

< head>

< title> TMP< / title>

< meta http-equiv =" Content-Type"

content =" text / html; charset = iso-8859-1">

< script type =" text / javascript">


/ **

* myClassSuper用作myClass的超类

* /

函数myClassSuper(){

//添加受保护的成员< ; messenger>

//来自myClassUtils类:

this.messenger = function(message){

this.utils.messenger(message) ;

}

}


/ **

*受保护的类myClassUtils

*只能在

* myClass中使用:

* /

函数myClassUtils(obj){


if(myClassUtils.caller!== myClass){

抛出新错误(退出我受保护的东西!);

}


this.messenger = function(message){

// JavaScript的感觉很弱

//对象中的上下文。我会打电话给

//失禁< this> :-)

//任何方式:

// alert(arguments.callee.caller);

//为您提供公共方法of myClassSuper

// - 你可能会以某种方式检查。

alert(''myClassUtils.messenger说:''+消息);

} $ / $

/ **

* myClass构造函数:

* /

函数myClass(){

myClassSuper.call(this); //想想super()

this.utils = new myClassUtils(this);

}


function init(){


尝试{

var goodBoy = new myClass();

goodBoy.messenger(''嗨!'');

}

catch(e){

alert(e.message);

}


尝试{

var badBoy = new myClassUtils();

badBoy.messenger(''嗨!'');

}

catch(e){

alert(e.message);

}

}

< / script>

< / head>


< body onload =" init(); ">


< / body>

< / html>


注意[1]

我正在使用< class>术语作为便利术语。您可以免费致电

it< prototype>或者<构造函数持有者>或者< foobar>或者任何其他名称。

;-)


注意[2]

JavaScript和Java是不同的语言,但它们有一些事情

的共同点。在JavaScript的特定解释性质和

字节编码(读取:解释)Java的本质使它们都打开

源语言。

这意味着其中的受保护/私人/静态/最终的东西不是一个防弹锁,而只是一个不方便保护。 (比如在页面上锁定

上下文菜单)。你总是可以研究代码和/或恶搞

构造函数。在JavaScript中它比在Java中更容易 - 这只是

唯一的区别。

所以如果你想满足你的老板,请告诉他如何调用

受保护会员导致例外。他应该开心。 :-)

你个人应该记住它是一个虚构的

保护,你绝不应该暴露任何敏感数据

公众或受保护/私人成员。


我个人最后将所有受保护命名为和私人

成员



<snip>

Wow - you really tried to get 100% out of JavaScript object model :-)

You may also want to look at my game records:

<html>
<head>
<title>TMP</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<script type="text/javascript">

/**
* myClassSuper is used as super class of myClass
*/
function myClassSuper() {
// Adds protected member <messenger>
// from myClassUtils class:
this.messenger = function(message) {
this.utils.messenger(message);
}
}

/**
* Protected class myClassUtils
* can be used only from within
* myClass:
*/
function myClassUtils(obj) {

if (myClassUtils.caller !== myClass) {
throw new Error("Get out of my protected stuff!");
}

this.messenger = function(message) {
// JavaScript has a very weak feeling
// of the context in objects. I would call
// it "incontinence of <this>" :-)
// Any way:
// alert(arguments.callee.caller);
// gives you the public method of myClassSuper
// - you may check against somehow.
alert(''myClassUtils.messenger says: ''+message);
}

}
/**
* myClass constructor:
*/
function myClass() {
myClassSuper.call(this); // think super()
this.utils = new myClassUtils(this);
}

function init() {

try {
var goodBoy = new myClass();
goodBoy.messenger(''Hi!'');
}
catch(e) {
alert(e.message);
}

try {
var badBoy = new myClassUtils();
badBoy.messenger(''Hi!'');
}
catch(e) {
alert(e.message);
}
}
</script>
</head>

<body onload="init();">

</body>
</html>

Note [1]
I''m using the <class> term as a convenience term. You are free to call
it <prototype> or <constructor holder> or <foobar> or any other name.
;-)

Note [2]
JavaScript and Java are different languages but they have some things
in common. In the particular interpreted nature of JavaScript and
byte-coded (read: "interpreted") nature of Java makes them both open
source languages.
It means that protected/private/static/final stuff in them is not a
bulletproof lock but merely an "inconvenience protection" (like locking
context menu on the page). You always can study the code and/or spoof
the constructor. In JavaScript it''s easier than in Java - that''s the
only difference.
So if you want to satisfy your boss, show him how the call to
"protected" member leads to an exception. He should get happy. :-)
And you personally should remember that it''s all an imaginary
protection and you should never expose any sensitive data neither in
public nor in protected/private members.

I personally just ended up by naming all "protected" and "private"
members with


_ + name。我在评论中说,如果有东西开始

_ + name. And I said in comments that if something starts
with


_那么你不应该直接触摸它或者物体可能会崩溃 -

从这一点开始我只是洗了手。这种方法可能是b
可丢弃,但它可以节省大量的时间和空间

{pseudo {clo} sure {protection}}无论如何都不保证任何东西。

_ then you should not touch it directly or the object may crash -
and from this point out I simply washed my hands. This approach may be
discutable but it saves hell a lot of time and space on
{pseudo {clo}sure {protection}} which doesn''t guarantee anything anyway.


这篇关于受保护的静态成员,抽象类,对象组合与子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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