Talk on the comparison of interfaces and inheritance methods

xiaoxiao2021-03-06  58

When inheriting, it is mainly necessary to re-develop and have the features I need for the parent class without having to understand the details. However, many times, a subclass does not require all the features of the parent class, which may only need some of these features, but because of inheritance, all the features of the parent class have, needed and unwanted features are also available at the same time. . And those subclasses do not need to be used, sometimes even trying to avoid using features, which is the side effects of inheritance. In particular, it is easy to cause errors that are not easy to discover more in the OO language that allows multiple inheritance. Therefore, in the oo language, various provisions will be created to limit some methods in the subclass using the parent class. Just take the example of you, if the dog's owner just wants the dog to climb the lower tree, but don't want its tail, you can fly like a monkey, so that the owner can't help it. Then the dog's owner certainly will not have a monkey to inherit the dog. Design mode is more emphasis on interface. The monkey has two interfaces, one is climbing the tree, one is the tail. I only need my dog ​​climbing the tree now, but don't want it to sleep, then I just want my dog ​​to realize the climbing interface. At the same time, it will not bring the side effects of the tail presented on the monkey. This is the advantage of the interface. OO technology development has also been for many years. A very clear trend is less and less use, and the use of the interface is more and more wide. In fact, as long as the earliest class library in JDK is a little more than the class library that has recently added, it can feel obvious to the programming style of the OO technical field, from a lot of inheritance to almost everywhere Interface programming. Oh, the interface is not an alternative inheritance. For example, I am now going to climb the trees now. I don't need to know that the dog is going to climb the tree or the monkey to climb the tree. I sent a "can climb the tree" animal to climb. This animal that can climb the tree can be either a monkey or a dog. Is this not very flexible? Dog (climbing trees, biting) monkeys (climbing trees, tails) If I just meet the requirements of climbing trees, I just don't care if it is. If I have to climb the tree, I can bite people, then I can certainly choose a dog, or you can create an interface (climbing the tree), then let the dog implement (climb the bite) interface. Because I want to achieve my software, just achieve the function of my demand, I manage it? Is it a dog? Maybe the dog can, maybe the dog is not, maybe the dog can, and will not be. I don't care. I just need (climb the tree bite) interface. Maybe I have always used a dog to complete my climbing tree interface, but later I found another animal, such as a cat, which is more flexible than the dog, so I have replaced the dog with a cat. And do not need to be modified at all.

转载请注明原文地址:https://www.9cbs.com/read-84254.html

New Post(0)