oc10--练习
//
// 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--练习的更多相关文章
- oc总结
OC10天大纲 一.类和对象 1.什么是类? 同一种对象的抽象就是类. 2.什么是对象? 世界上的任何事物都可以称为对象,每个对象都有他自己的属性和行为. 3.如何创建一个类(请把一个.h和一个.m粘 ...
- oc界面开发整理
oc界面开发整理 ViewController.h from test82 #import <UIKit/UIKit.h> @interface ViewController : UIVi ...
随机推荐
- [你必须知道的.NET]目录导航
http://www.cnblogs.com/anytao/archive/2007/09/14/must_net_catalog.html
- Web Api跨域登录问题
最近项目第一次尝试使用web api,照搬了一般mvc的Forms登录方式,在和前端对接的时候出现一个问题: 前端使用ajax调用登录接口完成登录后,再调用别的接口,被判断为未登录. 如果直接在浏览器 ...
- Spring Boot (20) 拦截器
动态资源和静态资源 拦截器可以算是aop的一种实现,专门拦截对动态资源的后台请求,也就是拦截对控制层的请求,主要用于判断用户是否有权限请求后台.拦截器不会拦截静态资源,如spring boot默认静态 ...
- jquery对象与DOM对象的转化(简化版):
1:DOM对象 var apple = document.getElementById('apple'); 2:jquery对象 var _apple = $('#apple'); 3:DOM对象=& ...
- python--5、包
包 包,即一个包含__init__.py文件的文件夹,创建包的目的也就是为了用文件夹将文件(模块)组织起来.python3中,即使包里没有__init__.py文件,仍能import使用.而pytho ...
- 使用Sql Server Management Studio 2008将数据导出到Sql文件中
最近需要将一个Sql Server 2005数据库中的数据导出,为了方便,就希望能导出成Sql文件,里面包含的数据是由Insert 语句组成的. 在Sql Server Management St ...
- SAP computer之input and MAR
Input and MAR Below the program counter is the input and MAR block. It includes the address and data ...
- jQuery顺序加载图片(终版)
这一篇是对上一篇(jQuery顺序加载图片(初版)--http://www.cnblogs.com/newbie-cc/p/3707504.html)的改进. function loadImage(i ...
- dubbo之服务分组
当一个接口有多种实现时,可以用group区分. 服务 <dubbo:service group="feedback" interface="com.xxx.Inde ...
- andorid 查看OpenCv Mat的Debug信息
在进行Android调试时,不能再Console显示Debug信息,只能在LogCat上显示,显示信息如下图: 代码段: public void printMat2Txt(Mat ElemM, Str ...