Do I correctly understand what a class is?

2019-02-08 08:20发布

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 understanding this correctly. As I understand it, a class is the set of code that controls an object. For example, in an app that has a button for 'Yes' and a button for 'No', and a text box for output, the code that tells the computer what to do when the user uses the Yes button is one class, the code for hitting No is another class, and an object is the two buttons and what they do together to influence the output box. Am I right, or am I confusing terms here?

Thanks

标签: oop class object
9条回答
姐就是有狂的资本
2楼-- · 2019-02-08 09:22

From the definition of Class at Wikipedia:

In object-oriented programming, a class is a construct that is used as a blueprint (or template) to create objects of that class. This blueprint describes the state and behavior that the objects of the class all share. An object of a given class is called an instance of the class. The class that contains (and was used to create) that instance can be considered as the type of that object, e.g. an object instance of the "Fruit" class would be of the type "Fruit".

A class usually represents a noun, such as a person, place or (possibly quite abstract) thing - it is a model of a concept within a computer program. Fundamentally, it encapsulates the state and behavior of the concept it represents. It encapsulates state through data placeholders called attributes (or member variables or instance variables); it encapsulates behavior through reusable sections of code called methods.

Your understanding of a Class isn't at all incorrect but to make things clear consider the following...

The Yes and No buttons plus the TextBox are usually specified within a class taking for example code written in C# (Microsoft .NET Framework). Let's name this class MyClass.

The actions the buttons cause are handled by what are called handlers (methods). You could write your code in such a way that when you click the Yes button something gets written in the TextBox.

To instantiate MyClass you'd do the following:

MyClass myClass = new MyClass();

myClass.ButtonYes += new EventHandler(YourMethodForYes);
myClass.ButtonNo += new EventHandler(YourMethodForNo);

myClass.TextBox.Text = "Yes button was clicked";

Hope you get the idea.

I wrote usually above because this cenario you described could be implemented in a number of ways. OOP gives you plenty of ways to accomplish the same task.

Besides the definition of Class I think that reading about Object Oriented Programming (OOP) can help you a lot to understand it even more. Take a look at Fundamental Concepts.

查看更多
Deceive 欺骗
3楼-- · 2019-02-08 09:22

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.

查看更多
别忘想泡老子
4楼-- · 2019-02-08 09:23

This is going to be a very simplified explanation. A class is a set of functions and variables and is used to create objects. I think it's good to use real examples instead of dog / bark / talk etc.

  • Class Email
    • Subject (string)
    • Message (string)
    • ToAddress (string)
    • FromAddress (string)
    • Send (function)

When you call 'new Email()' it creates a new object with those variables and functions. Then you can populate the variables and send it.

查看更多
登录 后发表回答