什么是类继承的好例子

什么是类继承的好例子

本文介绍了什么是类继承的好例子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为面向对象语言编写文档,我想知道什么样的类将是继承的一个很好的例子。



一些常见的例子: / p>

  class Person {
}
class Employee extends Person {
}

目前我最喜欢的,但我不喜欢Person-> Employee,因为Employee并不完全看起来很有趣。 / p>

  class Bicycle {
}
class MountainBike延伸自行车{
}

我在一些,但是自行车应该具有什么属性并不是很明显。

  class Animal {
}
class Bird extends Animal {
}

与自行车相同。

  class A {
}
class B扩展A {
}

太抽象了。主要的问题是这样一个类将需要更多的抽象属性和方法。



有没有人有一个更简单的类层次结构的例子?

解决方案

由于多种原因,Animal类是类继承的典型示例。



首先,有明显的方法来扩展潜在的动物类。您可能从哺乳动物,鸟类,甲壳类动物等子类开始。



一些类,如哺乳动物,将通过添加相当的属性来扩展Animal明显的(暖血等)。



其他更有问题的是,在开发类层次结构中相当常见的问题是非常明显的,如果您用动物继承来说明 - 这是一个为了解释的目的。鸟飞,对吧?那么,不是所有的鸟...所以,你怎么代表飞行?当然,有一些经典的解决方案和大量关于如何解决每个解决方案引入的问题和权衡的讨论信息。



因此,我强烈建议使用动物为例,因为可用信息和示例的丰富性。


I'm writing documentation for an object-oriented language, and I wonder what kind of classes would be a good example for inheritance.

Some common examples:

class Person {
}
class Employee extends Person {
}

Currently my favorite, but I don't like Person->Employee because 'Employee' does not exactly look like fun.

class Bicycle {
}
class MountainBike extends Bicycle {
}

I found this in some Java tutorial, but it's not really obvious what attributes a bike should have.

class Animal {
}
class Bird extends Animal {
}

Same as the bicycle.

class A {
}
class B extends A {
}

Too abstract. The main problem is that such a class will need even more abstract attributes and methods.

Does anyone have a better example for a simple class hierarchy?

解决方案

The Animal class is the classic example of class inheritance for a number of reasons.

First, there are obvious ways to extend the underlying animal class. You'll likely start with sub-classes such as Mammal, Bird, Crustacean, etc.

Some classes, such as Mammal, will extend Animal by adding attributes that are fairly obvious ("Warm-Blooded", etc.).

Other, more problematic, issues that are quite common in developing a class hierarchy are quite obvious if you illustrate with animal inheritance - and this is a good thing for purposes of explanation. Birds fly, right? Well, not all birds... So, how do you represent flying? There are, of course, classic solutions and a wealth of discussion information online about how to solve the problems and tradeoffs that each solution introduces.

Thus, I would highly recommend using "Animal" as your example because of the richness of available information and examples.

这篇关于什么是类继承的好例子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 19:18