override vs new

namespace PersonTest
{
class Person
{
static int count = 0;   // static (class) variables

string name;    // instance variables
int age;        // instance variables

public string Name
{
get
{
return name;
}
set
{
name = value;
}
}

public int Age
{
get
{
return age;
}
set
{
age = value;
}
}

public Person() : this(“”, 0)
{
}

public Person(string name, int age)
{
count++;
this.name = name;
this.age = age;
}

public virtual void Print()    // instance methods
{
Console.WriteLine(“Person Name: {0} Age: {1}”, name, age);
}

public override string ToString()      // instance methods
{
return “Person Name: ” + name + ” Age: ” + age;
}

public static int GetCount() { return count; }  // static (class) methods
public static void PrintCount() { Console.WriteLine(“Person Count: ” + count); }  // static (class) methods
}
}

namespace PersonTest
{
class Student : Person
{
static int scount = 0; // static (class) variables
int id;
public int ID
{
get
{
return id;
}
set
{
id = value;
}
}

public Student() : this(“”, 0, 5208) // Student() => public Student : base()와 같은 의미, 생성자는 상속안됨
{
}

public Student(string name, int age, int id)
: base(name, age)
{
this.id = id;
scount++;
}

public void BasePrint()
{
base.Print();
}

public override void Print() // instance methods
{
Console.WriteLine(“Student Name: {0} Age: {1} Id: {2}”, Name, Age, ID);
}

public override string ToString()  // instance methods
{
return “Student Name: ” + Name + ” Age: ” + Age + ” Id: ” + ID;
}

// Person 클래스의 GetCount & PrintCount와 동일한 이름이므로, Student 감춤. new 키워드를 사용하여 새로정의함.
public static new int GetCount() { return scount; } // static (class) methods
public static new void PrintCount() { Console.WriteLine(“Student Count: ” + scount); }  // static (class) methods
}
}

namespace PersonTest
{
class Program
{
static void Main(string[] args)
{
Person p1 = new Student(“Kyoung”, 22, 1207); // up casting
p1.Age = 30;
p1.Print(); // dynamic binding Student Name: Kyoung Age: 30 ID: 1207
//p1.BasePrint(); // cannot call Student method due to Person
//p1.PrintCount(); // cannot call static method with instance name
Person.PrintCount(); // calls Person.PrintCount() 1
Student.PrintCount(); // calls Student.PrintCount() 1

Student s1 = (Student)p1; // down casting
s1.Name = “HCI”;
s1.Age = 2;
s1.ID = 2016;
s1.Print(); // Student Name: HCI Age: 2 ID: 2016
s1.BasePrint(); // Person Name: HCI Age: 2
Person.PrintCount(); // calls Person.PrintCount() 1
Student.PrintCount(); // calls Student.PrintCount() 1

Student s2 = new Student(“Shin”, 20, 1207);
s2.Print(); // Student Name: Shin Age: 20 ID: 1207
s2.BasePrint(); // Person Name: Shin Age: 20
Person.PrintCount(); // calls Person.PrintCount() 2
Student.PrintCount(); // calls Student.PrintCount() 2

Person p2 = new Person(“Park”, 10);
p2.Print(); // Person Name: Park Age: 10
Person.PrintCount(); // calls Person.PrintCount() 3
Student.PrintCount(); // calls Student.PrintCount() 2

Person p3 = new Person();
p3.Print(); // Person Name: Age: 0
Person.PrintCount(); // calls Person.PrintCount() 4
Student.PrintCount(); // calls Student.PrintCount() 2

Console.WriteLine(“Number of Person: {0}”, Person.GetCount());
Console.WriteLine(“Number of Student: {0}”, Student.GetCount());
}
}
}