반응형
using System;
class Sizes
{
public const int Medium = 100; // 상수로써 사용되는것 const 항상동일한수
public const int Large = 105; // const 라고 붙은것은 별도의 객체생성없이도 가능
public const int XLarge = 110;
}
class Shirt
{
public static void Main()
{
while(true)
{
string s;
Console.WriteLine("치수 기호를 입력하세요. (M)edium (L)arge (X)Large (q)uit");
s = Console.ReadLine();
if(s == "q") break;
switch(s)
{
case "M" :
Console.WriteLine(Sizes.Medium); //객체선언없이 가능
break;
case "L" :
Console.WriteLine(Sizes.Large);
break;
case "X" :
Console.WriteLine(Sizes.XLarge);
break;
default :
Console.WriteLine("저희는 그런 치수를 가지고 있지 않습니다.");
break;
}
}
}
}
/////////////////// / 읽기전용 readonly
using System;
class Sizes
{
public readonly int Medium = 100; // 읽기전용 readonly
public readonly int Large = 105; // 변수 초기화부분
public readonly int XLarge; //
public Sizes()
{
this.XLarge = 110; // 생성자의 경우는 가능
}
}
class Shirt
{
public static void Main()
{
Sizes a = new Sizes();
Console.WriteLine("Medium : {0} ", a.Medium);
Console.WriteLine("Large : {0} ", a.Large);
Console.WriteLine("XLarge : {0}", a.XLarge);
}
}
///////////////////////////////////
public struct Point //구조체
{
public int x;
public int y;
//생성자
public Point(int x,int y)
{
this.x = x;
this.y = y;
}
}
class Structure
{
public static void Main()
{
//new를 이용해 생성자를 호출한다.
Point p = new Point(30, 70); // 구조체 안에있는 맴버를 불러올때
Console.WriteLine("I'm at Heap!");
Console.WriteLine("Point.x : {0}, Point.y : {1}", p.x, p.y);
//new 없이 객체를 선언하면 모든 멤버를 초기화 해줘야 합니다.
Point s; // 구조체 선언
s.x = 20; // 초기화해주어야한다
s.y = -49; //
Console.WriteLine("I'm at Stack!");
Console.WriteLine("Point.x : {0}, Point.y : {1}", s.x, s.y);
}
}
using System;
interface IPrinter
{
void Print();
}
class Printer : IPrinter
{
public void Print()
{
Console.WriteLine("난 프린터라오.");
}
}
struct Paper
{
}
class WhoamI
{
public static void Main()
{
IPrinter p = new Printer();
p.Print();
Console.WriteLine("객체 p는 누구일까요? : {0}", p.GetType());
Paper sheet = new Paper();
Console.WriteLine("객체 sheet는 누구일까요? : {0}", sheet.GetType());
int i = 3;
Console.WriteLine("객체 i는 누구일까요? : {0}", i.GetType());
}
}
[5.클래스 관련예제 \ Is.cs]
using System;
interface IMammal
{
void Walk();
}
class Cat : IMammal
{
public void Walk()
{
Console.WriteLine("고양이과는 걸을 수 있습니다.");
}
}
class Lion : Cat, IMammal //
{
public new void Walk() // new 키워드로 재정의
{
Console.WriteLine("사자는 걸을 수 있습니다.");
}
}
class WhoamI
{
public static void Main()
{
IMammal simba = new Lion(); //
Cat leo = new Cat();
Console.WriteLine("Is Simba a Cat? : {0}", simba is Cat);
Console.WriteLine("Is leo a Lion? : {0}", leo is Lion);
}
}
[5.클래스 관련예제 \ As.cs]
using System;
interface IMammal
{
void Walk();
}
class Cat : IMammal
{
public void Walk()
{
Console.WriteLine("고양이과는 걸을 수 있습니다.");
}
}
class Lion : Cat, IMammal
{
public new void Walk()
{
Console.WriteLine("사자는 걸을 수 있습니다.");
}
}
class WhoamI
{
public static void Main()
{
IMammal simba = new Lion();
object obj = simba as Cat; //
if( obj != null)
Console.WriteLine("Is Simba a Cat? : {0}", obj.GetType());
else
Console.WriteLine("저는 고양이가 아닌디유...");
Cat leo = new Cat();
object obj2 = leo as Lion;
if( obj2 != null)
Console.WriteLine("Is leo a Lion? : {0}", obj2.GetType());
else
Console.WriteLine("저는 사자가 아닌디유...");
}
}
'.NET > C# Basic' 카테고리의 다른 글
C# 어트리뷰트 예외처리 (0) | 2008.10.16 |
---|---|
C# 켈렉션 콜렉션 ArrayList Hashtable SortedList Stack (0) | 2008.10.16 |
닷넷 클래스 object is 연산자 GetHashCode() (0) | 2008.10.16 |
닷넷 클래스 리플렉션 reflection (0) | 2008.10.15 |
프로퍼티 인덱서 (0) | 2008.10.15 |
연산자 메서드 operator +(), operator-() , 연산자 오버로딩? (0) | 2008.10.15 |
static정적맴버 배열 연산자 is/as foreach 문 메서드 오버로딩 메서드 가변 인자 (0) | 2008.10.14 |
인터페이스(interface) 와 추상화클래스(abstract) 예제 (0) | 2008.10.14 |