类方法(class method)

转载请注明出处
http://blog.youkuaiyun.com/sdnxr/article/details/43155705

A class method is a method that operates on class objects rather than instances of the class. In Objective-C, a class method is denoted by a plus (+) sign at the beginning of the method declaration and implementation:

类方法操作的是类对象而不是类的实例。在OC中,使用一个放置在方法声明和实现之前的“+”号来标记类方法:
 
 
 
 
+ (void)classMethod;
 
 
 
 

To send a message to a class, you put the name of the class as the receiver in the message expression:

向一个类传递消息,你需要将这个类作为方法的接收者:

 
 
 
 
[MyClass classMethod];
 
 
 
 

Subclasses(子类)

You can send class messages to subclasses of the class that declared the method. For example,  NSArray  declares the class method  array  that returns a new instance of an array object. You can also use the method with  NSMutableArray , which is a subclass of  NSArray :

在子类中,你可以使用父类声明的方法。举个例子,NSArray类声明了类方法array,该方法返回一个新的数组实例对象。你可以在NSArray的子类NSMutableArray中使用该方法:

 
 
 
 
NSMutableArray *aMutableArray = [NSMutableArray array];
 
 
 
 In this case, the new object is an instance of  
 NSMutableArray 
 , not  
 NSArray 
 . 
 

上例中,新生成的对象是NSMutableArray的实例,而不是NSArray类。

Instance Variables(实例变量)/*注意*/

Class methods can’t refer directly to instance variables. For example, given the following class declaration:
类方法不能直接引用实例变量。举个例子,如下类的声明:
@interface MyClass : NSObject {
 
  
     NSString *title;
 
  
}
 
  
+ (void)classMethod;
 
  
@end
you cannot refer to  title  within the implementation of  classMethod .
在classMethod的实现中,你不可以引用title。

self(self指针)/*注意*/

Within the body of a class method,  self  refers to the class object itself. You might implement a factory method like this:
在类方法中,self指向类对象本身。你可以如下实现一个工厂方法:
+ (id)myClass {
 
  
      return [[[self alloc] init] autorelease];
 
  
}
In this method,  self refers to the class to which the message was sent . If you created a subclass of  MyClass :
在这个方法中,self指向消息的接受类。如果你创建了如下MyClass的子类:
@interface MySubClass : MyClass {
 
  
}
 
  
@end
and then sent a  myClass  message to the subclass:
然后向MySubClass发(diao)送(yong)了消(fang)息(fa)myClass:
 
 
id instance = [MySubClass myClass];
 
 at runtime, within the body of the  
 myClass 
  method,  
 self 
  would refer to the  
 MySubClass 
  class (and so the method would return an instance of the subclass). 
 

运行时,在myClass方法中,self指向MySubClass类(该方法返回一个MySubClass的实例)。

转载请注明出处
http://blog.youkuaiyun.com/sdnxr/article/details/43155705
在Python中,类方法Class Method)是一种与类本身相关联的方法,而不是与类的实例相关联的方法。类方法可以通过类名直接调用,也可以通过实例调用。类方法使用装饰器`@classmethod`来定义,并且第一个参数通常是`cls`,表示类本身。 ### 定义和使用类方法 下面是一个简单的例子,展示了如何定义和使用类方法: ```python class MyClass: class_variable = "I am a class variable" def __init__(self, instance_variable): self.instance_variable = instance_variable def instance_method(self): print("This is an instance method.") @classmethod def class_method(cls): print("This is a class method.") print(f"Class variable: {cls.class_variable}") # 通过类名调用类方法 MyClass.class_method() # 通过实例调用类方法 instance = MyClass("I am an instance variable") instance.class_method() ``` 在这个例子中,`class_method`是一个类方法。它可以通过类名`MyClass.class_method()`直接调用,也可以通过实例`instance.class_method()`调用。类方法可以访问类变量,但不能访问实例变量。 ### 使用类方法的优势 1. **访问和修改类变量**:类方法可以访问和修改类变量,而不需要创建类的实例。 2. **工厂方法**:类方法常用于创建类的实例,特别是当创建实例的逻辑比较复杂时。 ### 示例:工厂方法 ```python class Person: def __init__(self, name, age): self.name = name self.age = age @classmethod def from_birth_year(cls, name, birth_year): age = 2023 - birth_year return cls(name, age) person = Person.from_birth_year("Alice", 1990) print(person.name) # 输出: Alice print(person.age) # 输出: 33 ``` 在这个例子中,`from_birth_year`是一个类方法,用于根据出生年份创建`Person`类的实例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值