//
// Person.h
#import <Foundation/Foundation.h> @interface Person : NSObject @property int age;
@property NSString *name; /*
自定义构造方法:
其实就是自定义一个init方法
1.一定是对象方法
2.一定返回id/instancetype
3.方法名称一定以init开头
*/
- (instancetype)initWithAge:(int)age; //- (instancetype)initwithAge:(int)age; // 注意: 自定义构造方法中的init后面的With的W一定要大写 // 一个类可以有0个或者多个自定义构造方法
- (instancetype)initWithName:(NSString *)name; // 自定义构造方法可以有1个或多个参数
- (instancetype)initWithAge:(int)age andName:(NSString *)name;
@end
//
// Person.m #import "Person.h" @implementation Person - (instancetype)init
{
if (self = [super init]) {
_age = ;
}
return self;
} - (NSString *)description
{
return [NSString stringWithFormat:@"age = %i, name = %@", _age, _name];
} - (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;
}
@end
//
// Student.h #import "Person.h" @interface Student : Person @property int no; // 学号 - (instancetype)initWithAge:(int)age andName:(NSString *)name andNo:(int)no;
@end
//
// Student.m
// day14 #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];
}
*/
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
//
// main.m
// 自定义构造方法在继承中的表现 #import <Foundation/Foundation.h>
#import "Student.h"
#import "Person.h" int main(int argc, const char * argv[]) { // Student *stu = [[Student alloc] initWithAge:30 andName:@"lnj"];
Student *stu = [[Student alloc] initWithAge: andName:@"lnj" andNo:];
NSLog(@"%@", stu);
return ;
}

最新文章

  1. z-stack协议uart分析(DMA)
  2. 通过PowerShell启用AADC的密码同步功能
  3. html的基础标签
  4. shenben语录
  5. 菜鸟学Windows Phone 8开发(2)——了解XAML
  6. Linus:利用二级指针删除单向链表
  7. java 面向对象编程 第20章 XML技术解析
  8. 第六周 N题
  9. 基于PCA和SVM的人脸识别
  10. 最小生成树Prim算法
  11. HashMap 之弱引用 - WeakHashMap
  12. CentOS下使用VirtualBox 安装 Windows虚拟机的简单方法
  13. python3 获取Linux系统信息
  14. 在docker宿主机上查找指定容器内运行的所有进程的PID
  15. arcsde10 postgresql8.3 服务停止问题
  16. (算法)AA制
  17. IOS 地图移动中心点获取
  18. mysql的骚操作:自增长的字段同时插入到另一个字段
  19. onclick传参
  20. C#里面BLL、Model、DAL、UI层

热门文章

  1. Python--10、进程知识补充
  2. python gdal 矢量转栅格
  3. java攻城狮之路--复习JDBC
  4. MYSQL数据库迁移到ORACLE数据库
  5. PHP 之中文转为拼音
  6. Appium 使用android_uiautomator定位元素时报错: The requested resource could not be found, or a request was received using an HTTP method that is not supported by the mapped resource
  7. IE浏览器new Date()带参返回NaN解决方法
  8. php第十二节课
  9. HDU 1465(错排公式)
  10. 关于linux内核用纯c语言编写的思考