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.










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值