IOS OC语言基础(一)类的创建

一.创建类

创建oc对象时候,需要进行声明 实现 调用 三个步骤

类的声明

@interface Time : NSObject

{
    
    int hour;
//    int minute;
    int second;
}
@property int minute;
-(int)hour;
-(void)setHour:(int)h;
-(void)show;

@end



类的实现

@implementation Time
//@synthesize minute;
-(int)hour{
    return hour;
    
}
-(void)setHour:(int)h
{
    hour=h;
}
-(void)show{
    NSLog(@"minute%d",_minute);
    
    
}

@end

类的调用

int main(){
    Time* tm1=[[Time alloc]init];
    /*tm1->hour=14;
    tm1->minute=14;
    tm1->second=19;
    [tm1 show];*/
    [tm1 sethour:14 andMinute:15 andSecond:19];
    [tm1 show];


}

1.

 本地访问 需要声明public 公众变量

 所有成员不允许外部访问

 get 必须存在返回值

 set 赋值定义

 带一个参数 self = 方法名 = 值

 方法名 andSecond:  : 也是方法名字的一部分

self 也可以作为类本身使用

猜你喜欢

转载自www.cnblogs.com/zhangqing979797/p/13179644.html