๐ฆ The Tale of Class and Instance
Once upon a time, there was a magical blueprint called Person.
This blueprint described how a person behaves:
-
They have a name.
-
They can eat.
-
They can sleep.
-
They can work.
This blueprint is called a class in programming.
But a class is just an idea until we use it to make something real — like creating Seojun and Jia from the Person class.
These are called instances.
๐ฎ The Magical Code
using System;
class Person {
public string Name;
public Person(string name)
{
Name = name;
}
public void Eat()
{
Console.WriteLine(Name + " is eating.");
}
public void Sleep()
{
Console.WriteLine(Name + " is sleeping.");
}
public void Work()
{
Console.WriteLine(Name + " is working.");
}
}
class Program {
public static void Main (string [] args)
{
Person p1 = new Person("Seojun");
Person p2 = new Person("Jia");
p1.Eat();
p2.Eat();
p1.Sleep();
p1.Work();
}
}
๐ค Console Output
Comments
Post a Comment