【发布时间】:2014-05-05 10:16:24
【问题描述】:
哇,我自己发现了一个好问题。
这是什么?糖果还是大蒜?
关于 Objective-C 的一些事情:
在 (+) - 类方法中不使用“self”作为类有什么问题吗?
在一堂课的深处……
+(NSDate*)dateWithTimeInterval:(NSTimeInterval)interval {
return [self dateWithTimeIntervalSince1970:interval];
}
这里是鲁比: 例如,在 Ruby 中,一切都是对象,类是类的对象,并且有一个依赖自我的好习惯:
class DateClass
# self is DateClass here, inside of class definition, uh
self.dateWithTimeInterval(interval)
self.dateWithTimeIntervalSince1970(interval)
end
end
这里是 Perl: 另一个例子是在perl oop deep中找到的:(感谢this thread)
sub new {
my $proto = shift || die "Must pass a class or object into new()";
my $class = ref($proto) || $proto;
bless {}, $class;
}
所以,在 Perl 和 Ruby 中,人们总是依赖 $class refs
也许 Perl 代码的例子并不明显,但它总是会发生。程序员依赖 $class 引用并使用它的类名。此外,他们可以使用它调用一些方法:
my $class = 'Class';
$class->new();
或
Class::->new()
毕竟...
对于在 Objective-c 中使用 self 作为类,您可以提供哪些陷阱或警告?
【问题讨论】:
-
你是在问
[self dateWithTimeIntervalSince1970:interval];VS 的优缺点吗?[NSDate dateWithTimeIntervalSince1970:interval];? -
@Visput,是的,关于它
标签: objective-c