Introduction to Objects

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值