//Writing One:@interfaceperson:nsobject{} @property (Nonatomic, strong) NSString*name;@end@implementation Person@end//This applies to the general case, the compiler automatically generates the member variable _name, and the simplest notation, do not have to repeat the declaration. //two, in case of inheritance, expose the parent class member variable to the subclass :@interfaceperson:nsobject{NSString*_name;} @property (nonatomic, strong) NSString*name;@end@implementation Person@synthesizeName =_name;//This line is optional@end
@interfacetest:nsobject{//nsstring *_test; the disadvantage of not writing here is that a subclass inherits Test and will not be able to use the _test property directly } @property (nonatomic,copy) NSString*test;@end@implementationTest//@synthesize test = _test; The system did it for you, so you don't have to write .//@synthesize test = test_; If you think the front is unacceptable and you want to change it to the back, you have to write it .@endso write, the reason1, Concise2, external access is used. Test3, internal access with _test, System automatically generated
Writing one:
@interfacePersonNSObject{}@property(nonatomic,strong)NSString*name;@end@implementationPerson@end
This applies to the general case, the compiler automatically generates the member variable _name, and the simplest notation, do not have to repeat the declaration.
Two, in case of inheritance, expose the parent class member variable to the subclass:
@interface Person : NSObject{NSString *_name;}@property (nonatomic, strong) NSString *name;@end@implementation Person@synthesize name = _name;@end
Refusebt
Links: https://www.zhihu.com/question/22195598/answer/39593235
Source: Know
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.
Only use @property to define a property speed, the subclass cannot be used directly with _speed, it needs to be written in the interface member variable list _speed