+1

(Basic) Implementing Tony Stark

Backgound

Now that we have these two kind of things in our hand (see my previous article), why don't we just create something real from it. A living "instance". A first living being in our universe. Something, or someone, we can put a name on. I'm thinking about it right now. And.. "Tony Stark"?.

"Objection..!"

"Not every being is a human. It could be a plant. Or an animal, right?" So, why don't we just create a classification first, so we will never put "a monkey" in the same class as "a tony stark". "Hey, Tony, I just saved your day, man. You're welcome."

Creating the human race

Now for you interface lovers, do your job, ok. Let's do this in your side :

Interface

public interface IHuman : IBeing
{
    int Intelligence { get; }
}

"Hey why you put that attribute 'intelligence' right there? Do you mean animals or plants don't have one? Ok, plants maybe don't, but animals?" Ok, so, let's clear it up a bit first. Let's consider this as the pre-requisite attribute for mankind to build civilizations. Animals don't civilize themself, right. They just here to stay alive. Hey, why we even discuss this? I just wanna give you a sample about how a derived object can still have its own specific behaviour. So, for now, let's move on, shall we?

Now for you class admirers, you could also do this in your side :

Class

public abstract class CHuman : CBeing
{
    public abstract int Intelligence { get; }
}

Now for you both, just spread your smile to each other ok, because we're still on the same page, actually. So, smile..!

The first implementation of a human being

Now, we are all set to implement the first instance in our universe : a human being.

Sstt.. interface lovers, please do this, will you :

Interface

    public class Human : IHuman
    {
        int id, age, healthPoints, stamina, intelligence;
        string name;

        public Human(int id, string name, int age, int stamina, int intelligence)
        {
            this.id = id;
            this.name = name;
            this.age = age;
            this.stamina = stamina;
            this.intelligence = intelligence;

            this.healthPoints = 100;
        }

        public int Id
        {
            get { return id; }
        }

        public string Name
        {
            get { return name; }
        }

        public int Age
        {
            get { return age; }
        }

        public int HealthPoints
        {
            get { return healthPoints; }
        }

        public int Stamina
        {
            get { return stamina; }
        }

        public int Intelligence
        {
            get { return intelligence; }
        }
    }

And you fans of the class, please do your part this way :

Class

    public class Human : CHuman
    {
        int id, age, healthPoints, stamina, intelligence;
        string name;

        public Human(int id, string name, int age, int stamina, int intelligence)
        {
            this.id = id;
            this.name = name;
            this.age = age;
            this.stamina = stamina;
            this.intelligence = intelligence;

            this.healthPoints = 100;
        }

        public override int Id
        {
            get { return id; }
        }

        public override string Name
        {
            get { return name; }
        }

        public override int Age
        {
            get { return age; }
        }

        public override int HealthPoints
        {
            get { return healthPoints; }
        }

        public override int Stamina
        {
            get { return stamina; }
        }

        public override int Intelligence
        {
            get { return intelligence; }
        }
    }

Excellent! Officially, we could call this now an "implementation". Why? Because we just deal with how this thing actually works. A thing that ready to be instantiated. We have created a real mechanism. For example, we make every human gain 100% health points in its creation, and so on. This is the implementation is all about.

So, let's take a look at those two approaches. The interface implements things in a clearest way possible. While the derived class must use the "override" keyword to implement abstract attributes. Somehow, this could make the class looks bad compared to the interface. But let me tell you that it's not the case why so many people turn to the interface for their abstraction layers, by the way. I'll show you next time, ok?

Now is the time, when this universe will become a more exciting place to live. Ladies and gentlemen, let me introduce you with our first creation. It's a thing. A being. Or, to be precise, a human being.

And the name is..

public void CreatingTonyStark()
{
    Human tonyStark = new Human(1, "Tony Stark", 30, 50, 170);
}

..Tony Stark!

TonyStark-1024x640.jpeg

"Wait a minute.. Isn't that Robert Downey Jr.?" Errr.. Yes, if you say so. Just pass the name "Robert Downey Jr." in the "constructor" and you're good to go.

"What do you mean with a constructor?" Oh yes, I'm sorry, I forgot to mention this term. Let's just say, when you create an instance (like the sample code above), you do it by calling a constructor. Just let it know that you need "Tony Stark" (or "Robert Downey Jr."), and (voila!) you will get what you wish for, ok? Cool?

So what we have learned?

###Implementation always deal with how objects actually implement their behaviours. How they are ready to become instances, so we can actually use them.

Next chapter

A new frontier awaits us from this point. So wide open that I couldn't keep everything in one linear tutorial anymore. So, after this, please expect more plots, and sub-plots. Sequels, prequels or even spin-offs. Just follow the rabbit hole, ok? If you don't see such a hole, just click any link that seems interesting to you. If you still here with me, of course.

But for now, I think I need to make sure that this guy is really the man I know. Let's do some simple tests on him first (A.K.A our first Unit Test Project). This way, please!

[Updated]

Or, maybe you are too curious to see how the human race will reach the civilization. So, skip the test? You got it. Let's go to my next article, then!


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí