Objective-C 的OOP(下)-类(static)方法、实例方法、overwrite(覆写)、属性(property)...

先来定义一个Human父类

定义部分:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//
// Human.h
// OOP
//
// Created by jimmy.yang on 11-2-9.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
 
#import <Foundation/Foundation.h>
 
 
@interface Human : NSObject {
BOOL sex;
}
 
+(void) toString;
 
-(void) showSex;
 
@end

注:+(void)前的加号,就表示这一个是类方法(static 方法),而-(void)表示这是一个实例方法

实现部分:

注意:下面的 -(id) init 即为构造函数.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//
// Human.m
// OOP
//
// Created by jimmy.yang on 11-2-9.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
 
#import "Human.h"
 
 
@implementation Human
 
//构造函数
-(id) init
{
NSLog(@"init() in Human is called");
sex = TRUE;
return(self);
}
 
//static类方法
+ (void)toString
{
NSLog(@"this is a class method of Human");
}
 
 
//实例方法
- (void)showSex
{
NSLog(@"my sex is %@",sex?@"MALE":@"FEMALE");
}
 
 
@end

再来定义一个Woman子类

定义部分:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//
// Woman.h
// OOP
//
// Created by jimmy.yang on 11-2-9.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
 
#import <Foundation/Foundation.h>
#import "Human.h"
 
 
@interface Woman : Human {
BOOL married;
}
 
-(void) canCook:(NSString*) foodName;
 
-(void) setMarried:(BOOL)m;
 
-(BOOL) Married;
 
@end

实现部分:

注意下面的:setMarried 与 Married 就是obj-C中属性的标准写法(当然以后还能看到其它简化的写法)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//
// Woman.m
// OOP
//
// Created by jimmy.yang on 11-2-9.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
 
#import "Woman.h"
 
 
@implementation Woman
 
//Woman类的构造函数
-(id) init{
NSLog(@"init() in Woman is called!");
if (self==[super init]){
sex = FALSE;
married = FALSE;
}
return (self);
}
 
//overwrite父类中的toString()
+(void)toString
{
NSLog(@"This is Woman's ToString()");
}
 
//Woman能做饭
-(void)canCook:(NSString*) foodName
{
NSLog(@"I can cook %@",foodName);
}
 
//属性的setter
-(void) setMarried:(BOOL)m
{
married = m;
}
 
//属性的getter
-(BOOL) Married
{
return married;
}
 
@end

main方法中的调用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#import <Foundation/Foundation.h>
#import "Human.h"
#import "Woman.h"
 
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 
// insert code here...
NSLog(@"Hello, World!");
 
//调用类的“静态”方法
[Human toString];
 
NSLog(@"----------------");
 
//创造一个Human的实例
Human *man = [Human new];
 
//调用man的showSex方法
[man showSex];
 
NSLog(@"----------------");
 
//定义一个Woman子类的实例
Woman *wife = [Woman new];
[wife canCook:@"Rice"];
 
//调用继承自父类的方法
[wife showSex];
 
//设置属性
[wife setMarried:TRUE];
 
//读取属性值
NSLog(@"wife's married = %@",wife.Married==NO?@"FALSE":@"TRUE");
 
NSLog(@"----------------");
 
//调用overwrite后的toString方法
[Woman toString];
 
 
//Factory模式中常用的手法,在这里依然适用(只不过编译时会有警告 'Human' may not respond to '-canCook:')
Human *wife2 = [Woman new];
[wife2 canCook:@"soap"];
 
 
 
NSLog(@"----------------");
 
[pool drain];
return 0;
}

运行结果:

2011-02-09 17:01:02.016 OOP[1725:a0f] Hello, World!
2011-02-09 17:01:02.053 OOP[1725:a0f] this is a class method of Human
2011-02-09 17:01:02.062 OOP[1725:a0f] —————-
2011-02-09 17:01:02.075 OOP[1725:a0f] init() in Human is called
2011-02-09 17:01:02.091 OOP[1725:a0f] my sex is MALE
2011-02-09 17:01:02.094 OOP[1725:a0f] —————-
2011-02-09 17:01:02.099 OOP[1725:a0f] init() in Woman is called!
2011-02-09 17:01:02.104 OOP[1725:a0f] init() in Human is called
2011-02-09 17:01:02.105 OOP[1725:a0f] I can cook Rice
2011-02-09 17:01:02.108 OOP[1725:a0f] my sex is FEMALE
2011-02-09 17:01:02.109 OOP[1725:a0f] wife’s married = TRUE
2011-02-09 17:01:02.111 OOP[1725:a0f] —————-
2011-02-09 17:01:02.116 OOP[1725:a0f] This is Woman’s ToString()
2011-02-09 17:01:02.120 OOP[1725:a0f] init() in Woman is called!
2011-02-09 17:01:02.121 OOP[1725:a0f] init() in Human is called
2011-02-09 17:01:02.123 OOP[1725:a0f] I can cook soap
2011-02-09 17:01:02.125 OOP[1725:a0f] —————-

转载于:https://www.cnblogs.com/lm3515/archive/2011/04/08/2009825.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/435117.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

介绍最全的LVS负载均衡技术

LVS集群采用IP负载均衡技术和基于内容请求分发技术。调度器具有很好的吞吐率&#xff0c;将请求均衡地转移到不同的服务器上执行&#xff0c;且调度器自动屏蔽掉服务器的故障&#xff0c;从而将一组服务器构成一个高性能的、高可用的虚拟服务器。整个服务器集群的结构对客户是透…

linux 版本_Linux动态库版本号作用机制

熟悉linux的都知道&#xff0c;linux的动态库如果有版本号的话&#xff0c;一般是libsample.so.x.y.z这样的名字。x.y.z对应的就是major.minor.release(即主.次.发布)版本号Major&#xff1a;大版本更新&#xff0c;前后都不兼容(比如移除api&#xff0c;又新增api)Minor&#…

信息系统项目管理系列之九:项目质量管理

信息系统项目管理系列之九&#xff1a;项目质量管理 信息系统项目管理系列之九&#xff1a; 项目质量管理 发布日期&#xff1a;2011年4月9日 星期六 作者:EricHu 说明&#xff1a;本文属作者的读书笔记。 勤能补拙、笨鸟先飞。 本篇主要内容如下&#xff1a; 项目质量管…

arcgis在面内创建随机点

在数据管理工具下选择要素类&#xff0c;在要素类下选择创建随机点

python tkinter listbox_Python3 tkinter基础 Listbox for+insert 将list中元素导入listbox中

Python : 3.7.0OS : Ubuntu 18.04.1 LTSIDE : PyCharm 2018.2.4Conda : 4.5.11typesetting : Markdowncode"""Author : 行初心Date : 18-10-1Blog : www.cnblogs.com/xingchuxinGitee : gitee.com/zhichengjiu"""from tkinter import *def main(…

Apple`s Steve Jobs Has Reshaped(重塑) the Tech World: 10 Ways He Did It

A new book is in the works that promises to shed(除掉, 使脱落, 使流出, 使清晰) more light on Apple. Dubbed(命名的, 配音的, 译制的) “iSteve: The Book of Jobs,” the book, which is written by Walter Isaacson, is an authorized biography of Apple co-founder S…

python中osgeo库使用教程链接

1. 地理数据处理软件包GDAL简介 2. GDAL python教程&#xff08;1&#xff09;——用OGR读写矢量数据 3. GDAL python教程&#xff08;2&#xff09;——几何形状geometry与投影projection4. GDAL python教程&#xff08;3&#xff09;——过滤器&#xff0c;简单的空间分析&…

TrueFFS原理

TrueFFS原理 引用TrueFFS原理及其在CF卡上的实现的部分内容 “1、Wear-Leveling 闪速存储器不能无限次重复使用。它的每个扇区的擦除次数虽然很大&#xff0c;但却有限&#xff1b;因此&#xff0c;随着使用次数的加长&#xff0c;它最终会变成只读状态&#xff0c;所以应该尽…

winform 判断控件有没有被遮挡_编程入门基础之 winform(2)

在编程入门1中&#xff0c;我们通过WINFORM等控件画出了登录界面&#xff0c;我们今天写登录界面的登录按钮事件。装修预算小程序登录按钮事件在上图中&#xff0c;我们看到了&#xff0c;有几个判断 &#xff0c;在C#中&#xff0c;if ,else是作为条件判断语句来使用。条件判断…

利用python读取点矢量对应栅格值

每行代码都有详细注解 所需库 # GDAL是栅格和矢量地理空间数据格式的转换器库 # 旧版本加载库的方法 #import gdal,gdalconst # from 语句让你从模块中导入一个指定的部分到当前命名空间中 # GDAL数据驱动,与OGR数据驱动类似 gdal是读取栅格数据 ogr是读取矢量数据 # 查看支持…

在CF卡上实现TrueFFS

CF卡是一种基于Flash技术的容量大、携带方便的存储介质&#xff0c;已在嵌入式系统等领域得到广泛的应用&#xff1b;但是&#xff0c;有限的擦写次数极大地限制了CF卡的使用寿命。TrueFFS通过一系列算法&#xff0c;能够延长CF卡的使用寿命&#xff0c;提高CF卡的使用效率。文…

python中安装opencv一直说不是内部或外部文件_Window系统下Python如何安装OpenCV库

关于OpenCV简介OpenCV是一个基于BSD许可(开源)发行的跨平台计算机视觉库&#xff0c;可以运行在Linux、Windows、Android和Mac OS操作系统上。它轻量级而且高效——由一系列 C 函数和少量 C 类构成&#xff0c;同时提供了Python、Ruby、MATLAB等语言的接口&#xff0c;实现了图…

解决input设置背景后,在ie7下浏览内容过长背景跟着滚动

先发发牢骚&#xff0c;万恶的IE啊。这么点问题害我走弯路&#xff0c;浪费时间&#xff0c;浪费生命。本以为加上background-attachment:fixed;问题就没了&#xff0c;结果问题依然存在&#xff0c;查资料说好像是ie bug。无语&#xff0c;天杀的IE&#xff0c;谁叫人家IE是老…

python打开文件中文名_windows下Python打开包含中文路径名文件

windows使用gbx(gb2312,gbk,gb18030我也不知道是哪个)对文件名及文件路径进行编码保存。打开文件的函数中使用诸如open(filename.encode(‘gbk‘))可以很好的解决。#coding:utf8if __name__ ‘__main__‘:srcfile r"D:/测试路径/测试文件.txt"f open(srcfile.deco…

利用python读取栅格数据

代码具有详细说明注释 所需库 #gdalconst中的常量都加了前缀&#xff0c;力图与其他的module冲突最小 from osgeo import gdal,gdalconst from osgeo import gdal_array as ga # 用于引入一个模块的同时为该模块取一个别名 from osgeo.gdalconst import GA_ReadOnly# 开始对栅…

uC/GUI 在Cortex-M3 内核上的移植

屈环宇--嘉兴学院一、简介是一种专为嵌入式系统设计的图形界面支持系统。它的代码全部由标准C编写&#xff0c;模块化的设计&#xff0c;具有很强的可移植性。uC/GUI 适应大多数的黑白或彩色LCD 的应用&#xff0c;还提供一个可扩展的2D 图形库及占用极少RAM 的窗口管理体系。二…

从零开始破解WEP、WPA无线网络

以前出差时在机场用过无线网络&#xff0c;小区内是否也有无线网络呢&#xff1f;随便一搜&#xff0c;果然有几个无线网络信号&#xff0c;于是打起了免费蹭网的主意&#xff0c;但信号最好的几个网络的WEP或WPA密码成为了一个门坎&#xff0c;于是在公司上网查到相关资料&…

python读取栅格gdal库下载链接

gdal下载链接 安装 再下载后的文件所在文件下的路径 在此输入cmd

python中回车怎么表示_如何在python中使用读取行仅拆分回车符?

I have a text file that contains both \n and \r\n end-of-line markers. I want to split only on \r\n, but cant figure out a way to do this with pythons readlines method. Is there a simple workaround for this?解决方案As eskaev mentions, youll usually want t…