我想加深理解的重要概念

发布于 2024-12-20 01:53:32 字数 387 浏览 2 评论 0原文

我已经编程了不到一年的时间,并且自学了部分 Python、PHP 和 Javascript。我真的很想成为一名更好的程序员并理解编程背后的理论(本质上似乎是信息的操纵)。

基本上,它似乎编写了良好的代码 - 它必须是模块化的 - 这降低了程序的复杂性,这反过来又使它们更容易阅读和重现类似的效果。这使用了函数和类。

函数和类有什么区别?看起来它们都是可以传递参数的东西 - 我知道类比函数“更高”。

什么是命名空间?它与变量有何不同?数据结构?我在哪里可以找到此类信息的资源?我在网上查过,但很多看起来都像官话。我也已经获得了学士学位,所以我不打算回去攻读第二学位。我现在只想成为一名更好的程序员。我已经做了足够多的“黑客”工作,我已经了解了基本概念,但理论基础仍然不完整。

任何建议都会有帮助。

So I've been programming for a little under a year now, and I've taught myself parts of Python, PHP, and Javascript. I'm really trying to become a better programmer and understand the theory behind programming (which seems, in essence, manipulation of information).

Basically it seems to write good code - it must be modular - this reduces complexity of programs, which in turn makes them easier to read and reproduce similar effects. This uses functions and classes.

What is the difference between a function and a class? It seems like they are both something that can have an argument passed through to it - I understand that a class is "higher" than a function.

What is a namespace? How does it differ from a variable? A data structure? Where can I find resources on this sort of information? I've looked on the internet, but a lot of it seems like gobblygook. I also already have my bachelor's, so I am not looking to go back for a second degree. I just want to become a better programmer at this point. I've done enough "hacking" that I get the basic concept, but the theoretical underpinnings still aren't all there.

Any advice would be helpful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半暖夏伤 2024-12-27 01:53:32

该问题的部分问题(相关性警察可能很快就会关闭)是,您使用的许多术语根据您使用的语言范式具有不同的含义。 PHP 中的函数与 JavaScript 中的函数不同。

我建议从“JavaScript:优秀的部分”开始,以便从 JS 工作人员的特定角度进行理解。

Part of the problem with the question (which the relevancy police will probably be closing shortly) is that a lot of the terms you're using have different meanings depending on the language paradigm you're using. A function in PHP is a different beast from a function in JavaScript.

I recommend starting with "JavaScript: the Good Parts" for an understanding from the particular perspective of someone working in JS.

因为看清所以看轻 2024-12-27 01:53:32

好吧,让我们从上到下:

命名空间是代码的逻辑顺序。

现实世界的类比是图书馆。图书馆包含所有书籍,但图书馆的某些部分专门用于特定领域(例如有关物理的书籍(代码))是有意义的

。类是公开函数和属性的模型(几乎总是从现实世界的对象派生)。类可以(并且应该)封装(隐藏)开发人员不希望其他开发人员能够访问的属性和函数。考虑:

public class Car{

public Car(){}//default constructor.
public Car(int tirecount){//this constructor allows initialisation of the class to some 'safe' state
    Tires = new Tire[tirecount];
}
//properties 
public Tire[] Tires{get;set}//bad. at any point you can remove a tire from the car
public bool IsStopped{get;private set;}//safe. can check if car is stoppped outside class but can only change value inside car    

    //functions (...methods)
    public Start(){//starts car
        IsStopped = false;
    }
    public Stop(){
        IsStopped = true;
    }
    public RemoveTire(int tireIndex)
    {
        if(!this.IsStopped)this.Stop();
        Tires[tireIndex].Remove();//safe to remove tire when stopped
    }
}

为了获得代码重用和多态行为,您必须阅读有关接口的内容。接口允许定义契约。合约的内部实现可以更改,但在不破坏依赖于该合约的原始代码的情况下,无法更改已定义的方法。可以在不破坏旧实现的情况下添加额外协议。示例:

Man 类实现 ITalk
Dog 类实现 ITalk

ITalk 合约状态“我有函数‘Speak’”,即

interface ITalk{
     void Speak();
}

class World
{
    List<ITalk> beings;

    public World(List<ITalk> beingsToPopulateWorldWith)
    {
        beings = beingsToPopulateWorldWith;
    }

    public void MakeAllAnimalsTalk()
    {
        foreach(var b in beings)b.Speak();//because we know all object in the beings list use the ITalk interface (contract) we KNOW that we can call .Speak(). What each ITalk does in speak is up to them but we know we can call it.
    }
}

Man.Speak() 可能输出“Hi”,Dog.Speak() 可能输出“Woof”。

类也可以扩展,因此请考虑人/狗的示例。他们的基类可能是动物。动物定义了 IsAlive。人类源自动物,因此获得了 IsAlive,就像狗一样,但是人类可以定义替代行为,即与狗不同的“AbilityToMakeTools”。

我发现,一旦您开始将类设想为现实世界的对象/过程(一切都可以建模,甚至是最抽象的“事物”),那么类就开始具有逻辑意义。

华泰

Okay lets go from the top down:

A namespace is a logical ordering of code.

A real world analogy is that of a Library. A library contains all the books but it make sense to have sections of the library devoted to specific areas e.g. books (code) about physics

A Class is a model (almost always derived from real-world objects) which exposes functions and properties. Classes can (and should) encapsulate (hide) properties and functions which the developer doesn't wish other developers to be able to reach. Consider:

public class Car{

public Car(){}//default constructor.
public Car(int tirecount){//this constructor allows initialisation of the class to some 'safe' state
    Tires = new Tire[tirecount];
}
//properties 
public Tire[] Tires{get;set}//bad. at any point you can remove a tire from the car
public bool IsStopped{get;private set;}//safe. can check if car is stoppped outside class but can only change value inside car    

    //functions (...methods)
    public Start(){//starts car
        IsStopped = false;
    }
    public Stop(){
        IsStopped = true;
    }
    public RemoveTire(int tireIndex)
    {
        if(!this.IsStopped)this.Stop();
        Tires[tireIndex].Remove();//safe to remove tire when stopped
    }
}

In order to get code re-use and polymorphic behavior you must read about interfaces. Interfaces allow the definition of a contract. The internal implementation of the contract can change but the methods already defined cannot be changed without breaking the original code that relies on that contract. Extra agreements can be added without breaking old implementations. Example:

Class Man implements ITalk
Class Dog implements ITalk

ITalk contract states 'I have function 'Speak' i.e.

interface ITalk{
     void Speak();
}

class World
{
    List<ITalk> beings;

    public World(List<ITalk> beingsToPopulateWorldWith)
    {
        beings = beingsToPopulateWorldWith;
    }

    public void MakeAllAnimalsTalk()
    {
        foreach(var b in beings)b.Speak();//because we know all object in the beings list use the ITalk interface (contract) we KNOW that we can call .Speak(). What each ITalk does in speak is up to them but we know we can call it.
    }
}

So Man.Speak() may output "Hi" and Dog.Speak() may output "Woof".

Classes can also extend so consider the Man/Dog example. Their base class could be Animal. Animal defines IsAlive. Man derives from Animal so gains IsAlive as does Dog however Man could then define alternative behaviour i.e. AbilityToMakeTools than that of dog.

I find that as soon as you start to envisage classes as real world objects/processes (everything can be modelled even the most abstract 'thing') then classes start to make logical sense.

HTH

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文