연산자 메서드를 부르려면 해당 연산자를 사용하며,
연산자를 사용하면 코드가 훨씬 직관적이기 때문에 이해하기 쉬워진다
using System;
class Point
{
public int x;
public int y;
public Point( int x, int y )
{
this.x = x;
this.y = y;
}
public override string ToString()
{
// (x,y) 값
return String.Format("({0},{1})", x, y );
}
// + 연산자 메서드
public static Point operator +(Point pt1, Point pt2)
{
return (new Point(pt1.x + pt2.x, pt1.y + pt2.y));
}
// - 연산자 메서드
public static Point operator -(Point pt1, Point pt2)
{
return (new Point(pt1.x - pt2.x, pt1.y - pt2.y));
}
}
class Class1
{
static void Main(string[] args)
{
Point pt1 = new Point(100, 100);
Point pt2 = new Point(100, 200);
Point pt3, pt4;
// Point 객체 + 연산
pt3 = pt1 + pt2;
pt4 = pt1 - pt2;
Console.WriteLine("{0} + {1} = {2}__{3}", pt1, pt2, pt3, pt4);
}
}
모르겠쌈~~~~~~~~! 갈쳐줘 ㅋㅋ
C#에서 제공하는 연산자
C#에서 제공되는 연산자 중에서 일부 연산자를 클래스에 정의해서 쓸 수 있다
C# 연산자 중에서 연산자 오버로딩이 되는 것과 안 되는 것을 구분
연산자 오버로딩할 수 있는 연산자
unary 연산자 + - ! ++ -- true false
binary 연산자 + - * / % & | ^ << >>
비교 연산자 == != < > <= >=
연산자 오버로딩할 수 없는 연산자
&&, ||, [], ()
+=, -=, *=, /=, %=
= . ?: -> new is as sizeof typeof 등등
'.NET > C# Basic' 카테고리의 다른 글
닷넷 클래스 object is 연산자 GetHashCode() (0) | 2008.10.16 |
---|---|
닷넷 클래스 리플렉션 reflection (0) | 2008.10.15 |
클래스관련 const 상수 readonly 구조체관련 GetType() , is as (0) | 2008.10.15 |
프로퍼티 인덱서 (0) | 2008.10.15 |
static정적맴버 배열 연산자 is/as foreach 문 메서드 오버로딩 메서드 가변 인자 (0) | 2008.10.14 |
인터페이스(interface) 와 추상화클래스(abstract) 예제 (0) | 2008.10.14 |
C# Delegate와 Event 중요 (0) | 2008.10.13 |
Console.Read() 와 Console.ReadLine() 차이 (0) | 2008.10.10 |