抽象类:-实时示例 [英] Abstract Class:-Real Time Example

查看:31
本文介绍了抽象类:-实时示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近在一次采访中,我被问到一个非常笼统的问题java中的抽象是什么".我给出了定义,然后是其他一些关于抽象的问题,如什么是抽象方法以及抽象方法和具体方法之间的区别以及等等.然后最后面试官要我举一个实时的例子,我应该在什么时候使用或定义一个类为抽象的.我很困惑.我举了一些例子,但他不相信.

Recently in a interview I was asked a very general question "what is abstract in java".I gave the definition and it was followed with some other question on abstract as what is abstract method and difference between abstract method and concrete method and etc. Then at last interviewer asked to give a real time example when I should use or define a class as abstract.I got confused.I gave some example but he was not convinced.

我用谷歌搜索但没有找到真正的解决方案.

I googled it but found no real solution.

那么有人可以给我实时示例,即他在他/她的项目中将一个类定义为抽象类,为什么?

So can someone give me real time example i.e. when he defined a class as abstract in his/her project and why?

谢谢.

推荐答案

这里:-

抽象类的一个具体例子是一个叫做动物.你在现实生活中看到很多动物,但只有几种动物.也就是说,你永远不会看着紫色和毛茸茸的东西说那是一种动物,没有更具体的定义方法".相反,您会看到狗、猫或猪……所有动物.重点是,你永远看不到动物走来走去特别是其他东西(鸭子、猪等).动物是抽象类和 Duck/Pig/Cat 都是从它派生的类基类.动物可能会提供一个名为年龄"的函数,它会增加 1动物的生命年.它还可能提供一个抽象方法被称为IsDead",当被调用时,它会告诉你动物是否有死了.由于 IsDead 是抽象的,每个动物都必须实现它.所以,一个猫可能会在它长到 14 岁时决定它已经死了,但是鸭子可能会决定它在 5 岁后死亡.抽象类Animal 为所有派生自它的类提供 Age 函数,但是每个类都必须自己实现 IsDead.

A concrete example of an abstract class would be a class called Animal. You see many animals in real life, but there are only kinds of animals. That is, you never look at something purple and furry and say "that is an animal and there is no more specific way of defining it". Instead, you see a dog or a cat or a pig... all animals. The point is, that you can never see an animal walking around that isn't more specifically something else (duck, pig, etc.). The Animal is the abstract class and Duck/Pig/Cat are all classes that derive from that base class. Animals might provide a function called "Age" that adds 1 year of life to the animals. It might also provide an abstract method called "IsDead" that, when called, will tell you if the animal has died. Since IsDead is abstract, each animal must implement it. So, a Cat might decide it is dead after it reaches 14 years of age, but a Duck might decide it dies after 5 years of age. The abstract class Animal provides the Age function to all classes that derive from it, but each of those classes has to implement IsDead on their own.

企业示例:

我有一个持久化引擎,可以针对任何数据源工作(XML, ASCII (delimited and fixed-length), 各种JDBC源(Oracle、SQL、ODBC 等)我创建了一个基本的抽象类来提供此持久性中的常见功能,但实例化持久化我的对象时适当的端口"(子类).(这使得新端口"的开发更容易,因为大部分工作已经完成在超类中;尤其是各种 JDBC 的;因为我没有只做持久性,但其他事情[如表生成],我有为每个数据库提供各种差异.)最好的接口的业务示例是集合.我可以和一个java.util.List 不关心它是如何实现的;有名单作为抽象类没有意义,因为有基本的anArrayList 的工作方式与 LinkedList 的不同之处.同样,映射和设置.如果我只是和一群人一起工作对象并且不在乎它是 List、Map 还是 Set,我可以只使用收藏界面.

I have a persistance engine that will work against any data sourcer (XML, ASCII (delimited and fixed-length), various JDBC sources (Oracle, SQL, ODBC, etc.) I created a base, abstract class to provide common functionality in this persistance, but instantiate the appropriate "Port" (subclass) when persisting my objects. (This makes development of new "Ports" much easier, since most of the work is done in the superclasses; especially the various JDBC ones; since I not only do persistance but other things [like table generation], I have to provide the various differences for each database.) The best business examples of Interfaces are the Collections. I can work with a java.util.List without caring how it is implemented; having the List as an abstract class does not make sense because there are fundamental differences in how anArrayList works as opposed to a LinkedList. Likewise, Map and Set. And if I am just working with a group of objects and don't care if it's a List, Map, or Set, I can just use the Collection interface.

这篇关于抽象类:-实时示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆