我有点不清楚的一点是这些NSMutableArray方法之间的区别:
// Class Method Style
NSMutableData *myMutableDataInstance = [NSMutableData dataWithLength:WholeLottaData];
Run Code Online (Sandbox Code Playgroud)
和
// Instance Method Style
NSMutableData *myMutableDataInstance = nil;
myMutableDataInstance = [[[NSMutableData alloc] initWithLength:WholeLottaData]] autorelease];
Run Code Online (Sandbox Code Playgroud)
在引擎盖下,这里的类方法究竟是什么?它与实例方法有何不同?
干杯,道格
我想知道是否存在任何内存/性能缺陷,或者只是缺点,使用类方法,如:
+ (void)myClassMethod:(NSString *)param {
// much to be done...
}
Run Code Online (Sandbox Code Playgroud)
要么
+ (NSArray*)myClassMethod:(NSString *)param {
// much to be done...
return [NSArray autorelease];
}
Run Code Online (Sandbox Code Playgroud)
在类方法中放置很多功能很方便,特别是在我必须处理内存管理(iPhone)的环境中,但是在方便的时候通常会有一个问题?
一个例子可能是思考Web服务,它由许多具有非常简单功能的类组成.即
TomorrowsXMLResults;
TodaysXMLResults;
YesterdaysXMLResults;
MondaysXMLResults;
TuesdaysXMLResults;
.
.
.
n
Run Code Online (Sandbox Code Playgroud)
我在我的Web服务类中收集了大量这些,并且只是实例化Web服务类,并让这个类的方法在"结果"类上调用类方法.这些类很简单,但它们处理大量的Xml,实例化大量的对象等.
我想我问的是,类方法在堆栈和内存中是否存在或被处理的不同于实例化对象的消息?
或者它们是否只是在幕后实例化并再次拉下来,因此,只是一种节省几行代码的方法?
我Customer班上有一个叫做的方法save_from_row().它看起来像这样:
@classmethod
def save_from_row(row):
c = Customer()
c.name = row.value('customer', 'name')
c.customer_number = row.value('customer', 'number')
c.social_security_number = row.value('customer', 'social_security_number')
c.phone = row.value('customer', 'phone')
c.save()
return c
Run Code Online (Sandbox Code Playgroud)
当我尝试运行我的脚本时,我得到了这个:
Traceback (most recent call last):
File "./import.py", line 16, in <module>
Customer.save_from_row(row)
TypeError: save_from_row() takes exactly 1 argument (2 given)
Run Code Online (Sandbox Code Playgroud)
我不明白参数数量的不匹配.这是怎么回事?
我有两个类,Foo1并且Foo2它们不是分层相关的,所以实例Foo1不是实例,Foo2也不是反过来.
在与这些不同的类中,我有一个doStuffWithFoo理想情况下应该采用Foo1或Foo2作为输入的方法,然后相应地表现.这是我追求的基本想法:
private double[] doStuffWithFoo(Fooi foo, Bar bar1, Bar bar2, double fortyTwo) {
Fooi changingFoo = foo;
double[] fooBars = new double[N];
for (int i=0; i<N; ++i) {
double[] array = getArrayFromFoo(foo,bar1,fortyTwo);
double fooBar = getDoubleFromArray(array);
fooBars[i] = fooBar;
foo = foo.getNextFoo();
}
return fooBars;
}
Run Code Online (Sandbox Code Playgroud)
在那里我写了两种截然不同的方法
private double[] getArrayFromFoo(Foo1 foo, Bar bar, double fortyTwo)
private double[] getArrayFromFoo(Foo2 foo, Bar bar, double fortyTwo)
Run Code Online (Sandbox Code Playgroud)
每个Foo类都实现自己的 …
我担心这是一个愚蠢的问题,但我老实说不明白.
我有一个类,RemoteConfiguration目前它的功能是所有实例方法.既然如此,我必须使用它:
RemoteConfiguration* configs = [[RemoteConfiguration alloc] init];
[configs updateSettings];
[configs release];
Run Code Online (Sandbox Code Playgroud)
这很烦人,因为没有必要创建一个对象只是为了使用一个方法,它可以是一个类方法,并调用如下:
[RemoteConfiguration updateSettings];
Run Code Online (Sandbox Code Playgroud)
但是,当我将我的方法从Xcode 更改-为+抱怨我访问的每个对象时self,建议我使用->.(这也会引起警告)我不明白这一点.无论方法如何,对象仍将具有其成员变量.那么为什么有->必要呢?如何在类方法中使用成员变量?
我希望能够使用__delitem__类级变量.我的用例可以在这里找到(使用的答案_reg_funcs),但它基本上涉及一个装饰器类,它保存了它所装饰的所有函数的列表.有没有办法让类对象支持__delitem__?我知道我可以专门为此目的保留一个实例,但我宁愿不必那样做.
class Foo(object):
_instances = {}
def __init__(self, my_str):
n = len(self._instances) + 1
self._instances[my_str] = n
print "Now up to {} instances".format(n)
@classmethod
def __delitem__(cls, my_str):
del cls._instances[my_str]
abcd = Foo('abcd')
defg = Foo('defg')
print "Deleting via instance..."
del abcd['abcd']
print "Done!\n"
print "Deleting via class object..."
del Foo['defg']
print "You'll never get here because of a TypeError: 'type' object does not support item deletion"
Run Code Online (Sandbox Code Playgroud) 我正在写一个Fixnum类方法to_words,它接受任何数字并将其翻译成英语,所以,
2.to_words
#=> "two"
2030.to_words
#=> "two thousand thirty"
Run Code Online (Sandbox Code Playgroud)
我希望它能够处理所有数字,并且一旦我得到一点点超过10亿就会出现问题:
1000002000.to_words
#=> "one billion two thousand"
1074000000.to_words
#=> NoMethodError
1074000000.class
#=> Bignum
Run Code Online (Sandbox Code Playgroud)
有没有办法将我的Fixnum.to_words方法扩展到Bignum?
我正在阅读关于箭头函数(在打字稿上下文中).我遇到了这条线.
每个Handler类型的对象都会创建箭头函数.另一方面,方法只创建一次并附加到Handler的原型.它们在Handler类型的所有对象之间共享.
资料来源:https://www.typescriptlang.org/docs/handbook/functions.html
我无法理解.请回答是否有人可以解释.
我想做以下事情(在 Python 3.7 中):
class Animal:
def __init__(self, name, legs):
self.legs = legs
print(name)
@classmethod
def with_two_legs(cls, name):
# extremely long code to generate name_full from name
name_full = name
return cls(name_full, 2)
class Human(Animal):
def __init__(self):
super().with_two_legs('Human')
john = Human()
Run Code Online (Sandbox Code Playgroud)
基本上,我想__init__用父类的工厂类方法覆盖子类的方法。然而,所写的代码不起作用,并引发:
TypeError: __init__() takes 1 positional argument but 3 were given
Run Code Online (Sandbox Code Playgroud)
我认为这意味着作为变量super().with_two_legs('Human')传递。Humancls
1)为什么这不像写的那样工作?我假设super()会返回超类的代理实例,所以cls是Animal对的吗?
2)即使是这种情况,我也不认为这段代码实现了我想要的,因为 classmethod 返回 的实例Animal,但我只想以Human与 classmethod 相同的方式进行初始化,有什么方法可以实现我的行为想?
我希望这不是一个很明显的问题,我发现文件上super() …
我一直试图了解如何在 Python 中指定类方法的返回类型,以便即使对于子类也能正确解释(例如在我的 Sphinx 文档中)。
假设我有:
class Parent:
@classmethod
def a_class_method(cls) -> 'Parent':
return cls()
class Child(Parent):
pass
Run Code Online (Sandbox Code Playgroud)
a_class_method如果我希望它是Parent为父母和Child孩子的,我应该指定什么作为返回类型?我也试过__qualname__,但这似乎也不起作用。我应该不注释返回类型吗?
提前致谢!
class-method ×10
python ×4
inheritance ×3
class ×2
objective-c ×2
cocoa ×1
constructor ×1
instance ×1
ios ×1
iphone ×1
java ×1
javascript ×1
ruby ×1
typescript ×1
typing ×1