如何将一个类型作为另一个类型的一部分? (Java)

huangapple go评论82阅读模式
英文:

How to make a type part of another type? (Java)

问题

这个问题的措辞不够清晰,但基本上我创建了一些可以用作类型的类,例如Dog、Cat、Whale等。我还创建了另一个类,可以接受一个宠物。如何使类似dog、cat等的类属于宠物类型?

英文:

This question isn't worded the best, but essentially I created some classes that can be used as types called Dog, Cat, Whale, etc. I also made another class that could take in a pet. How would I make classes like dog, cat, etc. belong to the pet type?

答案1

得分: 0

你正在寻找的东西叫做继承。在Java中,你可以在类定义中使用extends关键字来使一个类扩展另一个类。

例如:

public class Dog extends Pet { ...
英文:

What you're looking for is called inheritance. In Java, you use the extends keyword in the class definition to make one class extend another.

For example:

public class Dog extends Pet { ...

huangapple
  • 本文由 发表于 2020年8月9日 07:06:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63321002.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定