类与对象

 

#import <Foundation/Foundation.h>

 

//面向过程,C语言是偏向面向过程编程的语言,C语言也可以实现面向对象编程,如:操作系统底层代码实现,很多都用了面向对象的编程思想

//面向过程的程序设计,把事物按照事物的发展过程,分成很多执行步骤,每一步都用函数实现,通过函数的调用实现对应的功能

//五子棋

//1.显示棋盘

//2.黑子先下

//3.判断输赢

//4.显示棋盘

//5.白子下棋

//6.判断输赢

//7.显示棋盘

//8.返回第二步

//9.赢棋结束

 

//面向对象编程

//把事物分成很多个对象,每一个对象都有对应的描述方法,对象之间通过发送消息进行通信,最终实现面向对象的程序设计

//1.棋盘对象

//2.黑白双方

//3.下棋规则

 

//首先是黑白双方接受终端输入下棋,发送消息给棋盘对象显示棋盘,棋盘对象发送消息给下棋规则对象,下棋规则对象判断输赢,没有赢发送消息给黑白双方

 

//类与对象

//从生活逻辑上

//人类     章子怡 奥巴马 张兆帅

//dog类    xiaoHei  xiaoBai

//电脑类    我桌子上这台  你桌子上那台

 

//从封装的角度上

//结构体: 封装数据类型

//函数:封装实现功能的方法

//类: 既封装了数据类型,又封装对应的实现方法

 

//可以把类看成一个升级版的结构体

//对象就是类定义的实例  Person *xiaoHong

 

//对象既封装了数据类型,又封装了实现方法

 

//从编程角度看

//对象就是类定义的变量

 

//官方定义

//类: 具有相同特征事物的统称

//对象: 类的实例化

 

//实例与方法

//把对象也叫着实例, 对象就是类的实例化

//操作对象的方法叫着实例方法

//

//[mycar drive];

//[mycar wash];

 

//[userText write];

//[userText read];

//[windows erase];

//[word  reloade];

 

//instance 实例(对象)

//instance variable 实例变量

 

//类的声明

@interface Student : NSObject //Student类继承与NSObject类, 类名首字母大写,Student是类名

{

    int _age;//实例变量以_开头 习惯使然 也符合标示符命名规范

    NSString *_name;

}

 

//构造方法

//返回值为id类型 方法名: init

- (id)init; //-表示对象方法,给对象调用 +表示类方法,给类本身调用

//id 类似与 C语言 void * 相当于泛型指针

 

//setter方法

//返回值: void  方法名: setAge: 形式参数: newAge

- (void)setAge:(int)newAge;

- (void)setName:(NSString *)newName;

//返回值: void 方法名: setAge:andName: 形式参数: newAge newName;

 

- (void)setAge:(int)newAge andName:(NSString *)newName;

 

//方法名: setAge::

//- (void)setAge:(int)newAge :(NSString *)newName;

 

 

//getter方法

 

- (int)getAge;

- (NSString *)getName;

 

//打印对象方法

 

- (void)print;

 

//类方法

 

+ (void)test;

 

@end

 

//类的实现

 

@implementation Student  //OC中的关键字都是以@开头

 

- (id)init //重写父类的构造方法

{

    self = [super init];//指向自己的对象指针 super是调用父类对象的关键字,相当于一个编译器符号, 先使用父类的构造方法构造对象返回

    if (self) {//判断父类方法是否调用成功

        _age = 12;

        _name = @"xiaohong";

    }

    return self;

}

 

//setter方法实现

//在当前类中,可以直接访问类中的实例变量,直接访问实例变量的方法有两种

//1.通过实例变量名直接访问

//2.通过 self指针直接访问

 

- (void)setAge:(int)newAge

{

    self->_age = newAge;

}

 

- (void)setName:(NSString *)newName

{

    _name = newName;

}

 

- (void)setAge:(int)newAge andName:(NSString *)newName

{

    _age = newAge;

    _name = newName;

}

 

//getter方法实现

 

- (int)getAge

{

    return _age;

}

 

- (NSString *)getName

{

    return _name;

}

 

- (void)print

{

    NSLog(@"name is %@, age is %d", _name,_age);

}

 

//NSLog类似于C语言中printf

//%@ 打印对象

//NSLog自动换行

 

//测试当前类是否可用

+ (void)test

{

    Student *xiaoFei = [[Student alloc] init];

    [xiaoFei setAge:20 andName:@"小非"];

    NSLog(@"name is %@ , age is %d", [xiaoFei getName],[xiaoFei getAge]);

}

@end

 

 

int main(int argc, const char * argv[]) {

    @autoreleasepool {

//        //1.用类创建一个对象

//        //xiaoHong 是对象指针变量  alloc申请的内存空间是对象

////        Student * xiaoHong = [Student alloc];//通过alloc方法创建对象

////        

////        xiaoHong = [xiaoHong init];

//        Student *xiaoHong = [[Student alloc] init];

//        

//        //NSLog(@"%p %ld", xiaoHong,sizeof(xiaoHong));

//        

//        [xiaoHong print];

//        

//        [xiaoHong setAge:20 andName:@"小红"];

//        [xiaoHong print];

//        

//        NSLog(@"名字:%@ 年龄: %d", [xiaoHong getName],[xiaoHong getAge]);

        

        [Student test];//利用类名调用类方法

        

    }

    return 0;

}

 

 

 

、、、、、、、、、、、、、、、、、、、、、、、、、、、分割线、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

以下程序顺序:main.m  --> .h -->.m

、、、、、、、、、、、、、、、、、、、、、、、、、、、分割线、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

#import <Foundation/Foundation.h>

#import "Person.h"//使用 import包含

//C与OC区别

//1.C语言包含头文件用include OC用import

//2.C语言为了防止头文件的重复包含 用#ifndef #define #endif

//OC语言不需要考虑这一点@class 防止循环包含

//3. OC的文件用.m C语言用.c

//4. OC语言的关键字用@开头

//5. OC语言完全兼容C语言

 

 

 

int main(int argc, const char * argv[]) {

    @autoreleasepool {

        

        [Person testPerson];

        

        

    }

    return 0;

}

 

 

 

#import <Foundation/Foundation.h>

 

@interface Person : NSObject

{

    float _weight;

    float _height;

    NSString *_name;

}

//构造方法

- (id)init;

 

//setter方法

 

- (void)setWeight:(float)newWeight;

 

- (void)setHeight:(float)newHeight;

 

- (void)setName:(NSString *)newName;

 

- (void)setWeight:(float)newWeight andHeight:(float)newHeight andName:(NSString *)newName;

 

//getter

 

- (float)weight;//习惯使然

- (float)height;

- (NSString *)name;

 

//打印方法

 

- (void)printPersonInformation;//OC中的命名规范,首字母小写,以后每个单词的首字母大写 "驼峰规则"

 

//类方法

+ (void)testPerson;

 

@end

 

 

 

#import "Person.h"

 

@implementation Person

 

- (id)init

{

    self = [super init];

    if (self) {

        _name = @"小红";

        _height = 165.5;

        _weight = 54.6;

    }

    return self;

}

 

- (void)setWeight:(float)newWeight andHeight:(float)newHeight andName:(NSString *)newName

{

    _weight = newWeight;

    _height = newHeight;

    _name = newName;

}

 

- (NSString *)name

{

    return self->_name;

}

 

- (float)weight

{

    return self->_weight;

}

 

- (float)height

{

    return self->_height;

}

 

- (void)printPersonInformation

{

    NSLog(@"name is %@  weight is %.2f height is %.2f", [self name], [self weight], [self height]);//通过self指针调用对应的方法

}

 

+ (void)testPerson

{

    Person *xiaoHua = [[Person alloc] init];

    [xiaoHua setWeight:67.8 andHeight:175.4 andName:@"小华"];

    NSLog(@"name is %@, weight is %.2f height is %.2f", [xiaoHua name], [xiaoHua weight], [xiaoHua height]);

}

 

@end

 

 

 

、、、、、、、、、、、、、、、、、、、、、、、、、、、、分割线、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

构造方法

#import <Foundation/Foundation.h>

#import "Dog.h"

 

int main(int argc, const char * argv[]) {

    @autoreleasepool {

        //Dog *dog = [[Dog alloc] init];//把对象空间清0

        [Dog testDog];

        

    }

    return 0;

}

 

 

#import <Foundation/Foundation.h>

 

@interface Dog : NSObject

{

    int _age;

    NSString *_name;

}

 

//构造方法

//- (id)init;

//必须以initWithXXX开头

 

- (id)initWithAge:(int)newAge;

 

- (id)initWithName:(NSString *)newName;

 

- (id)initWithAge:(int)newAge andName:(NSString *)newName;

 

//setter方法

 

- (void)setAge:(int)newAge andName:(NSString *)newName;

 

//getter方法

- (int)age;

- (NSString *)name;

 

//打印方法

- (void)printDogInfomation;

 

+ (void)testDog;

 

@end

 

 

 

#import "Dog.h"

 

@implementation Dog

 

- (id)init

{

    self = [super init];//super是调用父类的关键字, 先用父类的方法初始化

    if (self) {//初始化失败返回nil 类似于C语言中的NULL  (void *)0

        self->_age = 12;

        self->_name = @"xiaoHei";

    }

    return self;

}

 

- (id)initWithAge:(int)newAge

{

    if (self = [super init]) {

        self->_age = newAge;

    }

    return self;

}

 

- (id)initWithName:(NSString *)newName

{

    if (self = [super init]) {

        self->_name = newName;

    }

    return self;

}

 

- (id)initWithAge:(int)newAge andName:(NSString *)newName

{

    if (self = [super init]) {

        self->_age = newAge;

        self->_name = newName;

    }

    return self;

}

- (void)setAge:(int)newAge andName:(NSString *)newName

{

    _age  = newAge;

    _name = newName;

}

 

- (int)age

{

    return _age;

}

 

- (NSString *)name

{

    return _name;

}

 

- (void)printDogInfomation

{

    NSLog(@"name : %@, age : %d", [self name], [self age]);

}

 

+ (void)testDog

{

    Dog *dog1 = [[Dog alloc] init];

    //[dog1 setAge:12 andName:@"小黑"];

    NSLog(@"dog name is %@ dog age is %d", [dog1 name], [dog1 age]);

    Dog *dog2 = [[Dog alloc] initWithAge:30 andName:@"xiaoBai"];

    [dog2 printDogInfomation];

    

}

 

@end

 

 

、、、、、、、、、、、、、、、、、、、、、、、、、分割线、、、、、、、、、、、、、、、、、、、、、、、、、、、、

类方法

#import <Foundation/Foundation.h>

#import "Dog.h"

 

int main(int argc, const char * argv[]) {

    @autoreleasepool {

        [Dog testDog];

       Dog *dog = [Dog creatDogWithName:@"xiaoHuang" andAge:20];

        [dog print];

    }

    return 0;

}

 

 

#import <Foundation/Foundation.h>

 

@interface Dog : NSObject

{

    int _age;

    NSString *_name;

}

 

- (id)initWithName:(NSString *)newName andAge:(int)newAge;

 

- (void)setName:(NSString *)newName andAge:(int)newAge;

 

- (NSString *)name;

- (int)age;

 

- (void)print;

 

+ (id)creatDogWithName:(NSString *)newName andAge:(int)newAge;

 

 

+ (void)testDog;

 

@end

 

 

#import "Dog.h"

 

@implementation Dog

 

- (id)initWithName:(NSString *)newName andAge:(int)newAge

{

    if (self = [super init]) {

        _name = newName;

        _age = newAge;

    }

    return self;

}

 

- (void)setName:(NSString *)newName andAge:(int)newAge

{

    _name = newName;

    _age = newAge;

}

 

- (NSString *)name

{

    return _name;

}

 

- (int)age

{

    return _age;

}

 

- (void)print

{

    NSLog(@"name is %@, age is %d", [self name], [self age]);

}

 

//类方法给类调用的

//用类名调用

+ (void)testDog

{

    id dog = [[self alloc] initWithName:@"xiaoHei" andAge:12];//self 指向调用的类

    [dog print];

}

 

+ (id)creatDogWithName:(NSString *)newName andAge:(int)newAge

{

    id dog = [[self alloc] init];

    

    [dog setName:newName andAge: newAge];

    

    return dog;

}

 

@end

 

、、、、、、、、、、、、、、、、、、、、、、、、、、、分割线、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

#import <Foundation/Foundation.h>

#import "Adder.h"

 

int main(int argc, const char * argv[]) {

    @autoreleasepool {

        // insert code here...

        //

        //NSLog(@"Hello, World!");

        [Adder testAdder];

        

    }

    return 0;

}

 

 

#import <Foundation/Foundation.h>

 

@interface Adder : NSObject

 

 

+ (int)addA:(int)a andB:(int)b;

 

+ (void)testAdder;

 

@end

 

 

#import "Adder.h"

 

@implementation Adder

 

+ (int)addA:(int)a andB:(int)b

{

    return a+b;

}

 

+ (void)testAdder

{

    int a, b;

    scanf("%d %d",&a, &b);

    NSLog(@"sum = %d",[self addA:a andB:b]);

}

@end

 

 

、、、、、、、、、、、、、、、、、、、、、、、、、、、、分割线、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

单例类

#import <Foundation/Foundation.h>

#import "Dog.h"

//单例类

//单例类创建的对象只有一份

//实现多个对象指针共享数据

 

int main(int argc, const char * argv[]) {

    @autoreleasepool {

        //NSFileManager *file = [NSFileManager defaultManager];

//        Dog *dog1 = [[Dog alloc] init];

//        Dog *dog2 = [[Dog alloc] init];

//        Dog *dog3 = [[Dog alloc] init];

//        

//        NSLog(@"dog1 = %p", dog1);

//        NSLog(@"dog2 = %p", dog2);

//        NSLog(@"dog3 = %p", dog3);

 

        //单例

        

        Dog *dog1 = [Dog defaultDog];

        Dog *dog2 = [Dog defaultDog];

        Dog *dog3 = [Dog defaultDog];

        NSLog(@"dog1 = %p", dog1);

        NSLog(@"dog2 = %p", dog2);

        NSLog(@"dog3 = %p", dog3);

        

        

    }

    return 0;

}

 

 

#import <Foundation/Foundation.h>

 

@interface Dog : NSObject

 

//必须以default开头

//+ (NSFileManager *)defaultManager;

 

+ (Dog *)defaultDog;

 

@end

 

 

 

#import "Dog.h"

 

static Dog *dog=nil;

 

@implementation Dog

 

+ (Dog *)defaultDog

{

    if (!dog) {

        dog = [[self alloc] init];

    }

    return dog;

}

 

 

 

@end