iOS OC01_类和对象

.h

//

//  Student.h

//  OC01_类和对象

//

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

//  Copyright (c) 2015 zhozhicheng. 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

// 文件名和类名没有任何关系,但是为了方便对文件里的类进行管理,所以让文件名和类型相同

// 文件中可以有多个类,但是还是方便管理的原则,一个文件中只写一个类







.m

//

//  Student.m

//  OC01_类和对象

//

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

//  Copyright (c) 2015 zhozhicheng. 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






main

//

//  main.m

//  OC01_类和对象

//

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

//  Copyright (c) 2015 zhozhicheng. All rights reserved.

//


#import <Foundation/Foundation.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->_stuName);

//    stu->_stuAge = 100;

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

//    

//    //改姓名

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

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

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

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

    

    

    

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

    

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

//    [car knock];

//    car->_carCharge =@"10000";

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

    

    

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

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

    

    

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

    NSLog(@"%ld",car->_carCharge);

    

    

    

    // 使用sayHi方法

    [car sayHi];

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    return 0;

}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值