何时使用继承 [英] When use inheritance

查看:97
本文介绍了何时使用继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友们,



访谈员询问何时以及为何使用继承以及为什么不创建实例?



任何人都告诉我两者的区别。



我的尝试:



使用继承时:

A级{}

B级:A {}



为什么不呢使用如下:

B级{

A a =新A();

}

解决方案

因为当B类继承自A类时,它包含A的所有功能,并且可以覆盖或扩展该功能,同时仍被视为两个类的实例。

第二个例子是封装,并要求B类明确公开它希望用户访问的A的属性,字段,方法和事件。



如果你不喜欢我们已经知道了,你正在申请超出你能力水平的工作......这是任何C学生的基本内容#应该在大约一年的第2周了解...


你知道,谷歌是你的朋友:继承与作文 [ ^ ]。

Dear Friends,

Interviewer asked when and why use inheritance and why not create instance?

Anyone tell me difference on both.

What I have tried:

When use inheritance:
class A{}
class B : A {}

Why not use like this:
class B{
A a=new A();
}

解决方案

Because when class B inherits from class A it incorporates all the functionality of A, and can override or extend that functionality while still being considered as an instance of both classes.
The second example is encapsulation, and requires class B to explicitly expose the properties, fields, methods and events of A that it wants it's users to access.

And if you don't know that already, you are applying for jobs way above your ability level ... this is basic stuff that any student of C# should understand by about week 2 of a year long course...


You know, Google is you friend: "inheritance vs composition"[^].


这篇关于何时使用继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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