OC2

#import <Foundation/Foundation.h>
#import "Human.h"
#import "Person.h"
#import "Student.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
// Human *human = [[Human alloc]init];

/*
-:实例方法
1、调用方法
[对象 方法名]
2、格式
-(返回类型)方法名
-(返回类型)方法名参数描述名1:(参数类型)参数名
-(返回类型)方法名参数描述名1:(参数类型)参数名1 参数描述名2:(参数类型)参数名2 .....
*//*
// 无返回值 ,无参数
[human buywater];
[human sayHi];
//无返回,有一个参数
// [human lentOfMoney:500000000000];
// 无返回值,传两个参数
// [human drinkOfwater:@"农夫山泉" tea:@"铁观音"];
//有返回值,无参数
NSString *tea = [human drinktea];
NSLog(@"tea:%@",tea);
// 有返回值,传两个参数
NSString *newstring = [human drinkOfOtherWater:@"矿泉水" tea:@"绿茶"];
NSLog(@"new:%@",newstring);

NSInteger sum = [human sunOfNumber:1000 otherNumber:2000];
NSLog(@"sum= : %ld",sum);
*/


/*
+:类方法
1、调用方法
[类名 方法名]
*/
// [Human test];
// Human *human1 = [Human human];
// NSLog(@"human1 :%@",human1);
// Human *human2 = [Human humanWithName:@"lili" age:22];
// NSLog(@"%@" ,human2);
//
// //setter getter
// Human *human3 = [[Human alloc]init];
// [human3 setName:@"xiaoli"];
// [human3 setAge:22];
// NSLog(@"姓名:%@ 年龄:%ld",[human3 name],(long)[human3 age]);
点语法
// human3.name = @"tom";
// human3.age = 23;
// NSLog(@"姓名:%@ 年龄:%ld",[human3 name],(long)[human3 age]);
Student *student = [[Student alloc]init];
student.name = @"tom";
[student hello];

[student test];

[Student test];

Person *person = [[Student alloc] init];
[person hello];

}
return 0;

}


创建 Cocoa Touch Class文件  类名Human

.h

#import <Foundation/Foundation.h>

@interface Human : NSObject
{   //成员变量
    /*@protected(默认):本类和他的子类的方法可以访问
      @private:本类的方法可以访问
      @public:公开
     */
    NSString *_name;
    NSInteger _age;
}
//行为动作
-(void)buywater;
//实例方法
//无返回值,无参数
//打招呼
-(void)sayHi;
//无返回,有一个参数
- (void)lentOfMoney:(NSInteger)money;
//无返回值,传两个参数
- (void)drinkOfwater:(NSString *)water tea:(NSString *)tea;
//有返回值,无参数
- (NSString *)drinktea;
//有返回值,传两个参数
- (NSString *)drinkOfOtherWater:(NSString *)water tea:(NSString *)tea;

- (NSInteger)sunOfNumber:(NSInteger)number otherNumber:(NSInteger)otherNumber;
-(instancetype)initWithName:(NSString *)name age:(NSInteger)age;


//类方法
+(void)test;
//便利构造方法
+(Human *)human;
+(Human *)humanWithName:(NSString *)name age:(NSInteger)age;
-(NSString*)description; //打印对象的方式


//setter getter//存取
-(void)setName:(NSString *)name;
-(NSString*)name;
-(void)setAge:(NSInteger)age;
-(NSInteger)age;
@end


.m

#import "Human.h"

@implementation Human
-(void)buywater{
    NSLog(@"我想喝水,你去买");
}

-(void)sayHi{
    NSLog(@"早上好");
}

-(void)lentOfMoney:(NSInteger)money{
    NSLog(@"请你借%ld",money);
}

-(void)drinkOfwater:(NSString *)water tea:(NSString *)tea{

    NSLog(@"我想喝%@的%@茶",water,tea);
}

-(NSString *)drinktea{
return @"红茶";
}

-(NSString *)drinkOfOtherWater:(NSString *)water tea:(NSString *)tea{
    NSString *string = [NSString stringWithFormat:@"我想喝%@的%@茶",water,tea];
    return string;
}

-(NSInteger)sunOfNumber:(NSInteger)number otherNumber:(NSInteger)otherNumber{
    return number + otherNumber;
}

-(instancetype)initWithName:(NSString *)name age:(NSInteger)age{
    self = [super init];
    if (self) {
        _name = name;
        _age = age;
    }
    return self;
}


+(void)test{
    NSLog(@"类方法测试");
}

+(Human *)human{
    Human *human1 = [[Human alloc]init];
    return  human1;
}

+(Human *)humanWithName:(NSString *)name age:(NSInteger)age{
    Human *human2 = [[Human alloc]initWithName:name age:age];
    return human2;
}

-(NSString *)description{
    NSString *string = [NSString stringWithFormat:@"name: %@,age:%ld",_name,_age];
    return string;
}





//setter getter的目的 给成员变量赋值
-(void)setName:(NSString *)name{
    _name = name;  //赋值
}
-(NSString*)name{
    return _name;  //返回去
}

-(void)setAge:(NSInteger)age{
    if (age < 0) {
        _age = 18;
        return;
    }else{
        _age = age;
    }
}
-(NSInteger)age{
    return _age;
}

@end


//继承   Student继承Person

创建 Cocoa Touch Class文件  类名Person
.h

#import <Foundation/Foundation.h>

@interface Person : NSObject
{
    
    NSString *_name;
    NSInteger _age;
}
- (void)setName:(NSString *)name;
- (NSString *)name;

- (void)setAge:(NSInteger)age;
- (NSInteger)age;

- (void)hello;
@end

.m
#import "Person.h"

@implementation Person
- (void)setName:(NSString *)name{
    _name = name;
    
}
- (NSString *)name{
    return _name;
}

- (void)setAge:(NSInteger)age{
    _age = age;
}
- (NSInteger)age{
    return _age;
}

- (void)hello{
    NSLog(@"person hello");
}
@end


创建 Cocoa Touch Class文件  类名Student

.m

#import "Student.h"

@implementation Student
//- (void)setName:(NSString *)name{
//    _name = name;
//}
//- (NSString *)name{
//    return _name;
//}
//
//- (void)setAge:(NSInteger)age{
//    _age = age;
//}
//- (NSInteger)age{
//    return _age;
//}
//
- (void)setCode:(NSString *)code{
    _code = code;
}

- (NSString *)code{
    return _code;
}

- (void)hello{
    NSLog(@"student hello");
}

- (void)test{
    NSLog(@"---:%@",self);
}

+ (void)test{
    NSLog(@"+++++:%@",self);
}
@end

.h

#import "Person.h"
#import <Foundation/Foundation.h>
@interface Student : Person
{
    //    NSString *_name;
    //    NSInteger _age;
    NSString *_code;
}
//- (void)setName:(NSString *)name;
//- (NSString *)name;
//
//- (void)setAge:(NSInteger)age;
//- (NSInteger)age;

- (void)setCode:(NSString *)code;
- (NSString *)code;

+ (void)test;
- (void)test;

- (void)hello;
@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值