继续
来自: lopper(FREE TIME) 2010-03-18 21:32:16
When you inherit you create a new type, and the 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. This type equivalence via inheritance is one of the fundamental gateways in understanding the meaning of object-oriented programming.
当你因为继承而创造一个新类型时,新的类型不只是包含已经存在类型的所有成员(虽然private的那些成员是被隐藏和不可达的),更重要的是它复制了旧类的接口。这意味着,你可以对基类发送的消息都可以发送给衍生类。因为我们可以通过能给类发送的消息知道这个类,所以这意味着衍生类和基类属于同种类型。这种通过继承实现类型等价是你理解面向对象编程的一个基础。
Since both the base class and derived class have the same interface, there must be some implementation to go along with that interface. That is , there must be a method to execute when an object receives a particular message. If you simply inherit a class and don’t do anything else, the methods from the base-class interface come right along into the derived class. That means objects of the derived class have not only the same type, they also have the same behavior, which doesn’t seem particularly interesting.
因为基类和衍生类拥有相同的接口,因此这个接口必须要有必要的实现。这个意思是,当一个类接受到一个特定的消息时,它必须有一个方法去处理。如果你只是简单的继承一个类而不做其他的事情,那么这个基类的方法便会顺延到衍生类中。这意味着基类和衍生类拥有相同的类型和行为,这是没有意义的。
页47
You have two ways to differentiate your new derived class from the original base class it inherits from. The first is quite straightforward: you simply add brand new functions to the derived class. These new functions are not part of the base class interface. This means that the base class simply didn’t do as much as you wanted it to, so you add more functions. This simple and primitive use for inheritance is, at times, the perfect solution to your problem. However, you should look closely for the possibility that your base class might need these additional functions.
你有两种方法去区分衍生类和它所继承的原始基类:你可以简单的为衍生类加入标识性的方法。这些新方法不是基类接口的一部分。这意味着基类不能完成你所要求的全部功能,所以你在衍生类中新增了方法。这种简单而原始的利用继承的方式有时可能是最有效的解决方案。但是你需要准确的考虑你的地基类是否也需要这些附加方法。
Overriding base-class functionality
覆盖基类的方法
Although the extends keyword implies that you are going to add new functions to the interface, that’s not necessarily true. The second way to differentiate your new class is to change the behavior of an existing base-class function. This is referred to as overriding that function.
虽然extends关键词表示的意思是你将为这个基类的接口添加新的方法,但这并不尽然。第二种区分新类的方法就是去改变基类的已存在的行为(处理方法)。这被称为“覆盖”了旧方法。
To override a function, you simply create a new definition for the function in the derived class. You’re saying “I’m using the same interface function here, but I want it to do something different for my new type.”
去覆盖一个方法, 你可以为衍生类创造一个该方法的新的定义。好比你在说“我正在使用一个相同的接口函数,不过我希望它能为我的新类型做一些不同的工作”。
Is-a vs. is-like-a relationships
Is-a和is-like-a的关系
There’s a certain debate that can occur about inheritance: Should inheritance override only base-class functions ? This means that the derived type is exactly the same type as the base class since it has exactly the same interface. As a result, you can exactly substitute an object of the derived class for an object of the base class. This can be thought of as pure substitution. In a sense, this is the ideal way to treat inheritance. We often refer to the relationship between the base class and the derived classes in this case as an is-a relationship, because you can say “a circle is a shape.” A test for inheritance is whether you can state the is-a relationship about the classes and have it make sense.
有一些特定的讨论是针对继承的:是否继承覆盖仅仅是基类的方法?(不是很懂)这意味着衍生类型和基类是相同的类型因为他们有着相同的接口。结果是你可以用衍生类的对象替代基类的对象。这种方式的替代被认为是纯替代。某种意义上而言,这是一种对继承很好的使用。我们通常把这种基类和衍生类的关系称作is-a关系,因为你可以说“圆是一种形状”。一种对于继承的测试就是你是否可以说明一些类是is-a关系,并且能讲得通。
There are times when you must add new interface elements to a derived type, thus extending the interface and creating a new type. The new type can still be substituted for the base type, but the substitution isn’t perfect in a sense because your new functions are not accessible from the base type. This can be described as an is-like-a relationship; the new type has the interface of the old type but it also contains other functions, so you can’t really say it’s exactly the same. For example, consider an air conditioner. Suppose your house is wired with all controls for cooling; that is , it has an interface that allows you to control cooling. Imagine that the air conditioner breaks down and replace it with a heat pump, which can both heat and cool. The heat pump is-like-an air conditioner, but it can do more. Because your house is wired only to control cooling, it is restricted to communication with the cooling part of the new object. The interface of the new object has been extended, and the existing system do know about anything except the original interface.
有些时候,你必须去为衍生类增加新的接口元素,这扩展了接口并且创造了一个新的类型。新的类型仍然可以替换旧类型,但是这种替换并不完美,因为新的方法并不能在基类中得到。这种关系可以被描述为is-like-a;新类型包含接口并且含有一些新的方法,所以严格来说两者并不相同。举个例子,考虑一个空调。假设你的房子所有的线路都是为了制冷而设置的。所以,这里有一个制冷的接口允许你去控制冷度。打个比方说空调损坏并被换成了一个热泵,它既可以制冷又可以制热。热泵像一个空调,但是它能做的更多。因为你的房子已经按照制冷的模式布线,所以它只能和新对象制冷的部分通信,新对象的接口已经被扩展,但是旧系统只知道已经存在过的那部分。
你的回应
回应请先 登录 , 或 注册最新讨论 ( 更多 )
- 继续 (lopper)
- 终于开太阳了。 (lopper)
- 这几天感冒了,好好休息。 (lopper)
- 小组里好安静啊,是不是考虑年后要FB了。 (lopper)
- 继续,.....已经补了两天班了,同志们明天也要上班了吧. (lopper)