类的构建
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]);//100NSLog(@"student age is %@",[Student title]);//student
[student release];//对象使用完毕要释放内存
时间: 2024-10-16 07:00:19