ios学习笔记2

类的构建

Student.h

#include <Foundation/Foundation.h>
@interface Student:NSObject{
    int age;
}
-(int) age;
-(void) setAge:(int) newAge;
+(id) title;//+为类方法,相当于java中的static,另外字符串返回值使用id,不是NSString
@end //这个必须要,不然会在导入的文件里,提示missing end错误

Student.m

#import "Student.h"
@implementation Student

-(int) age{
    return _age;       
}
-(void) setAge:(int)newAge{
  _age=newAge;  
}

+(id) title{
return @"student";
} @end

使用

Student *student=[[Student alloc] init];
[student setAge:100];
NSLog(@"student age is %i",[student age]);//100
NSLog(@"student age is %@",[Student title]);//student
[student release];//对象使用完毕要释放内存

  

 二:属性

左边的自动声明虽然是存在,但是ios不会让他显示出来。

原文地址:https://www.cnblogs.com/1000pen/p/4490486.html