类方法和实例方法的区别是什么?

实例方法是访问器(getter和setter),而类方法几乎是其他所有东西吗?


当前回答

注意:这只是伪代码格式

类方法

它所需要做的几乎都是在编译时完成的。它不需要任何用户输入,也不需要基于实例进行计算。关于它的一切都是基于类/蓝图-这是独特的,即你没有多个蓝图为一个类。在编译期间可以有不同的变化吗?不,因此类是唯一的,所以无论你调用多少次类方法,指向它的指针都是一样的。

PlanetOfLiving: return @"Earth" // No matter how many times you run this method...nothing changes.

实例方法

相反,实例方法发生在运行时,因为只有在那时,您才创建了某个对象的实例,而该对象可能在每次实例化时都有所不同。

initWithName: @"John" lastName: @"Doe"Age:12 @"cool"
initWithName: @"Donald" lastName: @"Drumpf"Age:5 attitude:@"He started"
initWithName: @"President" lastName: @"Obama"Age:54 attitude: @"Awesome"
//As you can see the value can change for each instance.

如果你来自其他语言静态方法和类方法是一样的。 如果你来自Swift,类型方法和类方法是一样的。

其他回答

类方法

是声明为静态的方法。可以在不创建类实例的情况下调用该方法。类方法只能操作类成员,而不能操作实例成员,因为类方法不知道实例成员。类的实例方法也不能从类方法内部调用,除非在该类的实例上调用它们。

实例方法

另一方面,需要类的实例存在才能调用它们,因此需要使用new关键字创建类的实例。实例方法操作于类的特定实例。实例方法没有被声明为静态的。

在Objective-C中,所有方法都以“-”或“+”字符开头。 例子:

@interface MyClass : NSObject
// instance method
- (void) instanceMethod;

+ (void) classMethod;
@end

“+”和“-”字符分别指定方法是类方法还是实例方法。

如果我们调用这些方法,区别就很明显了。这里的方法是在MyClass中声明的。

实例方法需要类的实例:

MyClass* myClass = [[MyClass alloc] init];
[myClass instanceMethod];

在MyClass内部,其他方法可以使用self调用MyClass的实例方法:

-(void) someMethod
{
    [self instanceMethod];
}

但是,类方法必须在类本身上调用:

[MyClass classMethod];

Or:

MyClass* myClass = [[MyClass alloc] init];
[myClass class] classMethod];

这行不通:

// Error
[myClass classMethod];
// Error
[self classMethod];

注意:这只是伪代码格式

类方法

它所需要做的几乎都是在编译时完成的。它不需要任何用户输入,也不需要基于实例进行计算。关于它的一切都是基于类/蓝图-这是独特的,即你没有多个蓝图为一个类。在编译期间可以有不同的变化吗?不,因此类是唯一的,所以无论你调用多少次类方法,指向它的指针都是一样的。

PlanetOfLiving: return @"Earth" // No matter how many times you run this method...nothing changes.

实例方法

相反,实例方法发生在运行时,因为只有在那时,您才创建了某个对象的实例,而该对象可能在每次实例化时都有所不同。

initWithName: @"John" lastName: @"Doe"Age:12 @"cool"
initWithName: @"Donald" lastName: @"Drumpf"Age:5 attitude:@"He started"
initWithName: @"President" lastName: @"Obama"Age:54 attitude: @"Awesome"
//As you can see the value can change for each instance.

如果你来自其他语言静态方法和类方法是一样的。 如果你来自Swift,类型方法和类方法是一样的。

补充上述答案

Class方法将在Class上工作,我们将它用于一般用途,如+stringWithFormat,类的大小,最重要的是用于init等

NSString *str = [NSString stringWithFormat:@"%.02f%%",someFloat]; 

实例方法对一个类的实例起作用,而不是对一个类起作用,比如我们有两个人,我们想知道每个人的余额,这里我们需要使用实例方法。因为它不会返回一般响应。例如,确定NSSArray的计数等。

[johnson getAccountBalance];
[ankit getAccountBalance];

就像其他答案所说的,实例方法操作对象并可以访问它的实例变量,而类方法操作整个类,并且不能访问特定实例的变量(除非您将实例作为参数传入)。

类方法的一个很好的例子是计数器类型方法,它返回类实例的总数。类方法以“+”开头,而实例方法以“-”开头。 例如:

static int numberOfPeople = 0;

@interface MNPerson : NSObject {
     int age;  //instance variable
}

+ (int)population; //class method. Returns how many people have been made.
- (id)init; //instance. Constructs object, increments numberOfPeople by one.
- (int)age; //instance. returns the person age
@end

@implementation MNPerson
- (id)init{
    if (self = [super init]){
          numberOfPeople++;
          age = 0;
    }    
    return self;
}

+ (int)population{ 
     return numberOfPeople;
}

- (int)age{
     return age;
}

@end

main.m:

MNPerson *micmoo = [[MNPerson alloc] init];
MNPerson *jon = [[MNPerson alloc] init];
NSLog(@"Age: %d",[micmoo age]);
NSLog(@"%Number Of people: %d",[MNPerson population]);

输出: 年龄:0 人数:2人

另一个例子是,如果你有一个方法,你想让用户能够调用,有时把它变成一个类方法是很好的。例如,如果你有一个叫MathFunctions的类,你可以这样做:

+ (int)square:(int)num{ 
      return num * num;
}

然后用户会调用:

[MathFunctions square:34];

而不需要实例化类!

你也可以使用类函数来返回自动释放的对象,比如NSArray

+ (NSArray *)arrayWithObject:(id)object

它接受一个对象,将其放入数组中,并返回数组的自动释放版本,不需要内存管理,这对于临时数组和类似的东西很好。

我希望你现在明白什么时候和/或为什么你应该使用类方法!!