objective-c new关键字
xxx *a = [xxx new] 等价于 xxx *a = [[xxx alloc]init] ,但如果类的构造函数带参数就不能使用new了。
练习了下《Objective-C 基础教程》第3章代码。
Share.h 头文件
//
// Share.h
// SharesDemo01
//
// Created by apple on 13-7-15.
// Copyright (c) 2013年 FDStudio. All rights reserved.
//
#import <Foundation/Foundation.h>
typedef enum{
KCircle,
KRectangle,
KOblateSpheroid
} ShareType;
typedef enum{
KRedColor,
KGreenColor,
KBlueColor
} ShareColor;
typedef struct{
int x,y,width,height;
} ShareRect;
@interface Share : NSObject{
ShareColor fillColor;
ShareRect bounds;
}
-(void)setFillColor:(ShareColor)c;
-(void)setBounds:(ShareRect)rect;
-(NSString *) ColorName:(ShareColor) c;
-(void)draw;
@end //Share
@interface Circle : Share
@end //Circle
@interface Rectangle : Share
@end //Rectangle
Share.m 实现文件
//
// Share.m
// SharesDemo01
//
// Created by apple on 13-7-15.
// Copyright (c) 2013年 FDStudio. All rights reserved.
//
#import "Share.h"
@implementation Share
-(void)setFillColor:(ShareColor)c{
fillColor = c;
} //setFillColor
-(void)setBounds:(ShareRect)rect{
bounds = rect;
} //setBounds
-(NSString *)ColorName:(ShareColor)c{
switch (c) {
case KRedColor:
return @"red";
break;
case KBlueColor:
return @"blue";
break;
case KGreenColor:
return @"green";
break;
}
return @"no clue";
}
-(void)draw{
} //draw
@end //Share
@implementation Circle
-(void)draw{
NSLog(@"drawing a circle at (%d %d %d %d) in %@", bounds.x, bounds.y,bounds.width,bounds.height, [self ColorName:fillColor]);
}
@end //Circel
@implementation Rectangle
-(void)draw{
NSLog(@"drawing a Rectangle at (%d %d %d %d) in %@", bounds.x, bounds.y,bounds.width,bounds.height, [self ColorName:fillColor]);
}
@end
main.m 主文件
//
// main.m
// SharesDemo01
//
// Created by apple on 13-7-15.
// Copyright (c) 2013年 FDStudio. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "share.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
// insert code here...
id shares[2];
ShareRect rect0 = {0,0,10,30};
shares[0] = [Circle new];
[shares[0] setBounds:rect0];
[shares[0] setFillColor:KRedColor];
ShareRect rect1 = {30,40,50,60};
shares[1] = [Rectangle new];
[shares[1] setBounds:rect1];
[shares[1] setFillColor:KBlueColor];
for(int i=0; i<2; i++){
[shares[i] draw];
}
}
return 0;
}
objective-c new关键字的更多相关文章
- CoreData教程
网上关于CoreData的教程能搜到不少,但很多都是点到即止,真正实用的部分都没有讲到,而基本不需要的地方又讲了太多,所以我打算根据我的使用情况写这么一篇实用教程.内容将包括:创建entity.创建r ...
- Automake
Automake是用来根据Makefile.am生成Makefile.in的工具 标准Makefile目标 'make all' Build programs, libraries, document ...
- 作为一个新手的Oracle(DBA)学习笔记【转】
一.Oracle的使用 1).启动 *DQL:数据查询语言 *DML:数据操作语言 *DDL:数据定义语言 DCL:数据控制语言 TPL:事务处理语言 CCL:指针控制语言 1.登录 Win+R—cm ...
- Objective C ARC 使用及原理
手把手教你ARC ,里面介绍了ARC的一些特性, 还有将非ARC工程转换成ARC工程的方法 ARC 苹果官方文档 下面用我自己的话介绍一下ARC,并将看文档过程中的疑问和答案写下来.下面有些是翻译,但 ...
- Objective -C学习笔记之字典
//字典:(关键字 值) // NSArray *array = [NSArray array];//空数组 // NSDictionary *dictionary = [NSDictionary d ...
- Objective - C中属性和点语法的使用
一.属性 属性是Objective—C 2.0定义的语法,为实例变量提供了setter.getter方法的默认实现能在一定程度上简化程序代码,并且增强实例变量的访问安全性 ...
- Qt for iOS,Qt 与Objective C混合编程
项目设置 既然要聊 Qt 混合 OC 编程,首先要简单介绍一下 Objective C .我只有一句话:Go,问搜索引擎去.因为我所知实在有限,怕误导了您.当然如果您不怕,往下看吧. OC源文件介绍 ...
- iOS开发——新特性OC篇&Objective新特性
Objective新特性 Overview 自 WWDC 2015 推出和开源 Swift 2.0 后,大家对 Swift 的热情又一次高涨起来,在羡慕创业公司的朋友们大谈 Swift 新特性的同时, ...
- Objective中的协议(Protocol)
Objective中的协议(Protocol) 作用: 专门用来声明一大堆方法. (不能声明属性,也不能实现方法,只能用来写方法的声明). 只要某个类遵守了这个协议.就相当于拥有这个协议中的所有的方法 ...
- iOS完全自学手册——[三]Objective-C语言速成,利用Objective-C创建自己的对象
1.前言 上一篇已经介绍了App Delegate.View Controller的基本概念,除此之外,分别利用storyboard和纯代码创建了第一个Xcode的工程,并对不同方式搭建项目进行了比较 ...
随机推荐
- 北大ACM题库习题分类与简介(转载)
在百度文库上找到的,不知是哪位大牛整理的,真的很不错! zz题 目分类 Posted by fishhead at 2007-01-13 12:44:58.0 -------------------- ...
- fix eclipse gc overhead limit exceeded in mac
fix eclipse gc overhead limit exceeded: 在mac上找不到eclipse.ini文件编辑内存限制,在eclipse安装目录右击eclipse程序,选“显示包内容” ...
- a various of context
ContextWrapper.getApplicationContext():Return the context of the single, global Application object o ...
- UNIX 网络编程第三版
第五章p102: ps -t pts/6 -o pid,ppid,tty,stat,args,wchan 在我的系统上运行时出现:TTY not found linux发行版为mint17.1 改用 ...
- Intellij编译时报“java: System Java Compiler was not found in classpath”
问题如下: http://stackoverflow.com/questions/19889145/setting-up-intellij-12-idea-with-java-1-7-and-reso ...
- php5.2 连接 SQL Server2008
如果你见到下面这一段输出的话,那么你有福了!!!! Array ( [0] => Array ( [0] => IMSSP [SQLSTATE] => IMSSP [1] => ...
- 开源软件项目管理系统招设计/开发。。。。。Zend Framework2架构 svn://735.ikwb.com/pms
开源软件项目管理系统招设计/开发.....Zend Framework2架构svn://735.ikwb.com/pms
- HttpWebRequest 和HttpWebResponse总结
1. 总结 总结2 3. Code using System; using System.Collections.Generic; using System.Linq; using System.Te ...
- Codechef2015 May - Chef and Strings (后缀自动机)
用后缀自动机统计出出现1~n次的串的数量f[i] 对于ans[k]=sigma(f[i]*C(i,k)) i>=k ; mo=; ..maxn] of dword; nt:..maxn,'a'. ...
- session 和 cookie 的区别和联系
二者的定义: 当你在浏览网站的时候,WEB 服务器会先送一小小资料放在你的计算机上,Cookie 会帮你在网站上所打的文字或是一些选择,都纪录下来.当下次你再光临同一个网站,WEB 服务器会先看看有没 ...