iPhone 开发教程(斯坦福大学)

iPhone 开发教程(斯坦福大学)

5 (4人评价)
  • 课时:(28)

  • 学员:(670)

  • 浏览:(29673)

  • 加入课程

如何创建你自己的定制类的笔记

相关课时: 笔记详情:

@interface Person : NSObject

{

    NSString *name;

    int age;

}

- (NSString *)name; //get method

- (void)setName:(NSString *)value;

- (int) age; //get method;

- (void)setAge:(int)age;

- (BOOL)canLegallyVote;

//another way to creat property

//property declarations

@property int age;

@property (copy) NSString *name;

@property (readonly) BOOL canLegallyVote;

 

- (void)castBallot;

@end

 

// implementation file

@implementation Person;

- (int) age {return age;}

- (void)setAge:(int) value

{

    age=value; 

}

 

- (NSString *) name {return name;}

- (void)setName:(NSString *) value

{

    name=value;

}

 

//calling method

- (BOOL)canLegallyVote {

     return (self age>=18);

}

//antoher way to write it

@synthesize age;

@synthesize name;

- (BOOL)canLegallyVote{

    return(age >18);

 

 

- (void)castBallot {

   if([self canLegallyVote]){

     //do voting stuff

}else {

    NSLog(@"I'm not allowed to vote yet!");

}

}

}   

@end

 

// Autoreleasing example

- (NSString *)fullName{

      NSString *result;

      result=[[NSString alloc] initWithFormat:"%@ %@" , firstName, lastName];

      [result autorelease]; 

      return result;

 

 

0 0

你感兴趣的课程

移动开发 IOS开发入门教程
17万+浏览/ 7251学员/ 4.1评分
免费
7万+浏览/ 804学员/ 4.8评分
免费
6万+浏览/ 241学员/ 4评分
免费