ObjectiveC类的使用
来源:互联网 发布:win7网络共享无法访问 编辑:程序博客网 时间:2024/04/30 13:46
基本使用
lijun.h
//// Header.h// example//// Created by junli on 15/4/3.// Copyright (c) 2015年 junli. All rights reserved.//#ifndef example_Header_h#define example_Header_h/* Title:demo class usage in objective C. author:kagula date:2015-04-04 precondition:familiar with visual c++. environment:xcode 6.2, Mac OS 10.10.2 note: [1]if you class no parent,must be derived from NSObject class. [2]the language only allow one parent class, but allow multi interface, here, class name interface, interface name protocol. [3]disallow friend keyword. [4]disallow operator redefine. frequently shortcut different from visual studio: cmd is win key: cmd+b build cmd+r run ctrl+cmd+up(down) switch to corresponding file cmd+alt+left fold code block cmd+alt+right expand code block alt+left skip to left word alt+right skip to right word more shortcuts you should looking for in the top menu. */@interface lijun:NSObject{ int privateMember;//default is private access mode.@protected int protectedMember;@public int publicMember; int publicMember2;<span style="font-family: Arial, Helvetica, sans-serif;">//member variable only can be used in interior or this level.</span> //if you want exterior access, need using property.}/* Objective C only have two type member function. one is normal, the other is static store type. */-(id)init:(int) a labelB:(int)b;-(void)func;-(int)funcWithReturnValue;-(int)func1:(int) x;-(int)func2:(int)x andY:(int) y;+(int)funcStatic;//this is static store type member function.@end#endif
lijun.m
//// lijun.m// example//// Created by junli on 15/4/4.// Copyright (c) 2015年 junli. All rights reserved.//#import <Foundation/Foundation.h>#import "lijun.h"@implementation lijun-(id)init{ if (self=[super init]) { privateMember = 10; protectedMember = 20; publicMember = 100; } NSLog(@"i am constructor,all your initialization should be here!"); return self;//self like this notation in c++.}-(id)init:(int) a labelB:(int)b{ if (self=[super init]) { privateMember = a; protectedMember = b; publicMember = a*b; } NSLog(@"i am constructor,all your initialization should be here!"); return self;//self like this notation in c++.}-(void)func{ NSLog(@"i am no return no argument function!");}-(int)funcWithReturnValue{ return privateMember;}-(int)func1:(int) x{ return x*10;}-(int)func2:(int)x andY:(int) y{ return x*y;}+(int)funcStatic{ NSLog(@"demo static member function invoke!"); return 0;}-(void)dealloc{ NSLog(@"i am destructor equality with c++!");}@end
main.m
//// main.m// example//// Created by junli on 15/4/3.// Copyright (c) 2015年 junli. All rights reserved.//#import <Foundation/Foundation.h>#import "lijun.h"void illustrateClassUsage(){ { lijun *obj = [[lijun alloc]init]; NSLog(@"demo invoke function without arguments[%d]",[obj funcWithReturnValue]); NSLog(@"demo invoke function with one arguments[%d]",[obj func1:123]); NSLog(@"demo invoke function with one arguments[%d]",[obj func2:1 andY:2]); NSLog(@"demo access public member of the object %d",obj->publicMember); //obj of lijun will be release in automatic. } { //initialization object with arguments lijun *obj = [[lijun alloc]init:10 labelB:2]; NSLog(@"demo access public member of the object %d",obj->publicMember); }}int main(int argc, const char * argv[]) { @autoreleasepool { illustrateClassUsage(); } //invoke static member function of lijun. [lijun funcStatic]; return 0;}
继承与多态
polymorphism.h
//// Polymorphism.h// example//// Created by junli on 15/4/7.// Copyright (c) 2015年 junli. All rights reserved.//#import <Foundation/Foundation.h>#import "Ancestor.h"//protocl as c++'s pure virtual abstract class!// as other like "C" language's interface.@protocol MouseListener-(BOOL)mousePressed;@optional//this indicator will suppress no impplement "mouseClicked" warning in xcode.-(BOOL)mouseClicked;//this method can not be implemented by class.@end@protocol KeyboardListener-(BOOL)keyPressed;@end/*[1]only allow one class(in objective C name is interface) to derived from, but you can have many interface(in objective C name is protocol).[2]no access qualifier to instrain parent interface and protocol.[3]no access qualifier for interface's member method. @interface Polymorphism : NSObject<MouseListener,KeyboardListener>if no Ancestor,should using NSObject instead, demo is above. */@interface Polymorphism : Ancestor<MouseListener,KeyboardListener>/* Here, do not need declare protocol's method,you can implement a method in protocol or not. */@end
polymorphism.m
//// Polymorphism.m// example//// Created by junli on 15/4/7.// Copyright (c) 2015年 junli. All rights reserved.//#import "Polymorphism.h"@implementation Polymorphism-(BOOL)mousePressed{ NSLog(@"Polymorphism mousePressed"); return TRUE;}-(BOOL)keyPressed{ NSLog(@"Polymorphism keyPressed"); return TRUE;}@end
ancestor.h
//// Ancestor.h// example//// Created by junli on 15/4/7.// Copyright (c) 2015年 junli. All rights reserved.//#import <Foundation/Foundation.h>@interface Ancestor : NSObject-(void)live;@end
ancestor.m
//// Ancestor.m// example//// Created by junli on 15/4/7.// Copyright (c) 2015年 junli. All rights reserved.//#import "Ancestor.h"@implementation Ancestor-(void)live{ NSLog(@"Ancestor is living inside!");}@end
how to use them?
void illustratePolymorphism(){ //invoke interface method Polymorphism *pol = [[Polymorphism alloc]init]; BOOL bR = [pol mousePressed]; NSLog(@"%@",bR?@"YES":@"NO"); //interface member will not be find. if([pol respondsToSelector:@selector(mouseClicked)]) { [pol mouseClicked]; } else { NSLog(@"the method mouseClicked is not implemented!"); } //invoke ancestor method [pol live]; //upcast pointer, and invoke it's member. Ancestor *pAncestor = (Ancestor*)pol; [pAncestor live]; //downcast pointer, and invoke it's member. Polymorphism *pPol = (Polymorphism*)pAncestor; [pPol live];}
in a interface, the method name can be same but argument list must be difference for compiler discriminate who is who.
0 0
- ObjectiveC类的使用
- ObjectiveC 类的声明和使用
- ObjectiveC 类的声明和使用
- ios反射调用类的方法(objectiveC)
- ios反射调用类的方法(objectiveC)
- ios反射调用类的方法(objectiveC)
- ios反射调用类的方法(objectiveC)
- ProtocolBuffer ObjectiveC配置使用
- objectiveC【语法】调用父类自己的方法
- ObjectiveC - 类的声明和实现(一)
- ObjectiveC - 类的声明和实现(二)
- ObjectiveC的变量类型的字符代表
- objectivec中nil和NULL的区别
- ObjectiveC 中同步调用WebService的方法
- 关于objectiveC中的delegate的理解
- ObjectiveC开发教程--字符串的连接
- 黑马程序员之ios学习总结——08 ObjectiveC语言的类和对象、类的三大特性
- ObjectiveC开发教程--字符串的基本操作处理方法
- Android中如何使用View填充在多个TextView之间使之等间距
- mvc思想
- 未能加载文件或程序集"System.Data,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089"或它的某一个依赖项。系统找
- nginx禁止访问某个后缀名的文件
- Linux 内核启动过程分析----zImage自解压
- ObjectiveC类的使用
- 第4周-项目3-用多文件组织多个类的程序-三角形类
- Hadoop学习之HBase的伪分布式安装
- P52第24题 完数问题
- hdu 1485 非常可乐 题解
- linux密码加密方式与暴力破解代码
- thinking in java 第二天
- 第四周上机项目1 三角形类的构造函数
- 第四周项目三用对象数组操作长方柱类