Student.h
main.m
#import <Foundation/Foundation.h>
@interface Student : NSObject
{
//成员变量 学号
NSUInteger _no;
//年龄
NSUInteger _age;
}
//函数声明
-(void)setNo:(NSUInteger)no;
-(void)setAge:(NSUInteger)age;
//带两个参数的
-(void)setNo:(NSUInteger)no andAge:(NSUInteger)age;
//输出_no 和 _age的值
-(void)print;
@end
Student.m
#import "Student.h"
@implementation Student
//函数声明
-(void)setNo:(NSUInteger)no
{
//给_no成员变量赋值
_no = no;
}
-(void)setAge:(NSUInteger)age
{
//给_age成员变量赋值
_age = age;
}
//带两个参数的
-(void)setNo:(NSUInteger)no andAge:(NSUInteger)age
{
_no = no;
_age = age;
}
-(void)print
{
//成员变量在类内通过成员变量名来访问的
NSLog(@"no = %lu age=%lu",_no,_age);
}
main.m
#import <Foundation/Foundation.h>
//自定义的类使用" "
#import "Student.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
//访问 声明对象方法
//第一步 创建一个对象(类的实例化)
//OC中得对象都是创建在堆空间中得,且都是
//一个匿名对象,所以我们要通过该对象的一个指针来访问它。
//alloc是NSObject中继承过来的,通过类名发送 一条alloc消息用于堆中开辟内存空间,用于存储对象中得
//成员变量。然后再发送一条init消息 用于初始化 刚才通过alloc分配的内存。如果成功了返回这个实例(这个对象在内存中的地址,如果失败返回null)。
Student *cui = [[Student alloc] init];
//方法的调用
//看方法的类型 - 方法对象方法 那么就用对象调用
//[cui setNo:1410001];
/*
[对象名 方法名:实参];
*/
//[cui setAge:23];
[cui setNo:141001 andAge:23];
//输出
[cui print];
}
return 0;
}