///// 프로퍼티 /////////
using System;
class test
{
private int a;
public int A
{
get
{
Console.Write("get");
this.a = a+2;
return a;
}
set {
Console.WriteLine("set");
this.a = value; }
}
}
class jks
{
static void Main()
{
test t = new test();
t.A = 2;
Console.WriteLine("{0}",t.A);
}
}
using System;
class jksPro
{
private string Name;
public string NameP
{
get
{
string newName = this.Name + "추가문장";
Console.WriteLine("이름GET");
return newName;
}
set
{
Console.WriteLine("SET");
this.Name = value;
}
}
}
class MainClass
{
static void Main()
{
jksPro jks = new jksPro();
string value = Console.ReadLine();
Console.WriteLine("값을 입력하시요");
jks.NameP = value;
Console.WriteLine("{0}",jks.NameP);
}
}
///// 인덱서 ///////////
인덱서(indexer)
일련의 데이터 값을 가질 수 있는 데이터 구조가 필요
사용할 데이터형을 지정하고, 클래스 자체를 배열처럼 사용하기 때문에
this를 사용하고 [ ] 사이에 인덱스로 사용할 데이터형을 지정
프로퍼티와 마찬가지로 get/set 메서드가 불려지며, 특정 값을 가리키는 인덱스를 지정
예제)
using System;
// ArrayList 콜렉션 클래스를 다루기 위해 Collections 네임스페이스를 추가
using System.Collections;
// ObjList 클래스를 정의하고, 멤버로 ArrayList 클래스를 추가
public class ObjList
{
protected ArrayList data; // ArrayList 객체
public ObjList()
{
data = new ArrayList();
// ArrayList 클래스는 동적으로 배열 크기를 관리하며,
// 내부적으로 관리하는 데이터는 object 형
}
public object this[int i]
{
// ObjList 클래스에 다음과 같이 인덱서를 추가하고,
// get 메서드에서 값을 리턴하며, 인덱스가 범위를 벗어나는지 검사해서 예외 처리
get
{
if (i >= 0 && i < data.Count) // 인덱스 범위 검사
{
return data[i];
}
else
throw new IndexOutOfRangeException("범위 벗어남" + i);
}
// set 메서드에서 값을 넣을 때도 마찬가지로 범위를 검사해서 범위 내에들어있는
// 경우에만 값을 넣어주며, 마찬가지로 범위를 벗어나는 경우는 예외 처리
set
{
if (i >= 0 && i < data.Count) // 인덱스 범위 검사
{
data[i] = value;
}
else
if (i == data.Count) // 마지막 데이터 요소인 경우
{
data.Add(value); // 데이터를 추가합니다.
}
else
throw new IndexOutOfRangeException("범위 벗어남" + i);
}
}
}
public class Class1
{
public static void Main()
{
// 인덱서에 여러 데이터형을 가진 값을 넣고, 값을 출력
ObjList list = new ObjList();
list[0] = 100;
list[1] = "apple";
list[2] = 123.456;
for (int i = 0; i < 3; i++)
Console.WriteLine("[{0}] = {1}", i, list[i]);
}
}
////////////////// 간단한 쉬운 인덱스 예제~!!!!!!!!!!!!!!!!
using System;
class NameCard
{
private string[] names;
public NameCard(int i)
{
names = new string[i];
}
public string this[int index]
{
get
{
if (names[index] == null)
return "get값이없네 ?";
else
return names[index];
}
set
{
if(value == null)
Console.WriteLine("null 값은 저장할수없습니다");
else
names[index] = value;
}
}
}
class MainApp
{
static void Main()
{
const int max = 5;
NameCard my = new NameCard(max);
my[0] = "전광식";
// my[1] = "찌질이";
my[2] = "권오윤";
my[3] = null;
my[4] = "명건이";
for (int i = 0; i < max; i++)
Console.WriteLine(my[i]);
}
}
/////////////////// //Interfacs에서 property 선언 ///////////////////////////
using System;
interface INameCard
{
string Name //Interfacs에서 property 선언
{
get;
set;
}
string Telephone
{
get;
set;
}
}
class NameCard : INameCard
{
private string name;
private string tel;
private static int cnt;
public NameCard()
{
cnt++;
}
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public string Telephone
{
get { return tel; }
set { tel = value; }
}
public static int Count{ get{return cnt; } }
}
class MainApp
{
public static void Main()
{
NameCard my = new NameCard();
my.Name = "박상현";
my.Telephone = "555-2424";
Console.WriteLine("제 이름은 {0}입니다.", my.Name);
Console.WriteLine("전화번호는 {0}예요.", my.Telephone);
Console.WriteLine("현재 생성된 객체의 수 : {0}", NameCard.Count);
Console.WriteLine();
NameCard you = new NameCard();
you.Name = "채진실";
you.Telephone = "723-9292";
Console.WriteLine("제 이름은 {0}입니다.", you.Name);
Console.WriteLine("전화번호는 {0}예요.", you.Telephone);
Console.WriteLine("현재 생성된 객체의 수 : {0}", NameCard.Count);
}
}
'.NET > C# Basic' 카테고리의 다른 글
C# 켈렉션 콜렉션 ArrayList Hashtable SortedList Stack (0) | 2008.10.16 |
---|---|
닷넷 클래스 object is 연산자 GetHashCode() (0) | 2008.10.16 |
닷넷 클래스 리플렉션 reflection (0) | 2008.10.15 |
클래스관련 const 상수 readonly 구조체관련 GetType() , is as (0) | 2008.10.15 |
연산자 메서드 operator +(), operator-() , 연산자 오버로딩? (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 |