对象,类,消息 3

Classes
An object-oriented program is typically built from a variety of objects. A program based on the Cocoa frameworks  might use NSMatrix objects, NSWindow objects, NSDictionary objects, NSFont objects, NSText objects, and many others. Programs often use more than one object of the same kind or class—several NSArray objects

or NSWindow objects, for example.


In Objective-C, you define objects by defining their class. The class definition is a prototype for a kind of object; it declares the instance variables that become part of every member of the class, and it defines a set of methods   that all objects in the class can use.

类的定义是一种类型对象的原型,定义声明了实例变量,和一组方法.



The compiler creates just one accessible object for each class, a class object that knows how to build new  objects belonging to the class. (For this reason it’s traditionally called a factory object .) The class object is the   compiled version of the class; the objects it builds are instances of the class. The objects that do the main work   of your program are instances created by the class object at runtime.  

编译器只为每一个类创建一个可访问对象,这个对象知道怎么去创建一个从属与该类的新对象, 这个可访问对象也称为工厂对象.  工厂对象在运行时创建新对象.



All instances of a class have the same set of methods, and they all have a set of instance variables cut from the  same mold. Each object gets its own instance variables, but the methods are shared.

所有的新对象都共享相同的一组方法,而实例变量则是各自的空间.


By convention, class names begin with an uppercase letter (such as Rectangle); the names of instances   typically begin with a lowercase letter (such as myRectangle).

对象通常第一个字母大写,实例变量则第一个字母小写.


Inheritance

Class definitions are additive; each new class that you define is based on another class from which it inherits  methods and instance variables. The new class simply adds to or modifies what it inherits. It doesn’t need to  duplicate inherited code.

类的定义是累加的,每一个新的类都是基于其他的类,并继承起方法和实例变量,新类只是简单的增加与修改继承的而不需要重复的代码.


Inheritance links all classes together in a hierarchical tree with a single class at its root. When writing code  that is based upon the Foundation framework, that root class is typically NSObject. Every class (except a root class) has a superclass one step nearer the root, and any class (including a root class) can be the superclass
for any number of subclasses one step farther from the root. Figure 1-1 illustrates the hierarchy for a few of   the classes used in a drawing program.

继承通过一跟对象把所有的类组织成一继承数,当我们基于框架写代码时候,其根对象通常是NSObject.


Figure 1-1 Some drawing program classes


Figure 1-1 shows that the Square class is a subclass of the Rectangle class, the Rectangle class is a subclass  of Shape, Shape is a subclass of Graphic, and Graphic is a subclass of NSObject. Inheritance is cumulative (累积的). 

So a Square object has the methods and instance variables defined for Rectangle, Shape, Graphic, and  NSObject, as well as those defined specifically  for Square. This is simply to say that an object of type Square   isn’t only a square, it’s also a rectangle, a shape, a graphic, and an object of type NSObject.


The NSObject Class

NSObject is a root class, and so doesn’t have a superclass. It defines the basic framework for Objective-C objects and object interactions. It imparts to the classes and instances of classes that inherit from it the ability   to behave as objects and cooperate with the runtime system.

继承NSObject 就有跟运行时系统交换的特性.

Inheriting Instance Variables


When a class object creates a new instance, the new object contains not only the instance variables that were defined for its class but also the instance variables defined for its superclass and for its superclass’s superclass, all the way back to the root class. Thus, the isa instance variable defined in the NSObject class becomes part
of every object. isa connects each object to its class.

每一个继承的对象都有一份父类对象,直到根对象的实例的变量,

因此,没一个对象都有isa 变量,


Inheriting Methods
An object has access not only to the methods defined for its class but also to methods defined for its superclass, and for its superclass’s superclass, all the way back to the root of the hierarchy. For instance, a Square object  can use methods defined in the Rectangle, Shape, Graphic, and NSObject classes as well as methods
defined in its own class.
 跟实例变量一样,每一个对象继承了其父类对象所有方法,直到其根对象...


Overriding One Method with Another


There’s one useful exception to inheritance: When you define a new class, you can implement a new method  with the same name as one defined in a class farther up the hierarchy. The new method overrides the original;   instances of the new class perform it rather than the original, and subclasses of the new class inherit it rather
than the original.

新类(A)可以通过定义同名称的函数重写父类对象的方法..继承自A的新子类,则继承A的方法,,A父类的则不使用,,(个人意境:..有可能其方法是继承了的,只是多态性原里使用了新重写的版本..)


Although overriding a method blocks the original version from being inherited, other methods defined in the  new class can skip over the redefined method and find the original (see “Messages to self and super” (page43) to learn how).

继承自A的新子类还是能找到A父类的方法,所以上面的猜想是成立的,要不然怎么能调用到呢..


A redefined method can also incorporate the very method it overrides. When it does, the new method serves  only to refine or modify the method it overrides, rather than replace it outright. When several classes in the  hierarchy define the same method, but each new version incorporates the version it overrides, the
implementation of the method is effectively spread over all the classes.  (需要实践才知道具体表达什么)


Although a subclass can override inherited methods, it can’t override inherited instance variables. Because an object has memory allocated for every instance variable it inherits, you can’t override an inherited variable by  declaring a new one with the same name. If you try, the compiler will complain.

成员变量不能重写,否则编译出错..


Abstract Classes
Some classes are designed only or primarily so that other classes can inherit from them. These abstract classes group methods and instance variables that can be used by a number of subclasses into a common definition. The abstract class is typically incomplete by itself, but contains useful code that reduces the implementation
burden of its subclasses. (Because abstract classes must have subclasses to be useful, they’re sometimes alsocalled abstract superclasses.)

Unlike some other languages, Objective-C does not have syntax to mark classes as abstract, nor does it prevent you from creating an instance of an abstract class.

抽象类只是概念上的,,,没有语法支持,所以其实也是一个类,只是他的功能主要提供一组方法与变量,让其子类可以少写code

The NSObject class is the canonical (规范的) example of an abstract class in Cocoa. You never use instances of the  NSObject class in an application—it wouldn’t be good for anything; it would be a generic object with the   ability to do nothing in particular.


The NSView class, on the other hand, provides an example of an abstract class, instances of which you might  occasionally use directly.
Abstract classes often contain code that helps define the structure of an application. When you create subclasses   of these classes, instances of your new classes fit effortlessly into the application structure and work automatically   with other objects.




















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值