简体   繁体   English

如何在面向对象中表示动物园?

[英]How to represent a Zoo in Object Oriented?

i got this interview question about how to represent a Zoo in OOP and my answer was that the Zoo will have property set animals, where Animal is an abstract class and all the animals in the zoo will extend it, it will contain the species,city properties. 我得到了关于如何在OOP中代表一个动物园的这个采访问题,我的回答是动物园将拥有属性集动物,其中Animal是一个抽象类,动物园里的所有动物都会扩展它,它将包含物种,城市属性。 and Zoo will extend the abstract class Agricultural_land , which contains the space,place properties. 和Zoo将扩展抽象类Agricultural_land ,其中包含空间,地点属性。

and i got a question about if i have different types of birds, how to represent them in OOP and i said i will make another class for birds and different types of birds will extend it. 我得到一个问题,如果我有不同类型的鸟类,如何在OOP中代表它们,我说我将为鸟类制作另一个类别,不同类型的鸟类将扩展它。

please give me your ideas. 请把你的想法告诉我。

Main reason for asking open questions during an interview is a need for information on your skills. 在面试中提出问题的主要原因是需要有关您技能的信息。 So, there is no one good way to answer such kind of question. 所以,没有one good way可以回答这类问题。 Instead, you can consider which skills would like to demonstrate. 相反,您可以考虑要展示哪些技能。

A few hints bellow: 下面有几点提示:

  • basic object analysis (how you create classes, methods or attributes) 基本对象分析(如何创建类,方法或属性)
  • building of classes hierarchy (where inheritance is needed - when not) 类层次结构的构建(需要继承 - 何时不需要继承)
  • using interfaces 使用接口
  • Inversion of control 控制倒置
  • design patterns (eg. does observer pattern is suitable for vets and animals health control?) 设计模式(例如观察者模式是否适合兽医和动物的健康控制?)
  • your approach for building unit tests 你建立单元测试的方法

Any object is a combination of state (field values) and behaviour (methods logic). 任何对象都是状态(字段值)和行为(方法逻辑)的组合。 I think if you have to represent something as an object you better specify the context first. 我认为如果你必须将某些东西表示为对象,你最好先指定上下文。 If you develop some land trading application then your Zoo should have fields like cost , owner , lastSoldDate and methods like sell , changeOwner , calculateTax . 如果您开发了一些土地交易应用程序,那么您的Zoo应该包含costownerlastSoldDate以及sellchangeOwnercalculateTax等方法。 If you work on game then your Zoo will have animals , trees and so on. 如果您从事游戏,那么您的动物园将拥有动物树木等。 Clear context allows you to decide what is important for your object and what's not. 清晰的上下文允许您决定什么对您的对象重要,哪些不重要。

As for birds I suggest you to read first chapter of Head First Design Patterns book. 至于鸟类,我建议你阅读Head First Design Patterns一书的第一章。 There is very fun and clear explanation of how to design ducks hierarchy and how to avoid common pitfalls. 有关如何设计鸭子等级以及如何避免常见陷阱的非常有趣和清晰的解释。 They explain the composition over inheritance principal which become kind of enlightenment for me. 他们解释了继承主体的构成 ,这对我来说是一种启示。 Now I'm trying to avoid inheritance and design my objects as a set of components responsible for behaviour. 现在我试图避免继承并将我的对象设计为一组负责行为的组件。 And now no over 9000 classes for different birds, no complex classes hierarchy where you scratch your head if you need to add a new bird. 现在,对于不同的鸟类,没有超过9000个班级,没有复杂的班级层次结构,如果你需要添加一只新的鸟类,你可以抓住它们。 That's really nice book :) 那是非常好的书:)

Etymologically, the word "animal" derives from the Latin meaning a "living being, a being which breathes".Birds breathe and are members of the Animal Kingdom. 在词源学上,“动物”这个词来源于拉丁语,意思是“生物,呼吸的存在”。鸟类呼吸并且是动物王国的成员。

So Bird class will extend Animal class 所以Bird类将扩展Animal

you could start way more higher in the extension tree.... you have a class "livingt hings" that gets extended by birds, which gets extended... 你可以在扩展树中开始更高的方式....你有一个类“生命之翼”,它被鸟类扩展,延伸......

and a Zoo has more then only animals. 动物园里只有动物。 It has roads, workers, visiters, trees, garbage cans... 它有道路,工人,访客,树木,垃圾桶......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM