oc类和对象

//  main.m

//  OC01类和对象

//

//  Created by dllo on 15/7/15.

//  Copyright (c) 2015年 cml. All rights reserved.

//

 

#import <Foundation/Foundation.h>

// 先引头文件

#import"Phone.h"

#import"Audio car.h"

#import"Student.h"

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

    

    // 打印

//    NSLog(@"@刘山山");

//    // 整形

//    NSInteger i =100;

//    NSLog(@"%ld", i);

//    // 浮点型

//    CGFloat f =3.14;

//    NSLog(@"%g",f);

//    // 字符串

//    // OC的字符串方中文没有问题

//    NSString *str = @"刘山山";

//    NSLog(@"%@",str);

//    // OC里的数组

//    NSArray *arr = @[@"1", @"2"];

//    NSLog(@"%@",arr);

//    for (NSInteger i =0; i<2; i++) {

//        NSLog(@"%@",arr[i]);

//    }

    

    // 创建一个对象

    // 创建对象需要两步

    // 1.需要给对象开辟空间,开辟为空间的内存

//    Student *stu = [Student alloc];

    // 2.对象内存开辟之后,需要对对象进行初始化的设置

//    stu =[stu init];

    // 把两步进行合并

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

    

    // 通过对象来调用行为

    

//    [stu sayHi];

    

    // 操作成员变量

    // 对象通过->来访问自己的成员变量

//    NSLog(@"%@", stu->_stuAge);

//    

//    stu->_stuAge =100;

//    NSLog(@"%ld", stu ->_stuAge);

//    

//    // 改姓名

//    stu->_stuName = @"杨林";

//    NSLog(@"%@", stu->_stuName);

//    stu->_stuName =@"刘山山";

//    NSLog(@"%@",stu->_stuName);

    

    

    // 通过车类,创建对象,并且对对象的成员变量进行修改

//    Phone *phone = [[Phone alloc] init];

//    Audio_car *car = [[Audio_car alloc] init];

//    [car run];

//    car->_carColor =@"red";

//    NSLog(@"%@", car->_carColor);

    

     // 通过手机类,创建对象,并且对对象的成员变量进行修改

//    Phone *phone =[[Phone alloc] init];

//    phone->_color =@"红";

//    NSLog(@"@颜色",phone->color);

//    

    

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

//    NSLog(@"%@",stu->_stuName);

//    

 

//    Phone *phone =[[Phone alloc] init];

//    NSLog(@"%g", phone->_price);

//    

//    // 使用sayHi的方法

//    [phone sayHi];

//    phone->_type = @"你好";

//    [phone sayHi];

//    

    // 30 个类

    // 五个成员变量

    

    // 1个能打印全部信息的sayHi方法

    // 2个随便的方法

    

    // 完成init方法的重写

    

    

    

    

    

    return 0;

}

//

//  Student.h

//  OC01类和对象

//

//  Created by dllo on 15/7/15.

//  Copyright (c) 2015年 cml. All rights reserved.

//

 

#import <Foundation/Foundation.h>

 

// @interface 接口文件,一个类的开始

// Student 是当前类名

// NSObject是继承的父类

// 类到@end才结束

 

@interface Student : NSObject

// 特征

{

    @public

    // 成员变量的可见度

    // 成员变量,实例变量

    NSString *_stuName;  // 姓名

    NSString *_stuSex;   // 性别

    NSInteger _stuAge;   // 年龄

    CGFloat _stuScore;   // 成绩

    NSString *_stuHobby; // 爱好

}

 

// 行为

// 打招呼

- (void)sayHi;

// 吃

- (void)eat;

// 玩

- (void)play;

 

@end

//  Student.m

//  OC01类和对象

//

//  Created by dllo on 15/7/15.

//  Copyright (c) 2015年 cml. All rights reserved.

//

 

#import "Student.h"

// 对应的实现文件

@implementation Student

 

// 打招呼

- (void)sayHi{

    NSLog(@"你好");

}

 

// 吃

- (void)eat{

    NSLog(@"吃饭吧");

}

// 玩

- (void)play{

    NSLog(@"想玩");

}

 

// 重写继承过来的init方法

- (id)init{

    _stuName =@"何岸";

    _stuSex  =@"女";

    _stuHobby=@"男";

    _stuAge = 20;

    _stuScore=100;

    return self;

}

 

@end

/  Audio car.h

//  OC01类和对象

//

//  Created by dllo on 15/7/15.

//  Copyright (c) 2015年 cml. All rights reserved.

//

 

#import <Foundation/Foundation.h>

 

@interface Audio_car : NSObject

{

    @public

    NSInteger _carMoney;

    CGFloat  _carV;

    NSString *_carColor;

    CGFloat _carDisplacement;

    NSString *_carSize;

    CGFloat _carWeight;

    

}

 

- (void)run;

 

- (void)fuelCharge;

 

- (void)knock;

 

@end

 

//  Audio car.m

//  OC01类和对象

//

//  Created by dllo on 15/7/15.

//  Copyright (c) 2015年 cml. All rights reserved.

//

 

#import "Audio car.h"

 

@implementation Audio_car

- (void)run{

    NSLog(@"行驶");

}

 

- (void)fuelCharge{

    NSLog(@"");

}

 

- (void)knock{

    NSLog(@"");

}

@end

//  Phone.h

//  OC01类和对象

//

//  Created by dllo on 15/7/15.

//  Copyright (c) 2015年 cml. All rights reserved.

//

 

#import <Foundation/Foundation.h>

 

@interface Phone : NSObject

{

    @public

    NSString *_phoneName;

    NSString *_phoneColor;

    NSInteger phonePrice;

}

 

-(void)tpBoy;

 

-(void)

  

// 写一个用来打印全部信息的功能

- (void)sayHi;

 

//  Phone.m

//  OC01类和对象

//

//  Created by dllo on 15/7/15.

//  Copyright (c) 2015年 cml. All rights reserved.

//

 

#import "Phone.h"

 

@implementation Phone

 

 

 

 

 

 

 

 

// 重写电话的初始化方法

-(id)init

{

    _color = @"白色";

    _price = 1999;

    _type= @"大屏";

    _brand = @"华为";

    _screen = @"4.7寸";

    return self;

}

 

 

- (void)sayHi

{

    NSLog(@"%@,%@ ,%@,%@,%g",_brand, _type, _color, _screen, _price);

    

}

 

@end

 

转载于:https://www.cnblogs.com/cmle/p/4649499.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值