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关键字的更多相关文章

  1. CoreData教程

    网上关于CoreData的教程能搜到不少,但很多都是点到即止,真正实用的部分都没有讲到,而基本不需要的地方又讲了太多,所以我打算根据我的使用情况写这么一篇实用教程.内容将包括:创建entity.创建r ...

  2. Automake

    Automake是用来根据Makefile.am生成Makefile.in的工具 标准Makefile目标 'make all' Build programs, libraries, document ...

  3. 作为一个新手的Oracle(DBA)学习笔记【转】

    一.Oracle的使用 1).启动 *DQL:数据查询语言 *DML:数据操作语言 *DDL:数据定义语言 DCL:数据控制语言 TPL:事务处理语言 CCL:指针控制语言 1.登录 Win+R—cm ...

  4. Objective C ARC 使用及原理

    手把手教你ARC ,里面介绍了ARC的一些特性, 还有将非ARC工程转换成ARC工程的方法 ARC 苹果官方文档 下面用我自己的话介绍一下ARC,并将看文档过程中的疑问和答案写下来.下面有些是翻译,但 ...

  5. Objective -C学习笔记之字典

    //字典:(关键字 值) // NSArray *array = [NSArray array];//空数组 // NSDictionary *dictionary = [NSDictionary d ...

  6. Objective - C中属性和点语法的使用

    一.属性        属性是Objective—C 2.0定义的语法,为实例变量提供了setter.getter方法的默认实现能在一定程度上简化程序代码,并且增强实例变量的访问安全性         ...

  7. Qt for iOS,Qt 与Objective C混合编程

    项目设置 既然要聊 Qt 混合 OC 编程,首先要简单介绍一下 Objective C .我只有一句话:Go,问搜索引擎去.因为我所知实在有限,怕误导了您.当然如果您不怕,往下看吧. OC源文件介绍 ...

  8. iOS开发——新特性OC篇&Objective新特性

    Objective新特性 Overview 自 WWDC 2015 推出和开源 Swift 2.0 后,大家对 Swift 的热情又一次高涨起来,在羡慕创业公司的朋友们大谈 Swift 新特性的同时, ...

  9. Objective中的协议(Protocol)

    Objective中的协议(Protocol) 作用: 专门用来声明一大堆方法. (不能声明属性,也不能实现方法,只能用来写方法的声明). 只要某个类遵守了这个协议.就相当于拥有这个协议中的所有的方法 ...

  10. iOS完全自学手册——[三]Objective-C语言速成,利用Objective-C创建自己的对象

    1.前言 上一篇已经介绍了App Delegate.View Controller的基本概念,除此之外,分别利用storyboard和纯代码创建了第一个Xcode的工程,并对不同方式搭建项目进行了比较 ...

随机推荐

  1. IOS弹出视图 LewPopupViewController

    LewPopupViewController是一款IOS弹出视图软件.iOS 下的弹出视图.支持iPhone/iPad. 软件截图 使用方法 弹出视图 1 2 3 4 5 PopupView *vie ...

  2. jQuery 动态添加瀑布流

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. arguments.callee

    arguments.callee在哪个函数中运行,他就代表哪个函数,一般在匿名函数中.在匿名函数中有时需要自己调用自己,但是由于是匿名函数,没有名字,所以可以用arguments.callee来代替匿 ...

  4. iOS App上架流程(2016详细版

    http://www.jianshu.com/p/b1b77d804254 iOS App上传项目遇到的问题 http://www.jianshu.com/p/9195cd991fc7

  5. UE正则表达式查找和替换(将【,;】)替换为换行

  6. 原生javascript Ajax

    代码 1. IE5 ,IE6 使用ActiveXObject对象,   其余现代浏览器都支持XMLHttpRequest对象: function ajaxObject(){ var xmlhttp; ...

  7. bootstrap菜单完美解决---原创

    由于bootstrap的各方优点,偶的“点金项目细化分包管理平台”决定采用它.但在使用中遇到了一些问题,比如菜单的问题,这个菜单是用的一个JQuery的一个效果,点击后,所点击的链接处的class要加 ...

  8. python基础之模块之os模块

    os模块 os模块的作用: os,语义为操作系统,所以肯定就是操作系统相关的功能了,可以处理文件和目录这些我们日常手动需要做的操作,就比如说:显示当前目录下所有文件/删除某个文件/获取文件大小…… 另 ...

  9. MVC中Asp.Net管道(二)

    Asp.Net管道: 1.在工作进程w3wp.exe中,利用asp.net_isapi加载.NET运行时,6.0中引入了应用程序池的概念,一个工作进程对应的一个应用程序池.一个应用呢程序池可以加载一个 ...

  10. C++类的运用 和 三大函数

    在<数据结构与算法分析C++描述>一书中给出了三段代码,简单描述了C++类的接口.实现.与调用: #ifndef INTCELL_H_INCLUDED #define INTCELL_H_ ...