第04天OC语言(13):自定义构造方法以及成员变量名称注意点
2017-07-15 00:00
495 查看
不要等到明天,明天太遥远,今天就行动。
#####须读:看完该文章你能做什么?
学习到构造方法的一些注意点
#####学习前:你必须会什么?
什么是自定义构造方法
自定义构造方法:
#####一、本章笔记
#####二、code
######main.m
######Person
######Student
#####须读:看完该文章你能做什么?
学习到构造方法的一些注意点
#####学习前:你必须会什么?
什么是自定义构造方法
自定义构造方法:
其实就是自定义一个init方法
#####一、本章笔记
注意 : 自定义构造方法中的init后面的With的W一定要大写 注意 : 属性名称不要以new开头,有可能会引发一些未知错误 注意 : 方法名也不要以new开头
#####二、code
######main.m
#pragma mark 13-自定义构造方法以及成员变量名称注意点 #pragma mark 概念 /* 注意 : 自定义构造方法中的init后面的With的W一定要大写 注意 : 属性名称不要以new开头,有可能会引发一些未知错误 注意 : 方法名也不要以new开头 */ #pragma mark - 代码 #import <Foundation/Foundation.h> #pragma mark 类 #import "Student.h" #import "Person.h" #pragma mark - main函数 int main(int argc, const char * argv[]) { // Student *s = [[Student alloc]initWithAge:33 andName:@"lyh"]; Student *s = [[Student alloc]initWithAge:33 andName:@"lyh" andNo:1]; NSLog(@"%@",s); Person *p = [[Person alloc]init]; // p->newHeight = 1.71; [p newTest]; NSLog(@"%@",p); return 0; }
######Person
>>>.h #import <Foundation/Foundation.h> @interface Person : NSObject { // 注意: 属性名称不要以new开头,有可能会引发一些未知错误 double newHeight; } @property int age; @property NSString *name; //@property double newHeight; - (void)newTest; - (instancetype)initWithAge:(int)age; //- (instancetype)initwithAge:(int)age; // 错误写法 - (instancetype)initWithName:(NSString *)name; - (instancetype)initWithAge:(int)age andName:(NSString *)name; @end >>>.m #import "Person.h" @implementation Person - (void)newTest { NSLog(@"---"); } - (instancetype)init { if (self = [super init]) { _age = 10; } return self; } - (instancetype)initWithAge:(int)age // 注意 : 自定义构造方法中的init后面的With的W一定要大写 //- (instancetype)initwithAge:(int)age { if (self = [super init]) { _age = age; } return self; } - (instancetype)initWithName:(NSString *)name { if (self = [super init]) { _name = name; } return self; } - (instancetype)initWithAge:(int)age andName:(NSString *)name { if (self = [super init]) { _age = age; _name = name; } return self; } - (NSString *)description { NSLog(@"%f",newHeight); return [NSString stringWithFormat:@" age = %i , name = %@",_age,_name]; } @end
######Student
>>>.h #import "Person.h" @interface Student : Person @property int no; // 学号 - (instancetype)initWithAge:(int)age andName:(NSString *)name andNo:(int)no; @end >>>.m #import "Student.h" @implementation Student - (instancetype)initWithAge:(int)age andName:(NSString *)name andNo:(int)no { /* if (self = [super init]) { // _age = age; // 父类的属性不能使用 _去访问 // 狗拿耗子 多管闲事 // 自己的事情自己做 [self setAge:age]; [self setName:name]; _no = no; } */ if (self = [super initWithAge:age andName:name]) { _no = no; } return self; } - (NSString *)description { return [NSString stringWithFormat:@" age = %i , name = %@ no = %i",[self age],[self name],_no]; } @end
相关文章推荐
- 第04天OC语言(12):自定义构造方法在继承中的表现
- 第04天OC语言(11):自定义构造方法
- 第01天OC语言(12):局部变量和全局变量以及成员变量对比
- 构造方法的调用顺序和成员变量的初始化时机以及动态绑定
- ObjectC语言基础4—ARC、成员变量、构造方法、property
- OutMan——Objective-C中成员变量的作用域、@property和@synthesize介绍和使用、构造方法和自定义构造方法
- OC特有语法1—点语法,成员变量作用域,@property,id,构造方法,多文件编程
- 第04天OC语言(08):构造方法基本概念
- OC特有语法1—点语法,成员变量作用域,@property,id,构造方法,多文件编程
- IOS开发---OC语言-⑭id、构造方法、自定义构造方法
- Java语言基础{Java_se(05)}-面向对象-封装-构造方法-成员变量,局部变量
- 黑马程序员——OC核心语法点语法,成员变量,@property和@synthetic,构造方法
- 黑马程序员-OC学习笔记-点方法、成员变量作用域以及set方法和get方法@property的综合题
- 黑马程序员——OC学习小结------成员变量以及对应的set、get方法
- OC的特有语法-分类Category、 类的本质、description方法、SEL、NSLog输出增强、点语法、变量作用域、@property @synthesize关键字、Id、OC语言构造方法
- OC语言学习 (三) 成员变量get/set方法和“.”语法,@proterty和@synthesize关键字
- OC语言学习 (三) 成员变量get/set方法和“.”语法,@proterty和@synthesize关键字
- 黑马程序员-OC语言-self修饰变量以及使用总结和注意事项(听课笔记)
- 第04天OC语言(15):类工厂方法在继承中的注意点
- 黑马程序员-OC的构造方法init以及自定义构造方法