英文:
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 { ...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论