property 实例
//创建一个动物model类,用属性设置它的name,age,weight,并设置初始化方法
#import <Foundation/Foundation.h>
@interface Animal : NSObject
#pragma mark 动物名称
@property (nonatomic,readwrite,copy)NSString *aName;
#pragma mark 动物体重
@property (nonatomic,readwrite,assign)CGFloat aWeight;
#pragma mark 动物年龄
@property (nonatomic,readwrite,assign)NSInteger aAge;
/**
* 初始化Animal类信息
*
* @param newName 新名称
* @param newWeight 新体重
* @param newAge 新年龄
*
* @return instance of Animal
*/
- (id)initWithName:(NSString *) newName
Weight:(CGFloat) newWeight
AndAge:(NSInteger) newAge;
@end
#import "Animal.h"
@implementation Animal
- (id)initWithName:(NSString *)newName
Weight:(CGFloat)newWeight
AndAge:(NSInteger)newAge
{
if(self = [super init])
{
self.aName = newName;
self.aWeight = newWeight;
self.aAge = newAge;
}
return self;
}
@end
#import <Foundation/Foundation.h>
#import "Father.h"
#import "Animal.h"
//用[xx setxx:xx] 可读性较高
int main(int argc, const char * argv[]) {
@autoreleasepool {
//创建一个动物model类,用属性设置它的name,age,weight,并设置初始化方法
Animal *animal = [[Animal alloc]initWithName:@"老虎" Weight:280AndAge:180];
NSLog(@"name:%@,weight:%g,age:%ld",[animal aName],[animal aWeight],[animal aAge]);
}
return 0;
}