既然我开始编程并且课程对我来说是个问题,那么有人可以解释什么课程是什么以及课程中哪些是如此重要? [英] since i started programming and classes are problem to me can someone explain what classes are for and what is so important with classes ?

查看:71
本文介绍了既然我开始编程并且课程对我来说是个问题,那么有人可以解释什么课程是什么以及课程中哪些是如此重要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以向我解释一下这些类是什么,以及它们在代码中的用途是什么,为什么有类很重要,我的意思是任何代码都可以在没有类的情况下正常工作?也许一个例子对我来说是好的,你发现任何一个例子对我来说都有好处,因为我现在已经阅读并搜索了一周,直到我发现这个页面得到了很多帮助

推荐答案

所以,基本上,你想知道在没有课程的情况下你可以完成几乎所有编程的课程的价值是什么?



让我告诉你一个故事。很久以前,在一个与我们自己不同的星系中,编程语言的发展远不如C#。其中一种语言称为汇编语言。这种汇编语言与处理器非常接近,无需编写机器码,这只是一堆0的和1。汇编语言允许您使用计算机执行任何操作,因为它使您可以访问处理器可以计算的完整指令集。



但是,有一个汇编语言的问题。这很简单,许多常见的任务很难完成,并且有很多不必要的重复输入。例如,如果要创建函数,则必须启动跳转到它的指令,必须将寄存器值保存到堆栈中,当函数调用结束时,必须跳回到调用它的位置。来自,并撤消你在初始跳跃时所做的所有步骤。



Modern 高级编程语言使这些常见任务变得更加容易,因为它们内置了快捷方式。例如,在C#中,调用函数的复杂汇编语言任务就像这样完成:

So, basically, you are wondering what the value of classes are when you can accomplish pretty much all programming without classes?

Let me tell you a story. A long time ago in a galaxy not unlike our own, there were programming languages much less developed than C#. One of these languages was called assembly language. This assembly language was as close to the processor as you could get without writing in machine code, which is just a bunch of 0's and 1's. Assembly language allows you to do anything you want with a computer, as it gives you access to the full instruction set that a processor can compute.

However, there is one problem with assembly language. It's so simple, that many common tasks are very hard to accomplish, and there is a lot of unnecessary repeat typing. For example, if you want to create a function, you have to initiate the instruction to jump to it, you have to save register values to the stack, and when the function call is over, you have to jump back to where it was called from, and undo all the steps you did when you did the initial jump.

Modern high level programming languages make these common tasks much easier, as they have shortcuts built in. For example, in C#, that complicated assembly language task of calling a function is done like this:
namespace MyConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            int y = Add(5, 6);
        }

        static int Add(int x1, int x2)
        {
            return x1 + x2;
        }
    }
}



现在,我只想演示添加函数及其调用,但为了完整起见,我还包括命名空间,类和主函数。以下是我所讨论的唯一重要部分:


Now, I only wanted to demonstrate the Add function and the call to it, but I also include the namespace and class and main function for completeness sake. Here are the only important parts that I was talking about:

int y = Add(5, 6);
static int Add(int x1, int x2)
{
  return x1 + x2;
}



非常简单。拥有这样的功能并且能够在许多位置轻松调用该功能意味着您已经增加了该代码的可重用性 。这是一件非常好的事情,因为可重用性意味着你必须输入更少,因此它可以让你更快地编程。相同类型的原则适用于类。如上所述,我有一个名为 Program 的类,其中包含两个函数。但是,将所有功能放在一个类中可能会失控。我曾在代码库中工作过数百万行代码。通过将代码拆分为不同的函数,类和命名空间,您可以使用名为封装的内容更好地理解代码。 。封装基本上意味着编写执行大量工作的类和函数,但函数/类的调用者不必知道。举个例子,我创建了一个新类,并将 Add 函数移入其中:


Very simple. Having a function like this and being able to easily call that function in many locations means that you have increased the reusability of that code. That is a very good thing, as reusability means you have to type less, and so it lets you program faster. The same types of principles apply with classes. As you saw above, I had a class called Program with two functions in it. However, putting all of your functions into a single class can get out of hand. I have worked in code bases with millions of lines of code. By splitting your code into different functions, classes, and namespaces, you can make better sense of the code with something called encapsulation. Encapsulation basically means writing classes and functions that do a bunch of work, but that the caller of the function/class doesn't have to know about. To give you an example, I have created a new class and moved our Add function into it:

namespace MyConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            Maths m = new Maths();
            int y = m.Add(5, 6);
            int z = m.Add(8, 9);
        }
    }
    class Maths
    {
        public int Add(int x1, int x2)
        {
            return x1 + x2;
        }
    }
}



如果需要,我们可以将Maths类移动到另一个文件中。现在,上面的所有代码都在Program.cs文件中,但是我们可以创建一个名为Maths.cs的新文件并将Maths类放入其中。这是该文件包含的内容:


If we wanted, we could move the Maths class into a different file. Right now, all of the above code is in the Program.cs file, but we could create a new file called Maths.cs and put the Maths class in it. This is what that file would contain:

namespace MyConsoleApplication
{
    class Maths
    {
        public int Add(int x1, int x2)
        {
            return x1 + x2;
        }
    }
}



如果我们再次查看Program.cs文件,我们会发现它很简单:


If we look at our Program.cs file again, we see that it is very simple:

namespace MyConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            Maths m = new Maths();
            int y = m.Add(5, 6);
            int z = m.Add(8, 9);
        }
    }
}



用几百个同事写几百万行代码,类的值变得多了更清晰。它们允许您封装功能和数据以简化代码库并允许更高的可重用性。



我不会在这里教授完整的计算机科学课程,而是让您了解为什么其他类有用,它们可用于:存储数据,创建序列化结构,允许继承和接口实现等等。随着时间的推移,你会遇到所有这些。现在,只要相信课程确实有它们的用途,你肯定会在你的职业生涯中大大使用它们。


Write a few million lines of code with a few hundred coworkers, and the value of classes becomes much more clear. They allow you to encapsulate functionality and data to simplify the code base and allow for greater reusability.

I am not going to teach a full Computer Science course here, but to give you a taste of why else classes are useful, they can be used to: store data, create structure for serialization, allow for inheritance and interface implementation, and much more. You will come across all of this over time. For now, just trust that classes do have their uses and you will definitely make use of them greatly over your career.


我想加入我自己的旋转,因为我不喜欢我认为解决方案真的可以告诉你一个类的价值是什么......



首先,背景。是的,在汇编的几天里很难编程,正如Aspdotnetdev所说,然而随着Fortran,Cobol等高级语言的出现,软件变得越来越复杂,需要面向对象的编程。



早在编程的早期阶段,语言就是从上到下贯穿的语句列表。有些语言有函数调用或子程序可以将复杂的代码转储分解为更易读的版本,但是没有任何面向对象的东西。程序员需要采用真实世界系统并将其转换为程序,并且很难为非面向对象编程(OOP)维护更少的功能。



让我们采取例如,你想要编程Car,在线性编程中你会有一个循环和一堆函数,if语句,开关等来处理汽车的所有功能。 OOP在编程时引入了一种新的思维概念,如果你想开车,你可能会有类似的东西:



I'd like to add my own spin on this as I don't think the solution really shows you what the value of a class is...

First, the background. Yes, in days of assembly it was difficult to program, just as Aspdotnetdev said, however as higher level languages emerged such as Fortran, Cobol, etc, and software got more complex there was a need for object oriented programming.

Back in the early years of programming, languages were simply statement lists that ran through from top to bottom. Some languages had function calls or subroutines that could break up complicated code dumps into more readable versions, but there wasn't anything object oriented. Programmers needed to take real world systems and turn them into programs and it was difficult to maintain much less add functionality to non-object oriented programming (OOP).

Lets take for example that you wanted to program a Car, in linear programming you would have a loop and a bunch of functions, if statements, switches, etc to handle all the features of the car. OOP introduces a new concept of thinking when programming, if you wanted to do a car, you might have something like this:

public class Car
{
    public void StartEngine() { }
    public void StopEngine() { }
    public void Accelerate() { }
    public void Decelerate() { }
    public void OpenDriversDoor() { }
    public void OpenPassengerDoor() { }
}





您实现例程,您的软件运行良好。一年后你的老板来找你并说我们需要增加一种新型汽车,一辆配备4轮驱动的SUV,但我们无法打破现有代码。很容易说,你这样做:





You implement the routines and your software runs great. A year down the line your boss comes to you and says "we need to add a new kind of car, an SUV that has 4-wheel drive, but we can't break exising code". Easy you say, you do something like this:

public class Car
{
    public void StartEngine() { }
    public void StopEngine() { }
    public void Accelerate() { }
    public void Decelerate() { }
    public void OpenDriversDoor() { }
    public void OpenPassengerDoor() { }
}

public class SUV : Car
{
    public void Enable4x4() { }
    public void Disable4x4() { }
}





现在SUV拥有Car的所有功能(因为它来自它) ),它有4x4。您已经为软件添加了一组新功能,而无需修改现有代码,这可以减少错误,代码流失,错误等。



上面的示例可以扩展,假设你想创建一个飞行汽车,你可以添加另一个类,派生自Car,并实现飞行程序。



考虑到这些程序的方式随着OOP原则而改变,程序不再是某些东西的线性近似,而是分解为各个系统并连接在一起以便它们更模仿如何在现实世界中实施它们。如果你扩展它,你可以找到单一责任,依赖注入等原则,这些原则可以使编码非常强大,易于维护,并且易于扩展。



以下是一些参考资料:



https:// en.wikipedia.org/wiki/Object-oriented_programming [ ^ ]



https:// en.wikipedia.org/wiki/Solid_(object-oriented_design) [ ^ ]



https:/ /en.wikipedia.org/wiki/Comparison_of_programming_paradigms [ ^ ]



Now SUV has all the functionality of Car (because it derives from it), and it has 4x4. You've added a new set of features to your software without the need to modify existing code, this reduces bugs, code churn, errors, etc.

The example above can be extended, lets say you wanted to create a flying car, you'd add another class, derive from Car, and implement the flying routines.

The way of thinking about these programs changed with OOP principles, programs were no longer linear approximations of something, but broken down into the individual systems and wired together so that they more mimic how they would be implemented in the real world. If you extend this out, you can find principles like Single Responsibility, Dependency Injection, etc that can really make coding very powerful, easy to maintain, and easy to extend.

Here are some references for you:

https://en.wikipedia.org/wiki/Object-oriented_programming[^]

https://en.wikipedia.org/wiki/Solid_(object-oriented_design)[^]

https://en.wikipedia.org/wiki/Comparison_of_programming_paradigms[^]


有些人给出了很好的,深入的答案,但是,认真地,买书或读一些高级别的文章我不知道我在做什么的问题。
Some people have given good, in depth answers but, seriously, buy a book or read some articles for high level 'I have no clue what I am doing' questions.


这篇关于既然我开始编程并且课程对我来说是个问题,那么有人可以解释什么课程是什么以及课程中哪些是如此重要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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