Objective-C学习笔记-自定义类

1.OC中一个类由.h文件和.m文件组成,.h文件负责声明接口,.m文件负责具体实现

2.在.h文件中@interface后面的格式为类名:基类名

3.成员变量需要写在大括号内,最好使用下划线开头,使用成员变量需要写存取方法,为了开发效率,目前推荐使用属性代替成员变量,属性会自动生成带下划线的成员变量以及存取方法,还可以通过readonly,readwrite等来控制属性特性

4.成员方法以减号开头,静态成员方法以加号开头,参数类型以及返回值类型需要用括号括起来

5.可以通过重写description方法,让%@能描述自身

6.OC中self代表自己,super代表调用父类方法

7.在.m文件中可以使用@interface声明一些对外不可见的成员变量和成员方法

8.OC中可以通过@public @private @protecte等修饰成员变量,但是不能修饰成员方法

#import <Foundation/Foundation.h>

@interface MyDesktop : NSObject
{
    @public
    int _weight;
    int _height;
}
-(int)height;
-(void)setHeight:(int)h;
-(int)weight;
-(void)setWeight:(int)w;
-(void)setCustomSize:(int)newSize;

+(void)sayHello;

@property(nonatomic,readonly) int size;
@end
#import "MyDesktop.h"
//类扩展
@interface MyDesktop()
@property(nonatomic)int childCount;
@property(nonatomic,readwrite) int size;
@end

@implementation MyDesktop

- (int)height{
    return _height;
}

- (void)setHeight:(int)h{
    _height=h;
}

- (int)weight{
    return _weight;
}

- (void)setWeight:(int)w{
    _weight=w;
}

-(void)setCustomSize:(int) newSize{
    self.size=newSize;
}

- (NSString *)description
{
    return [NSString stringWithFormat:@"height=%d,weight=%d,size=%d,childcount=%d", _height,_weight,_size,_childCount];
}

+(void)sayHello{
    NSLog(@"hello world");
}
@end

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值