知识点:继承,方法调用,多态
带参构造方法(多样化初始化),便利构造
研究性知识点:
1.搜索了解instancetype的含义
2.了解博客中对于self和super的理解
<Person.h>
//
// Person.h
// 5-15-OC-Class2-Lecture
//
// Created by Knight on 5/15/15.
// Copyright (c) 2015 Knight. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Person : NSObject
{
NSInteger age;
NSString *gender;
NSString *name;
}
- (instancetype)init;
- (instancetype)initWithAge:(NSInteger)initAge;
- (instancetype)initWithName:(NSString *)initName andAge:(NSInteger)initAge;
+ (instancetype)personWithAge:(NSInteger)age1;
+ (instancetype)personWithName:(NSString *)name1 andAge:(NSInteger)age1;
+ (instancetype)initPersonWithName:(NSString *)name andAge:(NSInteger)age1;
- (void)initData;
//写一个find方法,通过电脑编号查找,不需要返回值
- (void)find:(NSInteger)computerNumber;
//写一个方法,比较2个人的名字,返回字母顺序靠前的名字
- (NSString *)compareName:(NSString *)name1 secondName:(NSString *)name2;
//更好的方法,把整个对象作为形参
- (NSString *)compareName:(Person *)person1 secondPerson:(Person *)person2;
//比较2个人年龄,返回较大的年龄
- (NSInteger)compareAge:(NSInteger)personAge;
- (NSInteger)comparePersonAge:(Person *)person;
/**
* 方法的分类: 类方法(+) : 获取,创建,实例化。例如 + (instancetype)creatPerson
* 实例方法(-) : 动作,事件
*/
+ (instancetype)creatPerson; //创建(对象)
+ (instancetype)personWithName:(NSString *)name; //创建,实例化
- (NSString *)getTime; //动作
+ (NSString *)beijingTime; //获取(静态数据)
- (NSInteger)age;
- (void)setAge:(NSInteger)age1;
- (NSString *)gender;
- (void)setGender:(NSString *)gender1;
- (void)setName:(NSString *)name1;
- (NSString *)name;
- (Person *)compareWithAge:(Person *)other;
+ (Person *)compareAgeBetween:(Person *)person1
and:(Person *)person2;
@end
- (instancetype)initWithAge:(NSInteger)initAge
{
self = [super init];
if (self) {
age = initAge;
}
return self;
}
- (instancetype)initWithName:(NSString *)initName andAge:(NSInteger)initAge
{
self = [super init];
if (self) {
name = initName;
age = initAge;
}
return self;
}
//便利构造(整合alloc,init)
+ (instancetype)personWithAge:(NSInteger)age1
{
//从头构造对象
Person *person = [[self alloc] initWithAge:age1];
return person;
<Person.m>
//
// Person.m
// 5-15-OC-Class2-Lecture
//
// Created by Knight on 5/15/15.
// Copyright (c) 2015 Knight. All rights reserved.
//
#import "Person.h"
@implementation Person
/**
* 方法 = 功能 + 动作
*/
- (instancetype)init
{
self = [super init]; //对象内存分配后,会被初始化为父类的对象
//super: 把对象当作父类的状态去调用方法
if (self) {
//成员变量的默认值和初始设定
age = 23;
// NSLog(@"person init");
}
return self;
}
- (void)initData
{
NSLog(@"person initData");
}
//带参数初始化方法
//优点:帮助所有子类便利构造
//无需内存管理
}
+ (instancetype)personWithName:(NSString *)name1 andAge:(NSInteger)age1
{
Person *person = [[self alloc] initWithName:name1 andAge:age1];
return person;
}
//写一个便利构造方法,通过age和name构造person
+ (instancetype)initPersonWithName:(NSString *)name andAge:(NSInteger)age1
{
Person *person = [[self alloc] initWithName:name andAge:age1];
return person;
}
- (NSInteger)compareAge:(NSInteger)personAge
{
//这里age是自己的年龄,初始化方法完成之后,自己就具有age的值
if (age > personAge) {
return age;
} else {
return personAge;
}
}
//当完成事件需要某个功能作为辅助的时候,可以定义单独的方法完成事件。
- (NSInteger)comparePersonAge:(Person *)person
{
if (age > [person age]) {
return age;
} else {
return [person age];
}
}
//成员变量允许外部获取
- (NSInteger)age
{
return age;
}
//成员变量允许外部设置
- (void)setAge:(NSInteger)age1
{
age = age1;
}
//增加一个成员变量gender,然后完成它的设值和取值
- (NSString *)gender
{
return gender;
}
- (void)setGender:(NSString *)gender1
{
gender = gender1;
}
//增加一个成员变量name
- (void)setName:(NSString *)name1
{
name = name1;
}
- (NSString *)name
{
return name;
}
/**写一个方法,返回年龄较大的那个人
*(1)从群体中,找出2个人,返回年龄较大的人: 用类方法
*(2)一个人,与另外一个人比较年龄,返回年龄大的人: 用实例方法
*/
- (Person *)compareWithAge:(Person *)other
{
if (age > [other age]) {
return self;
} else {
return other;
}
}
+ (Person *)compareAgeBetween:(Person *)person1
and:(Person *)person2
{
if ([person1 age] > [person2 age]) {
return person1;
} else {
return person2;
}
}
@end
<Student.h>
#import "Person.h"
@interface Student : Person
- (void)initData;
@end
<Student.m>
#import "Student.h"
@implementation Student
- (instancetype)init
{
//self的意思:代表自己(对象),指的是调用当前方法的对象
//super的意思:也代表调用方法的对象,但是是将这个对象当作父类使用
self = [super init];
if (self) {
age = 20; // 子类继承父类(Person类)的成员变量,可以直接使用它
// NSLog(@"student init");
}
return self;
}
//如果子类使用和父类相同的方法,将重写这个方法。
/**
* 方法调用原则:优先在本类中查找,如果没有,以此查找父类中的方法。
*/
- (void)initData
{
NSLog(@"student initaData");
}
@end
<main.m>
#import <Foundation/Foundation.h>
#import "Person.h"
#import "Student.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
//person“持有/引用”一个Person类的对象
Person *person = [[Person alloc] init];
// [person initData];
//传人年龄29,和自己的年龄23做比较,返回大的年龄
NSInteger maxAge1 = [person compareAge:15];
NSLog(@"maxAge = %ld", maxAge1);
//传入一个人,自己的年龄和这个人的年龄比较,返回大的年龄
Person *other = [[Person alloc] init];
[other setAge:30];
NSLog(@"Other's age = %ld", [other age]);
NSInteger maxAge2 = [person comparePersonAge:other];
NSLog(@"maxAge = %ld", maxAge2);
//修改person的年龄,比较person和other的年龄
[person setAge:40];
Person *newPerson = [[Person alloc] init];
//调用类方法,消息的接收者是类
newPerson = [Person compareAgeBetween:person and:other];
NSLog(@"maxAge = %ld",[newPerson age]);
// Student *student = [[Student alloc] init];
// [student initData];
Person *newPerson2 = [Person initPersonWithName:@"Mike" andAge:24];
NSLog(@"Name = %@, Age = %ld",[newPerson2 name], [newPerson2 age]);
}
return 0;
}