Chapter8. Objective-C learning note 1.

Object: an object is a thing. 

Instance: a specific allocation of a class. a unique occurrence of a class is an instance. In object-oriented parlance, your particular car is an instance of a car. 所以每次生产一辆汽车,一个新的instance 来自 cars 这个 class就被创建,each instance of the car 被称为 an object.

Method: a unique occurrence of a class is an instance, and the actions that are performed on the instance are called methods. In some cases, a method can be applied to an instance of the class or to the class itself. Applying a method to an object can affect the state of that object. The key concepts here are that objects are unique representations from a class, and each object contains some information (data) that is typically private to that object. The methods provide the means of accessing and changing that data.

[ClassOrInstance method];

When you ask a class or an instance to perform some action, you say that you are sending it a message; the recipient of that message is called the receiver. So another way to look at the general format described previously is as follows: 

[receiver message];

[receiver message: argument]

[receiver message: arg1 andArg:arg2]


Class: defines the grouping of data and code, the "type" of an object.

Instance Variable (or "ivar"): a specific piece of data belonging to an object. An instance method can always directly access its instance variables. A class method can't, because it's dealing only with the class itself, not with any instances of the class.


当我们想要获取我们的instance variables 从其他地方,我们不能直接获取因为它们是隐藏的。

The fact that they are hidden from you is a key concept called data encapsulation.

Encapsulation: keep implementation private and separate from interface. It enables someone writing class definitions to extend and modify the class definitions, without worrying about whether programmers are thinking with the internal details of the class. Data encapsulation provides a nice layer of insulation between the programmer and the class developer.

Inheritance: hierarchical organization, share code, customize or extend behaviors. Subclass inherit behavior and data from superclass. Subclasses can use, augment or replace superclass methods.

Protocol: a protocol is a collection of methods grouped together. Indicates that a class implements a protocol, similar to interfaces in java.


NSObject: it is the root class of most Objective-C class hierarchies. Through NSObject, objects inherit a basic interface to the runtime system and the ability to behave as Objective-C objects.


How to create a new class in obj-C?

A program is logically divided into three sections:

@interface section;  @implementation section;  program section.

The @interface section describes the class and its methods, it is in .h file.

The @implementation section describes the data (the instance variables that objects from the class will store) and contains the actual code that implements the methods declared in the interface section. It is in .m file.

The program section contains the program code to carry out the intended purpose of the program.


When you define a new class, you have to tell the Objective-C compiler where the class came from. That is, you have to name its parent class. Next, you need to define the type of operations, or methods, that can be used when working with objects from this class. 

@interface NewclassName: ParentClassName

       propertyAndMethodDeclarations;

@end


The @implementation section contains the actual code for the methods you declared in the @interface section. You have to specify what type of data is to be stored in the objects of this class. That is, you have to describe the data that members of the class will contain. These members are called instance variables.

@implementation NewClassName

{

memberDeclarations;

}

  methodDefinitions;

@end


The leading minus sign (-) tells the Objective-C compiler that the method is an instance method. The only other option is a plus sign (+), which indicates a class method. A class method is one that performs some operation on the class itself, such as creating a new instance of the class.

Instances respond to instance methods:

-(id) init;

-(float) height;

-(void) walk;

classes respond to class methods:

+(id) alloc;

+(id) person;

+(Person *)sharedPerson;


-(void) setNumerator: (int) n;

It is a instance method that return no value. The method takes an integer argument, which is indicated by the (int) in front of the argument name. "n" is the argument name. This name is arbitrary and is the name the method uses to refer to the argument. So the declaration of setNumerator specifies that one integer argument, called n, will be passed to the method and that no value will be returned. 


Fraction *myFraction;

This line says that myFraction is an object of type Fraction; that is, myFraction is used to store values from your new Fraction class. The asterisk that precedes the variable name is described in more detail below.

myFraction = [Fraction alloc]; allocate memory storage space for a new fraction.

myFraction = [myFraction init];  The init method initializes the instance of a class.

myFraction = [[Fraction alloc] init];


Automatic Reference Counting or ARC : programmers can rely on the system to take care of releasing memory as necessary.


In Objective-C, classes and instances are both objects. Classes declare state and behavior.  State (data) is maintained using instance variables. Behavior is implemented using methods. Instance variables typically hidden (只可以通过用getter/ setter methods 获取).


Example:

Person *voter;  // Person 是一个class, voter 是这个class 的一个object.

-(void) castBallot;

[voter castBallot]; //赋给voter 一个值, 通过method castBallot

-(int) age;

int theAge = [voter age];

-(void) setAge: (int)age;

[voter setAge:21];

-(BOOL)canLegallyVote;

if ([voter canLegallyVote]){

    // do something voter

}

-(void) registerForState: (NSString *)state party: (NSString *) party;

[voter registerForState: @"CA" party: @"Independant"];

-(Person *) spouse;

-(NSString *)name;

NSString *name = [[voter spouse] name];


-(void) defineShape: (int) sides height: (int) myHeight width: (int) myWidth{

int tempSides = sides;

int tempHeight = myHeight;

int tempWidth = myWidth;

}

Shape *basicShape;

[basicShape defineShape: 4 height: 3 width: 2];


float height = [person height];

float height = person.height;

[person setHeight: newHeight];

person.height = newHeight;

[[person child] setHeight: newHeight];

person.child.height = newHeight;


Type id: 

The id data type is used to store an object of any type. In a sense, it is a generic object type. 

id graphicObject;

-(id) newObject: (int) type;


Objective-C provides compile-time, not runtime, type checking.










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值