new C# class

Advertisements

Hello everybody,

Michael here, and today’s post will cover classes in C#.

What is a C# class? Better yet, what is a programming class in general?

If you’ve learned other programming languages, you’ll certainly have encountered this concept. However, in case you need a refresher, C#, like Java and Python (and possibly many other languages) is what’s known as an object-oriented programming language. Just like with Java and Python, everything comes in classes and objects. All those classes and objects come with their own methods and attributes.

new C# class

Now, how do we construct a C# class? Take a look at this example below:

class Cat
{
    string color = "orange";
    int age = 11;
    string gender = "male";

    static void Main(string[] args)
    {
        Cat orangeCat = new Cat();
        Console.WriteLine(orangeCat.age);

    }
}

11

In this simple Cat class, we have three attributes-color, age, and gender. We also created an object of the Cat class in the Main() (yes C# capitalizes the word Main) method called orangeCat and accessed its age attribute with the following code-orangeCat.age-which returns the value of 11.

  • To access any attribute of a C# object, remember the following syntax: objectname.attributename.

Easy enough right? Now let’s see some new C# class tricks!

new C# class (no default values)

Now, how do we create a C# class with no default values? Let’s take a look:

class NewCat
{
    string color;
    int age;
    string gender;

    public NewCat(string catColor, int catAge, string catGender)
    {
        color = catColor;
        age = catAge;
        gender = catGender;
    }

    static void Main(string[] args)
    {
        NewCat brownCat = new NewCat("brown", 11, "female");
        Console.WriteLine("This is a " + brownCat.color + " cat who is " + brownCat.age + " years old and is a " + brownCat.gender);

    }
}

This is a brown cat who is 11 years old and is a female

In this example, I didn’t declare default values for color, age, and gender. Instead, I used a special method called a constructor to allow any object of the Cat class to be initialized with any possible value for each attribute. The constructor must have the same name as the class (Cat in this case) and can contain as many parameters as you want (3 parameters in my case for color, age, and gender).

Notice how I still declared the color, age, and gender variables before the constructor even though I use these same three variables inside the constructor. Why might that be? The first time I mention color, age and gender, all I did was declare that these variables will be a part of this Cat class. However, when I mentioned them again in the constructor, I actually initialize them to whatever the values of catColor, catAge and catGender are set to in the parameters.

  • Remember that before you initialize a variable with a constructor, you must first declare it!
  • Even though I didn’t give any default values for each variable, I can still access each variable in the same manner I did in the previous example (e.g. objectname.attributename).
  • Yes, since I stored each example as a separate script in the same directory, I used the name NewCat for this class. Keep in mind that if you’re going to create multiple classes in the same directory, you must use different names for each class.

Here are the links to both of today’s scripts in my GitHub:

Thanks for reading,

Michael

Python Lesson 13: Classes & Objects

Advertisements

Hello everybody,

It’s Michael, and today’s Python lesson will be on classes & objects in Python.

Python, like Java, is an object-oriented programming language. Everything in Python is an object, and each object in Python has its own attributes and methods (or functions in Python lingo). Classes in Python serve as object-builders (or blueprints for creating objects) just like classes in Java.

How do you create a class in Python? Let’s find out:

To create a class in Python, you simply need to use the keyword class. That’s it! No need to write public or private or anything like that in front of the class keyword like you would need to do in Java.

Alright, now let’s create an object of the class:

Creating an object of a Python class is similar to creating an object of a Java class, except in Java you would need the new keyword in front of the class name. Pretty simple right?

Now, I just demonstrated the simplest form of classes and objects. These examples wouldn’t serve you well when you’re working on any sort of Python programming project; classes and objects are more complex than what I just discussed.

To better understand classes and objects in Python, we need to understand the __init__ function. The __init__ function is built into every Python class, and it allows you to assign values to objects. Here’s an example of a class with the __init__ function:

In this class, the __init__ function has 4 parameters-self, year, month, and day. Year, month, and day are the variables for the RandomDate class.

There is also another parameter that I haven’t yet discussed-self. The self parameter refers to the class itself; it is used to access variables of the class, which in turn allows you to assign values to these variables. In the example above, I am able to access the year, month, and day variables by using self.year, self.month, and self.day, respectively.

  • Remember that in order to access the variables in the __init__ function, use this syntax: self.[variable name] = [variable name]
  • Remember that in order to access the variables in the __init__ function, use this syntax: self.[variable name] = [variable name]

You don’t absolutely have to call the first parameter self; you can name it whatever you want but it must be the first parameter of the function. Using the example above, let’s see what happens when we use another variable name instead of self:

This time, I replaced self with thedate but the code-block executed just fine since I remembered to use thedate as the first parameter.

Now, let’s add a method to class. Let’s also create a new object of the class and call the new method we created:

I added the function today to the RandomDate class and created a new object of the RandomDate class-r. The object r consists of the three main parameters of the RandomDate class-year, month, and day. The self (or thedate in this case) parameter isn’t included in the object creation, as it is only used to access the three main parameters.

Once I create the r object of the RandomDate class, I call the today function and get the output Today is September 20, 2020.

  • As you can see above, the three main parameters-2020, September, and 20-are all Strings. This is because you can only concatenate Strings with other Strings in Python. Just something to keep in mind when you’re writing your Python code.

Now that we have created an object of a class, let’s see how we can modify that object. First, let’s modify one of the properties of r:

In this example, I changed the value of day to 21 (in String format), called the today function again, and got the output Today is September 21, 2020.

Now, let’s say we wanted to get rid of the year. How do we do that? Take a look:

To delete an attribute from an object, follow this easy syntax: del [object name].[attribute to be deleted].

OK, so what if you wanted to get rid of the entire r object? This is what you would do:

To delete an entire object, just follow this very easy syntax: del [object name].

The last point I wanted to bring up regarding Python classes and objects is that, just like Python functions, they can’t be empty. However, the same empty-function workaround can be used for empty classes-simply using the keyword pass in the body of the function:

Thanks for reading,

Michael