Do I correctly understand what a class is?

前端 未结 9 864
Happy的楠姐
Happy的楠姐 2021-01-01 20:52

I\'ve had trouble finding a clear, concise laymans definition of a class. Usually, they give general ideas without specifically spelling it out, and I\'m wondering if I\'m u

相关标签:
9条回答
  • 2021-01-01 21:52

    At the very basis, there's procedural code:

    var a = 4
    var b = 5;
    print a + b;
    … and so on, statements following statements…
    

    To make such pieces of code reusable, you make a function out of them:

    function a_plus_b() {
        var a = 4
        var b = 5;
        print a + b;
    }
    

    Now you can use that piece of code as often as you want, but you only had to write it once.

    Usually an application depends on a certain way of how pieces of code and variables have to work together. This data needs to be processed by that function, but cannot be processed by that other function.

    function foo(data) {
        …do something…
        return data;
    }
    
    function bar(data) {
        …do something else…
        return data;
    }
    
    a = "some data";
    b = 123456;
    
    a = foo(a);
    b = bar(b);
    c = bar(a); // ERROR, WRONG DATA FOR FUNCTION
    

    To help group these related parts together, there are classes.

    class A {
        var data = 'some data';
        function foo() {
            …do something…
            return data;
        }
    }
    

    The function foo now has a variable data that is "bundled" with it in the same class. It can operate on that variable without having to worry about that it may be the wrong kind of data. Also there's no way that data can accidentally end up in function bar, which is part of another class.

    The only problem is, there's only one variable data here. The function is reusable, but it can only operate on one set of data. To solve this, you create objects (also called instances) from the class:

    instance1 = new A();
    instance2 = new A();
    

    instance1 and instance2 both behave exactly like class A, they both know how to perform function foo (now called an instance method) and they both hold a variable data (now called an instance variable or attribute), but that variable data can hold different data for both instances.

    That's the basics of classes and objects. How your particular "OK", "Cancel" dialog box is implemented is a different story. Both buttons could be linked to different methods of different classes, or just to different methods of the same class, or even to the same method of the same class. Classes are just a way to logically group code and data, how that's going to be used is up to you.

    0 讨论(0)
  • 2021-01-01 21:52

    In a language agnostic fasion, I would describe a class as being an object that contains related information.

    A person class would have methods like Talk(), Walk(), Eat(); and attributes like Height, Color, Language, etc.

    A class is like a blueprint of things that you can instantiate, and also procedures that are related to each other.

    If you have a Database class, you might have many methods related to databases that do all sorts of voodoo with a database.

    0 讨论(0)
  • 2021-01-01 21:55

    In object-oriented programming, a class is a type for objects. An object is a bundle of data together with functionality that can operate in the context of that data; the definition of what the object is and does when it is first created is determined by its class.

    Like a type for data, the class of an object specifies what is common to all instances of that class. Instances, which are the objects themselves, then get to override that common baseline (otherwise there's not much point having distinct instances). In many OO systems, instances may or may not have new members that are not part of the class definition.

    What that means in the context of a specific object-oriented language is going to differ from language to language. But if you think of classes as types, and build on that, you won't go far wrong.

    0 讨论(0)
提交回复
热议问题