//
// main.m
// 练习 #import <Foundation/Foundation.h> @interface Car : NSObject
{
@public
int wheels;
}
// 方法的声明必须写在类的声明中
- (void)run;
- (void)test;
@end
@implementation Car
- (void)test
{
NSLog(@"测试一下车子:%i", wheels);
}
// 方法不能使用函数来实现, 方法是方法, 函数是函数
// 方法属于一个类, 函数属于一个文件
//void run()
- (void)run
{
NSLog(@"%i个轮子的车跑起来了", wheels); // 不能在一个函数中访问类的成员变量
} // 方法的实现只能写在@implementation和@end之间
- (void)haha
{
NSLog(@"调用了haha");
}
@end int main()
{
Car *c = [Car new];
[c run];
// test(); // 方法不能当做函数来调用 , 对象方法只能用对象调用
[c test];
// haha();
[c haha]; return ;
} @interface Test : NSObject - (int)addNum1:(int)num1 andNum2:(int)num2; // 每个参数数据类型前面都必须写上: - (double)pi; // 没有参数就不要写: - (void)test; // 在OC方法中()就一个作用, 用来扩住数据类型
@end @implementation Test - (int)addNum1:(int)num1 andNum2:(int)num2
{
return num1 + num2;
} - (double)pi
{
return 3.14;
} - (void)test
{ }
@end int main()
{
return ;
} @interface Person : NSObject
{
@public
int age;
double height; // 成员变量不能在定义的时候进行初始化
}
- (void)study; // 方法只能写在{}外面 // 缺少@end
@end @implementation Person
- (void)study
{
NSLog(@"年龄为%d的人在学习", age);
}
@end int main()
{
// 地址只能使用指针保存
Person *p = [Person new];
p->age = ;
p->height = 1.78f;
[p study];
return ;
}
//
// main.m
// 练习2
//
// Created by xiaomage on 15/6/18.
// Copyright (c) 2015年 xiaomage. All rights reserved.
// #import <Foundation/Foundation.h>
@interface Person : NSObject
{
@public
int age;
double height;
}
- (void)print;
@end // int newAge = 10, double newHeight = 1.5
void test1(int newAge, double newHeight);
void test2(Person *newP);
void test3(Person *newP);
void test4(Person *newP); int main()
{
Person *p = [Person new];
p->age = ;
p->height = 1.5f; test1(p->age, p->height); // 10, 1.5
[p print]; // 10, 1.5 test2(p); // 指针, 地址
[p print]; // 20, 1.7 test3(p); // 指针, 地址
[p print]; // 20, 1.7 test4(p); // 指针, 地址
[p print]; // 60, 1.9 return ;
}
@implementation Person
- (void)print
{
NSLog(@"年龄=%d,身高=%f", age, height); // 10, 1.5
}
@end void test1(int newAge, double newHeight)
{
newAge = ;
newHeight = 1.6;
} // Person *newP = p;
void test2(Person *newP)
{
newP->age = ;
newP->height = 1.7;
} // Person *newP = p;
void test3(Person *newP)
{
Person *p2 = [Person new];
p2->age = ;
p2->height = 1.8;
newP = p2; newP->age = ;
} void test4(Person *newP)
{
Person *p2 = newP;
p2->age = ;
p2->height = 1.9;
newP->age = ;
}

oc10--练习的更多相关文章

  1. oc总结

    OC10天大纲 一.类和对象 1.什么是类? 同一种对象的抽象就是类. 2.什么是对象? 世界上的任何事物都可以称为对象,每个对象都有他自己的属性和行为. 3.如何创建一个类(请把一个.h和一个.m粘 ...

  2. oc界面开发整理

    oc界面开发整理 ViewController.h from test82 #import <UIKit/UIKit.h> @interface ViewController : UIVi ...

随机推荐

  1. Windows phone开发 页面布局之屏幕方向

    (博客部分内容参考Windows phone开发文档) Windows phone的屏幕方向是利用Windows phone设备的方向传感器提供的数据实现切换的. Windows Phone支持纵向和 ...

  2. SLAM: Inverse Depth Parametrization for Monocular SALM

    首语: 此文实现客观的评测了使线性化的反转深度的效果.整篇只在表明反转可以线性化,解决距离增加带来的增长问题,有多少优势--%! 我的天呢!我竟然完整得翻译了一遍. 使用标记点地图构建SLAM的方法, ...

  3. C 预处理程序指令(CPP)

    #include 文件 提供的东西 stdio 提供 FILE.stdin.stdout.stderr 和 fprintf() 函数系列 stdlib 提供 malloc().calloc()和 re ...

  4. javascript中的计算题

    一.js中的数据类型共六种: 值类型五种:Boolea   Number  String  Null  undefined 引用类型:Object ----三大引用类型:Object   Array  ...

  5. Django 框架入门

    1.创建虚拟环境.(如果你想在你的服务器中运行多个项目,那么装虚拟环境是最好的选择) pip install virtualenv pip install virtualenvwrapper 安装好后 ...

  6. Skyline Web 端数据浏览性能优化

    三维数据的效率一直是个瓶颈,特别是在Web端浏览一直是个问题,在IE内存限制1G的条件下,对于三维数据动不动几十G的数据量,这1G显得多么微不足道.虽然现在三维平台都是分级加载,或者在程序中采用数据分 ...

  7. 如何快速从数据库获取表属性编写JavaBean

    以前自己对子设计好的数据库将数据库中的表转换为JavaBean,自己还一个一个慢慢打效率真是低,还是老师比较聪明学习学习. 我用的数据库可视化工具是Navicat Premium.UltraEdit( ...

  8. Springmvc中ajax与jason应用

    Springmvc中ajax与jason应用 相关依赖包 json数据转换的jar包 jackson-annotations-2.5.4 jackson-core-2.5.4 jackson-data ...

  9. springboot框架嵌入netty

    1.pom.xml添加依赖 <dependency> <groupId>io.netty</groupId> <artifactId>netty-all ...

  10. touch:命令创建文件

    touch:创建空文件或改变文件的时间戳属性 [功能说明] touch命令有两个功能:一是创建新的空文件:二是改变已有文件的时间戳属性 [语法格式] touch [option] [file] tou ...