认识object-c第一天
来源:互联网 发布:minecraft国际版 java 编辑:程序博客网 时间:2024/06/16 14:57
兼容C语言,也可以兼容C++代码
.m 可以开发OC代码 C语言代码
.mm 可以开发C++
BOOL 取值(YES 1 NO 0) char
NSLog(@“hello”) 输出 只能接受OC字符串 会自动换行
#import 的用途 1,跟#include 一样 拷贝文件内容 2,防止文件内容被重复拷贝
.m - 编译 - .o - 链接 - a.out 可执行文件
如果都使用可以引用主头文件
#import <Foundation/Foundation.h>
OC创建一个类 (OC对象不会自动回收)
1,类的声明和定义(实现)
类的声明 用来声明类的属性和行为
@interface 类名 : NSObject
{//用来声明对象属性
@public //可以让外部的指针间接访问内部变量
int 属性;
int xx;
}
//方法(行为)不能写到大括号里:方法名,参数,返回值
//只要是OC对象的方法就要写-
//OC方法中任何数据类型都必须yoga小括号()扩住
//OC方法中的小括号();扩住数据类型
- (void) run;
@end 声明完毕
类的实现 实现类中声明的方法
@implementation 类名
// 方法的实现(说清楚方法里面有什么代码)
- (void) run
{
NSLog(@"车子跑起来");
}
@end 声明完毕
int main{
//在oc中,想执行一些行为,就要协商中括号 ,[行为执行者 行为名称]
//定义了一个指针变量p,p将来指向的是Car类型的对象
Car *p = [Car new];//执行了Car这个类的new 行为来创建对象
p->xx = 4;//给p所指定的对象赋值
Car *p2 = [Car new];//重新创建一个对象
[p2 run];
[p run];//给p所指对象发送一条run消息
return 0;
}
@interface Person:NSObject
{
//成员变量;
@public
int age;
double weight;
}
//方法声明
- (void)walk;
@end
@implementation Person
//实现interface中声明的方法
- (void)walk
{
NSLog(@"%d岁,%fkg走一段路",age,weight);
}
@end
int main(){
//
Person *p = [Person new];
p->age = 23;
p->weight = 100.34;
//每个对象都有一个isa 指针 所有的对象 指向类
[p walk];
return 0;
}
对象跟函数使用的注意点
@interface Car:NSObject
{
@public
int wheels;// 不许初始化
int speed;
}
- (void)run;
@end
@implementation Car
- (void)run
{
NSLog(@"%d多少个轮子,速度为%dkm/h跑起来",wheels,speed);
}
@end
void test(int w,int s)
{
w = 20;
s = 200;
}
void test1(Car *newC){
newC -> wheels = 5;
}
void test2(Car *newC){
Car *c2 = [Car new];
c2 -> wheels = 5;
c2 -> speed = 300;
newC = c2;
newC -> wheel = 6;
}
int main()
{
Car *c = [Car new];
c -> wheels = 4;
c -> speed = 250;
test(c -> wheels,c -> speed);
[c run];
test1(c);
[c run];
test2(c);
[c run];
return 0;
}
OC方法跟函数的区别
1,对象方法都是以-开头
2,对象方法只能声明在@interface和@end之间
实现只能写在@implementation @end之间
3,对象方法只能由对象调用
4,函数能写在文件中的任意位置
5,对象方法对类所有
type emu{
SexMan,
SexWoman
} Sex;
type struct{
int year;
int month;
int day;
} Date;
typedef enum{
ColorBlack;
ColorRed;
ColorGreen;
} Color;
@interface Student:NSObject
{
@public
Sex sex;//性别
Date birthday;//生日
double weight;//体重(kg)
Color favColor;//最喜欢的颜色
}
- (void)eat;
- (void)run;
@end
@implementation Student
- (void)eat{
weight += 1;
NSLog(@"吃完这次后的体重%f",weight);
}
- (void)run{
weight -= 1;
NSLog(@"吃完这次后的体重%f",weight);
}
@end
int main(){
Student *s = [Student new];
s -> weight = 50;
s -> sex = SexMan;
s -> birthday = {2011,9,10};
s -> favColor = ColorBlack;
[s eat];
[s run];
return 0;
}
.m 可以开发OC代码 C语言代码
.mm 可以开发C++
BOOL 取值(YES 1 NO 0) char
NSLog(@“hello”) 输出 只能接受OC字符串 会自动换行
#import 的用途 1,跟#include 一样 拷贝文件内容 2,防止文件内容被重复拷贝
.m - 编译 - .o - 链接 - a.out 可执行文件
如果都使用可以引用主头文件
#import <Foundation/Foundation.h>
OC创建一个类 (OC对象不会自动回收)
1,类的声明和定义(实现)
类的声明 用来声明类的属性和行为
@interface 类名 : NSObject
{//用来声明对象属性
@public //可以让外部的指针间接访问内部变量
int 属性;
int xx;
}
//方法(行为)不能写到大括号里:方法名,参数,返回值
//只要是OC对象的方法就要写-
//OC方法中任何数据类型都必须yoga小括号()扩住
//OC方法中的小括号();扩住数据类型
- (void) run;
@end 声明完毕
类的实现 实现类中声明的方法
@implementation 类名
// 方法的实现(说清楚方法里面有什么代码)
- (void) run
{
NSLog(@"车子跑起来");
}
@end 声明完毕
int main{
//在oc中,想执行一些行为,就要协商中括号 ,[行为执行者 行为名称]
//定义了一个指针变量p,p将来指向的是Car类型的对象
Car *p = [Car new];//执行了Car这个类的new 行为来创建对象
p->xx = 4;//给p所指定的对象赋值
Car *p2 = [Car new];//重新创建一个对象
[p2 run];
[p run];//给p所指对象发送一条run消息
return 0;
}
@interface Person:NSObject
{
//成员变量;
@public
int age;
double weight;
}
//方法声明
- (void)walk;
@end
@implementation Person
//实现interface中声明的方法
- (void)walk
{
NSLog(@"%d岁,%fkg走一段路",age,weight);
}
@end
int main(){
//
Person *p = [Person new];
p->age = 23;
p->weight = 100.34;
//每个对象都有一个isa 指针 所有的对象 指向类
[p walk];
return 0;
}
对象跟函数使用的注意点
@interface Car:NSObject
{
@public
int wheels;// 不许初始化
int speed;
}
- (void)run;
@end
@implementation Car
- (void)run
{
NSLog(@"%d多少个轮子,速度为%dkm/h跑起来",wheels,speed);
}
@end
void test(int w,int s)
{
w = 20;
s = 200;
}
void test1(Car *newC){
newC -> wheels = 5;
}
void test2(Car *newC){
Car *c2 = [Car new];
c2 -> wheels = 5;
c2 -> speed = 300;
newC = c2;
newC -> wheel = 6;
}
int main()
{
Car *c = [Car new];
c -> wheels = 4;
c -> speed = 250;
test(c -> wheels,c -> speed);
[c run];
test1(c);
[c run];
test2(c);
[c run];
return 0;
}
OC方法跟函数的区别
1,对象方法都是以-开头
2,对象方法只能声明在@interface和@end之间
实现只能写在@implementation @end之间
3,对象方法只能由对象调用
4,函数能写在文件中的任意位置
5,对象方法对类所有
type emu{
SexMan,
SexWoman
} Sex;
type struct{
int year;
int month;
int day;
} Date;
typedef enum{
ColorBlack;
ColorRed;
ColorGreen;
} Color;
@interface Student:NSObject
{
@public
Sex sex;//性别
Date birthday;//生日
double weight;//体重(kg)
Color favColor;//最喜欢的颜色
}
- (void)eat;
- (void)run;
@end
@implementation Student
- (void)eat{
weight += 1;
NSLog(@"吃完这次后的体重%f",weight);
}
- (void)run{
weight -= 1;
NSLog(@"吃完这次后的体重%f",weight);
}
@end
int main(){
Student *s = [Student new];
s -> weight = 50;
s -> sex = SexMan;
s -> birthday = {2011,9,10};
s -> favColor = ColorBlack;
[s eat];
[s run];
return 0;
}
0 0
- 认识object-c第一天
- Object-C 学习第一天
- ios 学习第一天 object-c入门
- 第一天:认识CPLEX
- Objective - C基础: 第一天 - 3.BOOL基本认识
- object-c 学习第一天(windows编译环境配置)
- object-c 学习(第一天)经典 hello word!
- (第一天)object-c枚举和结构体
- 第一天(Ant的认识)
- 第一天先来认识认识namespace
- Objective - C基础: 第一天 - 11.OC方法的基本认识
- Objective - C基础: 第一天 - 13.匿名对象的基本认识
- C#学习第一天
- C第一天
- c学习第一天
- 学习c第一天
- Objective-C 第一天
- Objective-C 第一天
- 其实你也可以制作一款专属的书架app,信不信看看就知道
- Codeforces Round #266 (Div. 2)-D. Increase Sequence
- C/C++(7)字符串操作学习
- AndroidStudio下的jni开发细节
- Thinkphp5 多条件查询和聚合查询
- 认识object-c第一天
- html 预览-atom
- 稍微开始尝试写一下博客WWWW
- CUDA优化之Reducing
- Logstash学习--Filter
- Struts2 s2-033漏洞修复方案
- C#引用Office.word出错的解决办法
- 排序算法六 快速排序
- 关于thinkphp验证码的显示