TOC

This article is currently in the process of being translated into Czech (~17% done).

Třídy:

Úvod do C# tříd

Ve spoustě programovacích výukových programů budou informace o třídách zmíněny mnohem později. Nicméně, jelikož v C# je vše o objektově orientovaném programovaní a tím o třídách, podíváme se na základní úvod k nejdůležitějším funkcím.

First of all, a class is a group of related methods and variables. A class describes these things, and in most cases, you create an instance of this class, now referred to as an object. On this object, you use the defined methods and variables. Of course, you can create as many instances of your class as you want to. Classes, and Object Oriented programming in general, is a huge topic. We will cover some of it in this chapter as well as in later chapters, but not all of it.

V kapitole "Hello World" jsme poprvé viděli použití třídy, protože vše v C# je postaveno na třídách. Rozšiřme si naši aplikaci "Hello World" třídou, kterou sami vytvoříme:

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Car car;

            car = new Car("Red");
            Console.WriteLine(car.Describe());

            car = new Car("Green");
            Console.WriteLine(car.Describe());

            Console.ReadLine();

        }
    }

    class Car
    {
        private string color;

        public Car(string color)
        {
            this.color = color;
        }

        public string Describe()
        {
            return "This car is " + Color;
        }

        public string Color
        {
            get { return color; }
            set { color = value; }
        }
    }
}

Okay, lots of new stuff here, but almost all of it is based on things we've already used earlier in this tutorial. As you can see, we have defined a new class, called Car. It's declared in the same file as our main application, for an easier overview, however, usually new classes are defined in their own files. It defines a single variable, called color, which of course is used to tell the color of our car. We declared it as private, which is good practice - accessing variables from the outside should be done using a property. The Color property is defined in the end of the class, giving access to the color variable.

Besides that, our Car class defines a constructor. It takes a parameter which allows us to initialize Car objects with a color. Since there is only one constructor, Car objects can only be instantiated with a color. The Describe() method allows us to get a nice message with the single piece of information that we record about our car. It simply returns a string with the information we provide.

Now, in our main application, we declare a variable of the type Car. After that, we create a new instance of it, with "Red" as a parameter. According to the code of our class, this means that the color red will be assigned as the color of the car. To verify this, we call the Describe() method, and to show how easily we can create several instances of the same class, we do it again, but with another color. We have just created our first functional class and used it.

V následujících kapitolách budou podrobněji vysvětleny pojmy jako: vlastnosti, konstruktory a viditelnost.

This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!