我正在创建一个超过NSDate的类别.它有一些实用方法,不应该是公共接口的一部分.
我怎样才能让它们变成私密的?
在类中创建私有方法时,我倾向于使用"匿名类别"技巧:
@interface Foo()
@property(readwrite, copy) NSString *bar;
- (void) superSecretInternalSaucing;
@end
@implementation Foo
@synthesize bar;
.... must implement the two methods or compiler will warn ....
@end
Run Code Online (Sandbox Code Playgroud)
但它似乎不适用于另一个类别:
@interface NSDate_Comparing() // This won't work at all
@end
@implementation NSDate (NSDate_Comparing)
@end
Run Code Online (Sandbox Code Playgroud)
在类别中使用私有方法的最佳方法是什么?
这是C++类实现中一次又一次出现的问题.我很好奇人们的想法在这里.您更喜欢哪种代码?为什么?
class A
{
public:
/* Constructors, Destructors, Public interface functions, etc. */
void publicCall(void);
private:
void f(void);
CMyClass m_Member1;
};
Run Code Online (Sandbox Code Playgroud)
同
void A::publicCall(void)
{
f();
}
void A::f(void)
{
// do some stuff populating m_Member1
}
Run Code Online (Sandbox Code Playgroud)
或替代方案:
class A
{
public:
/* Constructors, Destructors, Public interface functions, etc. */
void publicCall(void);
private:
void f(CMyClass &x);
CMyClass m_Member1;
};
Run Code Online (Sandbox Code Playgroud)
同
void A::publicCall(void)
{
f(m_Member1);
}
void A::f(CMyClass &x)
{
// do some stuff to populate x,
// locally masking the …
Run Code Online (Sandbox Code Playgroud) 我正在为面向对象的设计课做一个家庭作业,而且我在使用Scala的伴侣对象时遇到了麻烦.我在一些地方读过,伴侣对象应该可以访问他们的伴侣类的私有方法,但我似乎无法让它工作.(正如一个注释,作业的内容与实现二叉搜索树有关,所以我不只是要求答案......)
我有一个对象应该创建我的私有类的实例,BstAtlas(Bst也在Atlas对象中定义,为了清楚起见将其取出):
object Atlas {
def focusRoom(newRoom:Room,a:Atlas):Atlas = a.helpFocusRoom(newRoom);
abstract class Atlas {
...
protected def helpFocusRoom(n:Room):Atlas;
...
}
private class BstAtlas(bst:Bst) extends Atlas {
...
protected def helpFocusRoom(newRoom:Room):Atlas = ...
// uses some of bst's methods
...
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我去编译时,我收到以下错误:
Question23.scala:15:错误:无法在Atlas.Atlas a.helpFocusRoom(newRoom)中访问方法helpFocusRoom;
函数helpFocusRoom需要隐藏,但我不知道如何隐藏它并仍然可以在伴随对象内访问它.
谁能告诉我这里我做错了什么?
那么,首先要问一下 - 请保持温和.
我正在与一些来自各种非Web编程背景的其他开发人员合作开展一个相当重的JavaScript项目,我们决定尝试在我们的JavaScript伪类中使用公共和私有方法和属性,纯粹是一种良好的编码实践(即.我们知道它没有实际的优势或安全性)
我们玩弄了几种不同的公共和私有方式(即使用本地范围的变量和函数,使用特权方法进行公共消费),我们目前已经决定让我们的JavaScript类构造函数实际返回一个只代表它们的对象公共界面,有效隐藏其他一切.
这是一个例子:
function MyObject()
{
var _this = this;
this._privateProperty = 'somevalue';
this._privateMethod = function()
{
// Do Something
}
this.public =
{
publicProperty : _this._privateProperty,
publicMethod : function(){ return _this.privateMethod() }
}
return this.public;
}
Run Code Online (Sandbox Code Playgroud)
在Chrome中实例化并登录时:
var obj = new MyObject();
console.log(obj);
Run Code Online (Sandbox Code Playgroud)
输出:
> Object
> publicMethod: function (){ return _this.privateMethod() }
> publicProperty: "somevalue"
>__proto__: Object
Run Code Online (Sandbox Code Playgroud)
现在回答我的问题:因为将构造函数中的公共接口作为新对象返回,所以当你在console.log中时,你会注意到它将自己标识为> Object
- 而如果我们不返回该公共接口则将其标识为> MyObject
.
理想情况下,我们希望将后者显示用于调试目的,并且我知道如何访问contstructor的"MyObject"名称_this.constructor.name
,但不知道如何设置它以便以这种方式识别它.
有谁知道如何手动设置这个?
注意:
我知道这在某些方面是JavaScript惯例的混合,并试图在圆孔中安装方形挂钩,但我们发现它是一种非常明显和可读的方式来完成我们想要做的事情.我愿意接受有关如何使用不同设计实现此目的的建议,但我最终会寻找适合我们当前设计的答案.
使用块在方法中定义私有方法而不是使用真正的私有方法有什么缺点?除了无法从其他地方调用该方法之外还有什么吗?
例:
-(NSDictionary*)serialize
{
NSMutableDictionary* serialization = [NSMutableDictionary dictionary];
TwoArgumentsBlockType serializeItemBlock = ^void(MyItemClass* item, NSString* identifier)
{
if (item)
{
// serialization code
}
};
serializeItemBlock(self.someItem1, kSomeIdentifier1);
serializeItemBlock(self.someItem2, kSomeIdentifier2);
serializeItemBlock(self.someItem3, kSomeIdentifier3);
serializeItemBlock(self.someItem4, kSomeIdentifier4);
serializeItemBlock(self.someItem5, kSomeIdentifier5);
serializeItemBlock(self.someItem6, kSomeIdentifier6);
serializeItemBlock(self.someItem7, kSomeIdentifier7);
serializeItemBlock(self.someItem8, kSomeIdentifier8);
serializeItemBlock(self.someItem9, kSomeIdentifier9);
serializeItemBlock(self.someItem10, kSomeIdentifier10);
serializeItemBlock(self.someItem11, kSomeIdentifier11);
return serialization;
}
Run Code Online (Sandbox Code Playgroud) 从这个问题的讨论如何在C++中实现私有变量的访问?我提出了一个变体:可以通过强制转换并依赖布局兼容性来调用私有成员函数,而不是访问私有数据成员吗?
一些代码(灵感来自Herb Sutter的列使用和滥用访问权限)
#include <iostream>
class X
{
public:
X() : private_(1) { /*...*/ }
private:
int Value() { return private_; }
int private_;
};
// Nasty attempt to simulate the object layout
// (cross your fingers and toes).
//
class BaitAndSwitch
// hopefully has the same data layout as X
{ // so we can pass him off as one
public:
int Value() { return private_; }
private:
int private_;
};
int f( X& x …
Run Code Online (Sandbox Code Playgroud) 我对这种行为感到有点困惑(使用python 3.2):
class Bar:
pass
bar = Bar()
bar.__cache = None
print(vars(bar)) # {'__cache': None}
class Foo:
def __init__(self):
self.__cache = None
foo = Foo()
print(vars(foo)) # {'_Foo__cache': None}
Run Code Online (Sandbox Code Playgroud)
我已经阅读了一些关于双下划线如何导致属性名称被"损坏"的内容,但在上述两种情况下我都希望使用相同的名称.
有什么想法在这里发生了什么?
python attributes double-underscore private-methods python-3.x
如何在 Scala 中使用 privateMethodTester 测试采用泛型类型的私有方法?
假设我有以下方法:
private def parseValueForJsonKeyWithReturnType[A: TypeTag](
node: JsonNode,
key: String,
defaultValue: Option[A] = None): A = {
val parsedValue = Option(node.get(key)).map(value => {
typeOf[A] match {
case t if t =:= typeOf[String] =>
value.textValue()
case t if t =:= typeOf[Double] =>
value.asDouble()
case t if t =:= typeOf[Long] =>
value.asLong()
case _ => throw new RuntimeException(s"Doesn't support conversion to [type=${typeOf[A]}] for [key=${key}]")
}
})
parsedValue.getOrElse(defaultValue.get).asInstanceOf[A]
}
Run Code Online (Sandbox Code Playgroud)
我可以像这样调用方法
parseValueForJsonKeyWithReturnType[Boolean](jsonNode, key="hours")
parseValueForJsonKeyWithReturnType[String](jsonNode, key="hours")
parseValueForJsonKeyWithReturnType[Long](jsonNode, key="hours")
Run Code Online (Sandbox Code Playgroud)
在测试中,我正在尝试做
val parseValueForJsonKeyWithReturnTypeInt …
Run Code Online (Sandbox Code Playgroud) 我正在尝试为一个类定义一个私有方法来测试不能从类外部调用这样的方法。但是,即使我使用规范中指示的语法,我也会遇到错误。我还检查了 MDN。
这是我班级的代码:
class CoffeeMachine {
#waterLimit = 200;
#checkWater(value) {
if (value < 0) throw new Error("Negative water");
if (value > this.#waterLimit) throw new Error("Too much water");
}
}
const coffeeMachine = new CoffeeMachine;
coffeeMachine.#checkWater();
Run Code Online (Sandbox Code Playgroud)
在调用 时coffeeMachine.#checkWater();
,我应该得到一个错误,表明不能从类外部调用这样的方法,但相反,我得到了Uncaught SyntaxError: Unexpected token '('
.
这可能是什么原因?
我工作的公司即将完成仅供员工内部使用的应用程序。根据我的研究,有些事情我不清楚,并且希望听到使用此方法部署应用程序的人们的意见。
对于三个商店(App store(testflight 除外)Google play 和 ap pgallery),最适合此问题的解决方案是什么?
如果我们只为员工私下部署一个应用程序,它还需要提交到 Apple/Google Play 或 Appgallery 吗?
还有其他要求吗?
如果过度发布到商店有什么限制?
每个更新版本都需要重新分发应用程序吗?
private-methods appstore-approval in-house-distribution google-play appgallery
private-methods ×10
c++ ×2
javascript ×2
scala ×2
appgallery ×1
attributes ×1
c++11 ×1
casting ×1
cocoa ×1
console ×1
constructor ×1
function ×1
google-play ×1
ios ×1
objective-c ×1
oop ×1
python ×1
python-3.x ×1
scalatest ×1