为什么我们在c#中使用继承 [英] why we use inheritance in c#

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

问题描述

告诉我为什么我们使用继承...不需要例子只是解释

tell me why we use inheritance... not need example just explaning

推荐答案

这又是一个我们为什么要呼吸?的问题。 />


请参阅:

http://en.wikipedia.org/wiki/Inheritance_%28object-oriented_programming%29 [ ^ ],

http://en.wikipedia.org/wiki/Override_(object-oriented_programming) [ ^ ],

http:// en.wikipedia.org/wiki/Interface_inheritance [ ^ ],

http:// en .wikipedia.org / wiki /面向对象_程序设计 [ ^ ]。



提出这些问题并不能帮助你专注于编程技术。相反,你应该至少抓住关于OOP或.NET的好书并阅读所有内容,并充分了解,在阅读时在代码中做一些简单的练习。没有什么可以取代普通教育。



-SA
This yet another question of the kind "why do we breath?"

Please see:
http://en.wikipedia.org/wiki/Inheritance_%28object-oriented_programming%29[^],
http://en.wikipedia.org/wiki/Override_(object-oriented_programming)[^],
http://en.wikipedia.org/wiki/Interface_inheritance[^],
http://en.wikipedia.org/wiki/Object-oriented_programming[^].

Asking such questions won't help you to lean about programming technology. Instead, you should grab at least on good book on OOP or on .NET and read it all, with good understanding, doing some simple exercises in code while reading. Nothing can replace regular education.

—SA


推荐



1. [MSDN]何时使用继承 [ ^ ]

Refer

1. [MSDN] When to Use Inheritance[^]
引用:

继承是一个有用的编程概念,但很容易使用不当。通常接口可以更好地完成工作。本主题和 何时使用界面 帮助您了解何时应该使用每种方法。

Inheritance is a useful programming concept, but it is easy to use inappropriately. Often interfaces do the job better. This topic and When to Use Interfaces help you understand when each approach should be used.

在以下情况下继承是一个不错的选择:

Inheritance is a good choice when:

  • 您的继承层次结构代表is-a关系,不是有 - 关系。

  • Your inheritance hierarchy represents an "is-a" relationship and not a "has-a" relationship.

您可以重复使用基类中的代码。

You can reuse code from the base classes.

您需要申请与不同数据类型相同的类和方法。

You need to apply the same class and methods to different data types.

类层次结构相当浅,其他开发人员不太可能添加更多级别。

The class hierarchy is reasonably shallow, and other developers are not likely to add many more levels.

您希望通过更改基类对派生类进行全局更改。

You want to make global changes to derived classes by changing a base class.

下面按顺序讨论这些注意事项。

These considerations are discussed in order below.



2. 为什么我们需要在C#或java中继承?为什么在编程中创建了继承? [ ^ ]


2. Why we need Inheritance in C# or java? Why Inheritance was created in programming?[^]

引用:

你知道这句话......不止一种方法给猫皮肤。



需要在这里是一个强有力的词。编程不断发展,首先是关闭。这是游戏的本质。程序员试图通过技术使生活更轻松。包括他们自己的生活。



面向对象编程解决了程序员长期面临的许多困境。例如,无处不在的意大利面条代码,其中包含重复的模块。如上所述,继承是OOP中一个非常重要的概念,理解它将使你成为一个更好的程序员。



但有几个原因......至于为什么程序员带来了继承混合:



干:不要重复自己。这是编程中的一个概念,讲的是生产力和可读性。继承减少或消除了重复代码的需要。



复杂性。 OOP降低了程序中代码的复杂性(或者至少应该......)。通过使用继承,您的代码将再次出现,而不是重复,并且更容易破译。



维护。在维护解决方案时,继承在正确实现时有很大帮助。



整个OOP范例还有很多内容,但要了解继承的方式和原因很重要。

You know the saying...more than one way to skin a cat.

"Need" is a strong word here. Programming is constantly evolving, first off. It's the nature of the game. Programmers try to make life easier through technology. Their own lives, included.

Object Oriented Programming tackles many dilemmas programmers had faced for a long time. Inelegant, spaghetti code with repeated modules everywhere, for example. As said, inheritance is an extremely important concept in OOP, and understanding it will make you a better programmer.

A few reasons though...as to why programmers brought inheritance into the mix:

DRY: Don't Repeat Yourself. It's a concept in programming that speaks about productivity, and readability. Inheritance reduces or eliminates the need for repeated code.

Complexity. OOP reduces the complexity of the code in a program (or at least, it should...). By using inheritance, your code is again, not repeated, and easier to decipher.

Maintenance. In maintaining a solution, inheritance helps out a great deal when implemented correctly.

There is a lot more to the entire OOP paradigm, but understanding the how and why of inheritance is important.


-Inheritance is a way to form new classes (instances of which are called objects) using classes that have already been defined.
-Inheritance is employed to help reuse existing code with little or no modification.
-The new classes, known as Sub-class or derived class, inherit attributes and behavior of the pre-existing classes, which are referred to as Super-class or Base class.







这是一个没有例子就无法解释的话题。



请通过这篇MSDN文章..



http://msdn.microsoft.com/en-us/library/27db6csx(v = vs.90).aspx [ ^ ]


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

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