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的工程,并对不同方式搭建项目进行了比较 ...
随机推荐
- swift语言之多线程操作和操作队列(上)———坚持51天吃掉大象
欢迎有兴趣的朋友,参与我的美女同事发起的活动<51天吃掉大象>,该美女真的很疯狂,希望和大家一起坚持51天做一件事情,我加入这个队伍,希望坚持51天每天写一篇技术文章.关注她的微信公众号: ...
- 同是url参数传进来的值,String类型就用getAttribute获取不到,只能用getParameter获取,而int就两个都可以这是为什么?
这是因为int的属性是id,这是在被放到modeldriver中的user所具有的属性,传递过来的参数如果和user的属性重名,struts2的有类似beanutil之类的工具会自动封装参数,这时候用 ...
- scanf
scanf函数: (1)与printf函数一样,都被定义在头文件stdio.h里,因此在使用scanf函数时要加上#include <stdio.h>.它是格式输入函数,即按用户指定的格式 ...
- PAT 07-0 写出这个数
用拼音输出一个数字的各位数字之和,这个或许比上面的标题恰当.这里我第一次用到了sprintf()(stdio.h)这个函数,我本来是要找itoa()(stdlib.h)函数来着,查资料一看,说这个函数 ...
- new work
果不其然,还是电子工程师适合我.
- 需求分析(NABC)
团队开发需求分析 队长:郭庆樑 成员:林彦汝.张金 经过讨论,我们决定做一个基于Windows的小游戏——躲避小球. 把这个项目实现,组长强调有两点: 1.可实现:2.有用户. 可以说,我们最大的特点 ...
- linux基础命令学习(六)DHCP服务器配置
工作原理: 1.客户机寻找服务器:广播发送discover包,寻找dhcp服务器 2.服务器响应请求:单播发送offer包,对客户机做出响应.提供客户端网络相关的租约以供选 ...
- Eclipse卡死问题解决办法
偶尔浏览到几个eclipse卡死的文章,收集一下. 1. eclipse 3.6卡死 eclipse自动提示反应慢,或者卡死, 有人说这是eclipse 3.6的版本bug, 但是3.5版本好像也有 ...
- eclipse中导入一个android工程有The import android cannot be resolved错误怎么办
解决方法: 右键工程→Bulid Path→Configure Build Path... 选择Android,如图,在Project Build Target里面勾选相应的SDK即可 右键工程,pr ...
- 【LEETCODE OJ】Binary Tree Preorder Traversal
Problem Link: http://oj.leetcode.com/problems/binary-tree-preorder-traversal/ Even iterative solutio ...