关于JAVA继承的资料翻译

What Is Inheritance?
Different kinds of objects often have a certain amount in common with each other. Mountain bikes, road bikes, and tandem bikes, for example, all share the characteristics of bicycles (current speed, current pedal cadence, current gear). Yet each also defines additional features that make them different: tandem bicycles have two seats and two sets of handlebars; road bikes have drop handlebars; some mountain bikes have an additional chain ring, giving them a lower gear ratio.

Object-oriented programming allows classes to inherit commonly used state and behavior from other classes. In this example, Bicycle now becomes the superclass of MountainBike, RoadBike, and TandemBike. In the Java programming language, each class is allowed to have one direct superclass, and each superclass has the potential for an unlimited number of subclasses:

A diagram of classes in a hierarchy.
A hierarchy of bicycle classes.

The syntax for creating a subclass is simple. At the beginning of your class declaration, use the extends keyword, followed by the name of the class to inherit from:

class MountainBike extends Bicycle {

// new fields and methods defining 
// a mountain bike would go here

}
This gives MountainBike all the same fields and methods as Bicycle, yet allows its code to focus exclusively on the features that make it unique. This makes code for your subclasses easy to read. However, you must take care to properly document the state and behavior that each superclass defines, since that code will not appear in the source file of each subclass.
译文:
什么是继承?
不同的对象经常一定数量的共同之处。山地自行车,公路自行车,以及双人自行车,举个例子,这些自行车表现出来的所有自行车(当前的速度,当前的踏板节奏,当前的档位)。然而每一辆都定义了额外的属性来时他们不同:双人自行车有两个座位和两个车把;公路自行车有赛车车把;有些山地自行车还有一个的额外的链环,是他们能有一个更低的齿轮传动比。
面向对象编程允许类继承其他有常用状态和行为的类。在这个例子中,自行车现在变成了山地自行车、公路自行车以及双人自行车的父类。在Java编程语言中,每个类都允许有一个直接的父类,并且每个父类有可能不止一个的子类。
创建一个子类的语法很简单。在声明类的开头,用继承extends关键词,后面接上你的类名去继承。
这给予了山地自行车作为自行车该有的共同字段和方法,但是只允许他的代码专注于使他独一无二的特性。这使父类的代码更容易阅读。然而,你必须注意去合理的记录每个父类定义的状态和行为,因为代码可能不会出现在每个子类的源文件中。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值