您的位置:首页 > 移动开发 > Objective-C

Introduction to Objects

2013-11-18 19:33 357 查看
The progress of abstraction
1.  Everything is an object. Think of an object as a fancy variable;
it stores data, but you can “make requests” to that object, asking it to perform operations on itself. In theory, you can take any conceptual component in the problem you’re trying to solve (dogs, buildings, services, etc.) and represent it as an object in
your program. 

2.  A program is a bunch of objects telling each other what to do by sending messages.
To make a request of an object, you “send a message” to that object. More concretely, you can think of a message as a request to call a method that belongs to a particular object. 

3.  Each object has its own memory made up of other objects. Put
another way, you create a new kind of object by making a package containing existing objects. Thus, you can build omplexity into a program while hiding it behind the simplicity of objects. 

4.  Every object has a type. Using the parlance, each object
is an instance of a class, in which “class” is synonymous with “type.” The most important distinguishing characteristic of a class is “What messages can you send to it?” 

5.  All objects of a particular type can receive the same messages. This
is actually a loaded statement, as you will see later. Because an object of type “circle” is also an object of type “shape,” a circle is guaranteed to accept shape messages. This means you can write code that talks to shapes and automatically handle anything
that fits the description of a shape. This substitutability is one of the powerful concepts in OOP.
An object has an interface
An object provides services 
高聚合,每个对象只做好一件事情。
The hidden implementation
Java uses three explicit keywords to set the boundaries in a class: public, private, and protected. These
access specifiers determine who can use the definitions that follow. public means the following element is available to everyone. The private keyword, on
the other hand, means that no one can access that element except you, the creator of the type, inside methods of that type. private is a brick wall between you and the client programmer. Someone who tries
to access a private member will get a compile-time error. The protected keyword acts like private, with
the exception that an inheriting class has access to protected members, but not private members. Inheritance will be introduced shortly.  

Java also has a “default” access, which comes into play if you don’t use one of the aforementioned specifiers. This is usually called package access because classes can access the
members of other classes in the same package (library component), but outside of the package those same members appear to be private.  
Reusing the implementation
Because inheritance is so important in object-oriented programming, it is often highly emphasized, and the new programmer can get the idea that inheritance should be used everywhere. This
can result in awkward and overly complicated designs. Instead, you should first look to composition when creating new classes, since it is simpler and more flexible. If you take this approach, your designs will be cleaner. Once you’ve had some experience,
it will be reasonably obvious when you need inheritance.  
Inheritance 
When you inherit from an existing type, you create a new type. This new type contains not only all the members of the existing type (although the private ones
are hidden away and inaccessible), but more importantly it duplicates the interface of the base class. That is, all the messages you can send to objects of the base class you can also send to objects of the derived class. Since we know the type of a
class by the messages we can send to it, this means that the derived class is the same type as the base class. 
Interchangeable objects with polymorphism
The singly rooted hierarchy
Containers
Parameterized types (generics) 
For example, with a parameterized container, the compiler could customize that container so that

it would accept only Shapes and fetch only Shapes.
Object creation & lifetime
Java uses dynamic memory allocation, exclusively. Every time you want to create an object, you use the new operator to build a dynamic instance of that object. 
Exception handling: dealing with errors
It’s worth noting that exception handling isn’t an object-oriented feature, although in object-

oriented languages the exception is normally represented by an object. Exception handling

existed before object-oriented languages.  
Concurrent programming
Java and the Internet
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息