http://dis.dankook.ac.kr/lectures/hci10/entry/Method-Overloading-vs-Method-Overriding
Method Overloading: 동일한 함수명에 매개변수가 다른 함수를 둘 이상 정의하는 것으로, 동일한 함수 기능을 수행하지만 다른 매개변수의 경우를 처리할 때 사용
Method Overriding: 상속받은 파생 클래스에서 동일한 함수명에 동일한 매개변수로 정의하여 함수를 재정의하는 것으로 상속되어진 함수의 기능을 변경해서 재사용하고 싶을 때 사용
{
protected static int count = 0;
protected int x, y;
public int X
{
{
set { this.y = value; }
{
public Point() : this(0, 0) { }
public Point(int x, int y) { this.x = x; this.y = y; count++; }
~Point() { count–; }
public void SetPosition(int x, int y) { this.x = x; this.y = y; }
public void Move(int x, int y) { this.x += x; this.y += y; }
public override string ToString() { return (String.Format(“({0}, {1})”, x, y)); }
public virtual void Print() { Console.WriteLine(“X={0} Y={1}”, x, y); }public virtual void Sum(Point other) { x += other.X; y += other.Y; }
public static int GetCount() { return count; }
// operator++ overload
public static Point operator++(Point p) { ++p.x; ++p.y; return p; }
// operator== overload
public static bool operator==(Point p1, Point p2) { return p1.Equals(p2); }
// operator!= overload
public static bool operator!=(Point p1, Point p2) { return !p1.Equals(p2); }
public override int GetHashCode() { return x ^ y; }
public override bool Equals(object obj)
{
// IEquatable
public virtual bool Equals(Point other)
{
public int CompareTo(Point other)
{
{
else if (this.x < other.x)
{
else
{
}
{// instance member field
protected int z;
{
set { this.z = value; }
// constructor & destructor
public Point3D() : this(0, 0, 0) { }
public Point3D(int x, int y, int z) : base(x, y) { this.z = z; }
~Point3D() { }
public void SetPosition(int x, int y, int z) { base.SetPosition(x, y); this.z = z; }
public void Move(int x, int y, int z) { base.Move(x, y); this.z += z; }
// override method
public override string ToString() { return (String.Format(“({0}, {1}, {2})”, x, y, z)); }
public override void Print() { Console.WriteLine(“X={0} Y={1} Z={2}”, x, y, z); }
public override void Sum(Point other)
{
// overload method
public void Sum(Point3D other) { x += other.X; y += other.Y; z += other.Z; }
// operator++ overload
public static Point3D operator++(Point3D p) { ++p.x; ++p.y; ++p.z; return p; }
// operator== overload
public static bool operator ==(Point3D p1, Point3D p2) { return p1.Equals(p2); }
// operator!= overload
public static bool operator !=(Point3D p1, Point3D p2) { return !p1.Equals(p2); }
public override int GetHashCode() { return x ^ y ^ z; }
public override bool Equals(object obj)
{
if (!(obj is Point3D))
return false;
{if (other is Point3D))return Equals((Point3D)other);
else
}
// IEquatable
public bool Equals(Point3D other)
{
public int CompareTo(Point3D other)
{
{
else if (this.x < other.x)
{
else if ((this.x == other.x) && (this.y < other.y))
{
else if ((this.x == other.x) && (this.y == other.y) && (this.z < other.z))
{
else
{
}
{
static void Swap(int p1, int p2)
{
p = p1;
p1 = p2;
p2 = p;
// pass-by-reference (value type)
static void Swap(ref int p1, ref int p2)
{
p = p1;
p1 = p2;
p2 = p;
// pass-by-value (reference type)
static void Swap(int[] p1, int[] p2)
{
p = p1;
p1 = p2;
p2 = p;
// pass-by-value (reference type)
static void Swap(ref int[] p1, ref int[] p2)
{
p = p1;
p1 = p2;
p2 = p;
static void Swap(ref Point p1, ref Point p2)
{
p = p1;
p1 = p2;
p2 = p;
static void Swap(ref Point3D p1, ref Point3D p2)
{
p = p1;
p1 = p2;
p2 = p;
static void Main(string[] args)
{
int i = 5, j = 10;
Console.WriteLine(“Before: i={0}, j={1}”, i, j); // i=5, j=10
Swap(i, j);
Console.WriteLine(“After Swap(i,j): i={0}, j={1}”, i, j); // i=5, j=10
Swap(ref i, ref j);
Console.WriteLine(“After Swap(ref i, ref j): i={0}, j={1}”, i, j); // i=10, j=5
int[] arr1 = { 1, 2, 3 };
int[] arr2 = { 4, 5, 6, 7, 8 };
Console.WriteLine(“Before array pass-by-value swap: “);
Console.Write(“arr1: “);
foreach (int a in arr1) Console.Write(“{0} “, a); // arr1: 1 2 3
Console.WriteLine();
Console.Write(“arr2: “);
Console.WriteLine();
Swap(arr1, arr2);
Console.WriteLine(“After Swap(arr1, arr2): “);
Console.Write(“arr1: “);
foreach (int a in arr1) Console.Write(“{0} “, a); // arr1: 1 2 3
Console.WriteLine();
Console.Write(“arr2: “);
foreach (int a in arr2) Console.Write(“{0} “, a); // arr2: 4 5 6 7 8
Console.WriteLine();
Swap(ref arr1, ref arr2);
Console.WriteLine(“After Swap(ref arra1, ref arr2): “);
Console.Write(“arr1: “);
foreach (int a in arr1) Console.Write(“{0} “, a); // arr1: 4 5 6 7 8
Console.WriteLine();
Console.Write(“arr2: “);
foreach (int a in arr2) Console.Write(“{0} “, a); // arr2: 1 2 3
Console.WriteLine();
Console.Write(“p1: “);
Point p1 = new Point();
p1.Print(); // Point.Print() (0, 0)
Console.Write(“p2: “);
Point p2 = new Point();
Console.Write(“p1 SetPosition: “);
p1.Print(); // Point.Print() (10, 10)
p1.Move(20, 50); // Point.Move(int, int)
Console.Write(“p1 Move: “);
Console.WriteLine(“p1=” + p1); // Point.ToString() (30, 60)
p2.SetPosition(20, 30); // Point.SetPosition(int, int)
Console.Write(“p2 SetPosition: “);
p2.Print(); // Point.Print() (20, 30)
Console.WriteLine(“\nSwap”);
Swap(ref p1, ref p2);
Console.WriteLine(“p1: {0}”, p1); // Point.ToString() (20, 30)
Console.WriteLine(“p2: {0}”, p2); // Point.ToString() (30, 60)
Console.WriteLine();
Console.WriteLine(“p1.m_nX={0}, p1.m_nY={1}, Point.GetCount()={2}”, p1.X, p1.Y, Point.Count);
Console.WriteLine(“p2.m_nX={0}, p2.m_nY={1}, Point.GetCount()={2}”, p2.X, p2.Y, Point.Count);
Console.Write(“p3: “);
Point3D p3 = new Point3D();
p3.Print(); // Point3D.Print() (0, 0, 0)
Console.Write(“p4: “);
Point3D p4 = new Point3D();
p4.Print(); // Point3D.Print() (0, 0, 0)
Console.WriteLine();
p3.SetPosition(10, 20); // Point3D.SetPosition(int,int)
Console.Write(“p3 SetPosition: “);
p3.Print(); // Point3D.Print() (10, 20, 0)
p3.SetPosition(30, 40, 50); // Point3D.SetPosition(int,int,int)
Console.Write(“p3 SetPosition: “);
p3.Print(); // Point3D.Print() (30, 40, 50)
++p3; // Point3D.operator++
Console.Write(“++p3: “);
p3.Print(); // Point3D.Print() (31, 41, 51)
Console.Write(“p3 Move: “);
p3.Print(); // Point3D.Print() (51, 71, 91)
p3.Move(10, 10); // Point3D.Move(int,int)
Console.Write(“p3 Move: “);
Console.WriteLine(p3); // Point3D.ToString() (61, 81, 91)
Console.WriteLine();
p4.SetPosition(100, 200, 300); // Point3D.SetPosition(int,int,int)
Console.Write(“p4 SetPosition: “);
p4.Print(); // Point3D.Print() (100, 200, 300)
p4.Move(10, 10, 10); // Point.Move(int, int,int)
Console.Write(“p4 Move: “);
p4.Print(); // Point3D.Print() (110, 210, 310)
Console.WriteLine(“\nSUM”);
p3.Sum(p4); // Point3D.Sum(Point3D) (61+110, 81+210, 91+310)
p3.Print(); // Point3D.Print() (171, 291, 401)
Console.WriteLine(“\nSwap”);
Swap(ref p3, ref p4); // (171,291,401) <-> (110,210,310)
Console.WriteLine(“p1: {0}”, p3); // Point3D.ToString() (110,210,310)
Console.WriteLine(“p2: {0}”, p4); // Point3D.ToString() (171,291,401)
Console.WriteLine();
Console.WriteLine(“p3.m_nX={0}, p3.m_nY={1}, p3.m_nZ={2}, Point.GetCount()={3}”, p3.X, p3.Y, p3.Z, Point.GetCount());
Console.WriteLine(“p4.m_nX={0}, p4.m_nY={1}, p4.m_nZ={2}, Point.count={3}”, p4.X, p4.Y, p4.Z, Point.Count);
if (p3 == p4) Console.WriteLine(“p3 == p4”); // Point3D.operator==
if (p3 != p4) Console.WriteLine(“p3 != p4”); // Point3D.operator!=
Console.Write(“p5: “);
Point p5 = p3; // upcasting – P5는 Point 타입인 Point3D 객체
p5.Print(); // Point3D.Print() (late binding) (110,210,310)
p5.SetPosition(10,20); // Point.SetPosition(int,int)
((Point3D)p5).SetPosition(10,20,30); // downcasting – Point3D.SetPosition(int,int,int)
Point p6 = new Point3D(110, 210, 310); // upcasting – p6도 Point 타입인 Point3D 객체
p6.Print(); // Point3D.Print() (late binding) (110,210,310)
Console.WriteLine(p7); // Point3D.ToString()
if (p7 == p8) Console.WriteLine(“p7 == p8”); // p7 == p8