Objective-C初探【2】之Classes, Objects, and Methods

Classes, Objects, and Methods

下面学习的Object-c中的类,对象以及方法。

Objective-c作为一个面向对象的语言,其中最基础的就是Object.Object之名,就是对象,也就是一样东西。

就现实生活为例,我们的车子是一个对象,它有自己的属性(颜色、牌子等),自己的方法(启动,转左右等)。

言归正传,在OC中,假如我们定义了一个对象为mycar,当我们需要使用其start和turn方法时,如下使用:

  无参数方法   带参数方法
Objective-c写法 [mycar start]; [mycar start:left];
java写法 mycar.start(); mycar.turn(left);

有了上诉的对象方法的调用方式,但是我们还少了对象方法的定义。Objective-c中是如何定义一个类呢?

需要通过两个关键的@interface和@implementation部分

interface和implementation共同代表一个类,两者的组合相当于java中的class。

下面用一个构造分数的类来说明一下:

@interface部分相当于接口部分,列出类中将会用到的方法(print、setNumerator,setDenominator)--- 制造了一个模具

-(void) print;             OC中方法会以“-”打头,表明这一个方法,紧跟后面(void) 代表的是返回值,最后就是方法的名称print。

-(void) setNumerator:(int)n;      当见方法中使用":",表明这个方法是带有参数的。(int)参数类型是整形,参数名称为n。

1 @interface Fraction: NSObject
2   -(void) print;
3   -(void) setNumerator: (int) n;
4   -(void) setDenominator: (int) d;
5 @end

@implementation部分相当于实现部分,重写了接口的所有的方法 -- 模具为模板,具体的实现

 1 @implementation Fraction
 2 {
 3   int numerator;
 4   int denominator;
 5 }
 6 –(void) print
 7 {
 8   NSLog (@"%i/%i", numerator, denominator);
 9 }
10 –(void) setNumerator: (int) n
11 {
12   numerator = n;
13 }
14 –(void) setDenominator: (int) d
15 {
16   denominator = d;
17 }

OC就是这样通过

[email protected]

[email protected]

3.[mycar start];

完成了方法的创建,以及调用。

以上主要参照Programming in Objective-C 6th Edition的内容,进入了个人的学习,如有错漏,烦请指正。

时间: 2024-10-29 19:06:24

Objective-C初探【2】之Classes, Objects, and Methods的相关文章

Grid (read-only) objects and methods (client-side reference)获取子表单对象的一些方法 Crm 2016

https://msdn.microsoft.com/en-us/library/dn932126.aspx#BKMK_GridControl Updated: November 29, 2016 Applies To: Dynamics 365 (online), Dynamics 365 (on-premises), Dynamics CRM 2016, Dynamics CRM Online You can set event handlers to execute scripts whe

C++ vs Objective C

oc Short list of some of the major differences: C++ allows multiple inheritance, Objective-C doesn't.一个允许多继承,一个不允许Unlike C++, Objective-C allows method parameters to be named and the method signature includes only the names and types of the parameter

iOS Developer Libray (中文版)-- Defining Classes 定义类

该篇是我自己学习iOS开发时阅读文档时随手记下的翻译,有些地方不是很准确,但是意思还是对的,毕竟我英语也不是很好,很多句子无法做到准确的字词翻译,大家可以当做参考,有错误欢迎指出,以后我会尽力翻译的更好,大家一起努力共同进入,有兴趣的同学可以一起学习. 注:部分图片没有上传,可以点我下载源文件: Defining Classes 定义类 When you write software for OS X or iOS, most of your time is spent working with

Pointers to classes (From the note of my firend)

 Pointers to classes Objects can also be pointed to by pointers: Once declared, a class becomes a valid type, so it can be used as the type pointed to by a pointer. For example:   Rectangle * prect;   is a pointer to an object of class Rectangle. S

Ignoring unused library classes...java.io.IOException: You have to specify '-keep' options for the s

执行Maven Install打包的时候,出现以下错误信息: Ignoring unused library classes-java.io.IOException: You have to specify '-keep' options for the shrinking step. [proguard] Ignoring unused library classes...java.io.IOException: You have to specify '-keep' options for

Java性能提示(全)

http://www.onjava.com/pub/a/onjava/2001/05/30/optimization.htmlComparing the performance of LinkedLists and ArrayLists (and Vectors) (Page last updated May 2001, Added 2001-06-18, Author Jack Shirazi, Publisher OnJava). Tips: ArrayList is faster than

Learning JavaScript Design Patterns -- A book by Addy Osmani

Learning JavaScript Design Patterns A book by Addy Osmani Volume 1.6.2 Tweet Copyright © Addy Osmani 2015. Learning JavaScript Design Patterns is released under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 unported license. It

(转)Awesome Courses

Awesome Courses  Introduction There is a lot of hidden treasure lying within university pages scattered across the internet. This list is an attempt to bring to light those awesome courses which make their high-quality material i.e. assignments, lect

CSE210 Advanced Object Oriented Programming

CSE210 Advanced Object Oriented ProgrammingCoursework 2019 Release date: 11th, Mar, 2019Deadline: 12:00PM, 23rd, Apr, 2019 1. DescriptionThe objective of the coursework is to develop a practical application for data processing, analysis and content s