为什么我们在c#中使用类 [英] why we use classes in c#

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

问题描述

为什么我们为成员和方法使用单独的类....相反,我们可以在main()中完成所有工作...

创建类继承它并使对象...为什么? ??

解决方案

在这里阅读答案,以及以下链接:



< a href =http://csharp.net-tutorials.com/classes/introduction/> http://csharp.net-tutorials.com/classes/introduction/ [ ^ ]

http://msdn.microsoft.com/en-us/library/vstudio/x9afc042.aspx [ ^ ]

http://www.homeandlearn.co.uk/csharp/csharp_s10p2.html [ ^ ]

http:/ /www.wikihow.com/Understand-Object-Oriented-Programming [ ^ ]

http://en.wikipedia.org/ wiki /面向对象编程 [ ^ ] <我们使用类和继承的原因是我们使用面向对象编程方法进行编码,这需要分离关注点以及许多其他原则。首先看一下,你会明白为什么要使用类。


因为它让生活更简单。老实说,确实如此。对于一个非常小的项目来说,实现同样的目标似乎需要付出很多努力。但是要扩大一点...



忽略电脑,让我们看看制作一个物理对象。



如果您正在制作一个引脚,则有四个过程:切割线,扭曲端(形成可以推动的blob),焊接结束以覆盖blob,锐化引脚。简单。它是微不足道的,一个人可以一个接一个地做所有这些。这是Main()方法的方法。



但是如果你正在开车怎么办?从一堆原钢,一些橡胶,一些塑料和一点硅开始是一个好主意吗?不 - 因为你必须经过的工艺非常复杂:只需制造钢丝就意味着拉钢(或最好是铜)使其变薄,用其它钢绞线将其拧紧以使其更坚固,然后用熔融塑料涂覆绝缘它。制作轮胎也很复杂:打好绑带,在其周围成型合适的橡胶......并且不要让我开始使用发动机和管理系统!


$ b相反,汽车的各个部分被分解成组件,然后组装成大型组件,然后更大,直到它们最终组装成汽车并且可以被驱动。各个组件不需要知道它们对其他组件的作用。



软件是相同的。当你扩展它时,对于一个简单的例子来说效果很好就会成为一个庞大的PITA。类似汽车组件 - 他们知道他们必须做什么,他们不了解自己以外的任何事情,因为如果你这样做会降低复杂性。


请参阅以下链接明确的OOP概念



OOP概念

基本概念

why we use separate classes for members and methods.... instead we can done all work in main()...
creating classes inherit it and make objcts...why????

解决方案

Have a read of the answers on here, and the following links:

http://csharp.net-tutorials.com/classes/introduction/[^]
http://msdn.microsoft.com/en-us/library/vstudio/x9afc042.aspx[^]
http://www.homeandlearn.co.uk/csharp/csharp_s10p2.html[^]
http://www.wikihow.com/Understand-Object-Oriented-Programming[^]
http://en.wikipedia.org/wiki/Object-oriented_programming[^]

The jist of why we use Classes and inheritance is because we are coding using the Object Oriented Programming approach which requires separation of concern as well as a lot of other principles. Start by looking at that and you will understand why classes are used.


Because it makes life simpler. Honest, it does. It may seem that it's a lot of work to go through to get to the same goal - and it is, for a very small project. But scale it up a bit...

Ignore computers, and let's look at making a physical object.

If you are making a pin, there are four processes: cut wire, twist end (to form a "blob" you can push), solder end to neaten the "blob", sharpen the pin. Easy. it;s trivial, and one man can do all of them, one after another. This is the Main() method way to do it.

But what if you are making a car? Would it be a good idea to start with a pile of raw steel, some rubber, some plastic and a bit of silicon? No - because the processes that you have to go through are enormously complex: just making wire means drawing the steel (or preferably copper) to thin it down, twisting it with other strands to make it stronger and flexible, then coating it in molten plastic to insulate it. Making a tire is also complicated: getting the banding laid down, moulding the right rubber around it...and don't get me started on the engine and management systems!

Instead, the various parts of the car are broken down into assemblies which are then assembled into large assemblies, and larger still until they can be finally assembled into a car and it can be driven. The individual assemblies don't need to know what they do in relation to other assemblies.

Software is the same. What works well for a trivial example becomes a huge PITA when you scale it up. Classes are like car assemblies - they know what they have to do, and they don't know about anything outside themselves, because it reduces the complexity if you do that.


Refer following link to clear OOPs concept

OOPs Concep
Basic concept


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

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