main
// main.m
// 12.5
//
// Created by 「ゆき」 on 15/12/5.
// Copyright © 2015年 「ゆき」. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[]) {
#warning 类和对象
/*
*创建对象
*[Receiver message] 消息语法
*[调用方法的人 方法 ]
*1. 开辟内存空间
*让Person类调用alloc方法 开辟一段内存空间 清空,返回首地址
*使用指针保存了对象所在堆内存空间所在地址 一般说 使用指针代替对象
*/
Person *p = [Person alloc];
//2. 初始化
p = [p init];
//创建对象正常人^_^方法
Person *P1 = [[Person alloc]init];
//alloc 类方法 + 由类调用
//init 实例方法 - 由对象调用
Person *p1 = [[Person alloc]init];
[P1 sayHi];
//public修饰下 在main函数下(Person类的外部) 通过 对象名 -> 成员变量 进行取值/赋值
return 0;
}
.h 文件
// Person.h
// 12.5
//
// Created by 「ゆき」 on 15/12/5.
// Copyright © 2015年 「ゆき」. All rights reserved.
//
#import <Foundation/Foundation.h>
//接口 类名 父类名
@interface Person : NSObject
/*
*.h文件 用来存放类的接口
*解决问题套路: 定义类 ->创建对象 ->使用对象
*类的描述: 具有相同特征和行为的事物的抽象(集合)
*类的构成: 接口@interface.....@end/实现@implementation.....@end
*类和文件没有关系
*.h文件也被称为头文件/接口文件
*.m文件 实现文件
*/
{
//类的特征
//成员(实例)变量
//成员变量可见度修饰符
// public(公开) private(私有) protected(保护) package(打包)
// @public// 公开
// NSString * -> char */char
NSString *_name;// 名字
// @protected
NSString *_sex;// 性别
// NSInteger -> int/long
NSInteger _age;// 年龄
// @private
// CGFloat -> float/double
CGFloat _height;// 身高
}
// 行为
// 方法(类似函数)
//void sayHi(void);
// 方法声明
/// 打招呼
- (void)sayHi;
// 吃
- (void)eat;
// 喝
- (void)drink;
// 玩
- (void)play;
// 睡
- (void)sleep;
@end
.m 文件
// Person.m
// 12.5
//
// Created by 「ゆき」 on 15/12/5.
// Copyright © 2015年 「ゆき」. All rights reserved.
//
import "Person.h"
//.m文件 用来存放类的实现
//实现 类名
@implementation Person
- (void)sayHi
{
NSLog(@"雪儿吃黑森林");
NSLog(@"法式焗蜗牛");
//当前类中可以使用自己的成员变量
//%@ NSString
//%ld NSInteger
_name = @"雪儿";
_age = "21";
NSLog(@"%@ %ld",_name,_age);
}
-(void)eat
{
NSLog(@"吃吃吃");
}
-(void)drink
{
NSLog(@"喝喝喝");
}
-(void)play
{
NSLog(@"一起出去玩");
}
-(void)sleep
{
NSLog(@"睡你MB,起来嗨");
}
@end
“`