VioletaBabel
2일차 본문
스레드
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; // 스레드 사용 namespace ConsoleApp1 { class Program { static void Fa() { for (int i = 0; i < 99; ++i) Console.WriteLine("a{0}", i); } static void Fb() { for (int i = 98; i > -1; --i) Console.WriteLine("b{0}", i); } static void Main(string[] args) { Thread t1 = new Thread(Fa); Thread t2 = new Thread(Fb); t1.Start(); t2.Start(); } } } | cs |
스레드 종료 대기
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; namespace ConsoleApp1 { class Program { static void Fa() { for (int i = 0; i < 99; ++i) Console.WriteLine("a{0}", i); } static void Fb() { for (int i = 98; i > -1; --i) Console.WriteLine("b{0}", i); } static void Main(string[] args) { Thread t1 = new Thread(Fa); Thread t2 = new Thread(Fb); t1.Start(); t2.Start(); t1.Join(); // t1이 끝날때까지 대기 Console.WriteLine("t1끝"); t2.Join(); Console.WriteLine("t2끝"); } } } | cs |
스레드 강제 종료 // 사용을 하지 않는 것을 추천. goto문과 같은 금기라고 보면 됨.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; // 스레드 사용 namespace ConsoleApp1 { class Program { static Thread t1 = new Thread(Fa); static Thread t2 = new Thread(Fb); static void Fa() { for (int i = 0; i < 99; ++i) { if (i > 2) t1.Abort(); // 스레드 강제 종료 Console.WriteLine("a{0}", i); } } static void Fb() { for (int i = 98; i > -1; --i) Console.WriteLine("b{0}", i); } static void Main(string[] args) { t2.Start(); t1.Start(); } } } | cs |
스레드 지연
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; // 스레드 사용 namespace ConsoleApp1 { class Program { static Thread t1 = new Thread(Fa); static Thread t2 = new Thread(Fb); static void Fa() { for (int i = 0; i < 99; ++i) { //스레드가 200밀리초 동안 지연 System.Threading.Thread.Sleep(200); Console.WriteLine("a{0}", i); } } static void Fb() { for (int i = 98; i > -1; --i) { //스레드가 100밀리초 동안 지연 System.Threading.Thread.Sleep(100); Console.WriteLine("b{0}", i); } } static void Main(string[] args) { t2.Start(); t1.Start(); } } } | cs |
스레드 일시정지
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; // 스레드 사용 namespace ConsoleApp1 { class Program { static Thread t1 = new Thread(Fa); static Thread t2 = new Thread(Fb); static void Fa() { for (int i = 0; i < 99; ++i) { if (i == 50) t1.Suspend();//일시정지 Console.WriteLine("a{0}", i); } } static void Fb() { for (int i = 98; i > -1; --i) { Console.WriteLine("b{0}", i); } t1.Resume();//t2가 끝나면 일시정지 해제 } static void Main(string[] args) { t2.Start(); t1.Start(); } } } | cs |
델리게이트
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; // 스레드 사용 namespace ConsoleApp1 { class Program { //델리게이트 생성 public delegate int Cal(int a, int b); public static int add(int a, int b) { return a+b; } public static int sub(int a, int b) { return a-b; } public static int mul(int a, int b) { return a*b; } public static int div(int a, int b) { return a/b; } static void Main(string[] args) { //델리게이트를 통해 스레드 사용 int x = 200, y = 50; Cal a = new Cal(add); Cal s = new Cal(sub); Cal m = new Cal(mul); Cal d = new Cal(div); Console.WriteLine("{0}\n{1}\n{2}\n{3}", a(x, y), s(x, y), m(x, y), d(x, y)); } } } | cs |
--
클래스
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | class Program { class Phone { string model; int ver; public void Call() { Console.WriteLine("전화"); } public void Web() { Console.WriteLine("인터넷"); } public void Photo() { Console.WriteLine("카메라"); } } static void Main(string[] args) { Phone p = new Phone(); p.Call(); p.Web(); p.Photo(); } } | cs |
sealed(상속 봉인. 상속이 불가능해지며, 이런 클래스를 봉인 클래스라고 함)
생성자와 소멸자
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | class Program { class Phone { string model; int ver; public Phone() // 생성자 { model = "iPhoneX"; ver = 11; } ~Phone() // 소멸자 { Console.WriteLine("종료"); } public void Call() { Console.WriteLine("전화"); } public void Print() { Console.WriteLine("{0} / {1}", model, ver); } } static void Main(string[] args) { Phone p = new Phone(); p.Print(); } } | cs |
상속
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | namespace ConsoleApp1 { class Character { public void Atk() { Console.WriteLine("공격"); } } class Healer : Character // 캐릭터 상속 { public void Heal() { Console.WriteLine("힐"); } } class Magician : Character { public void Magic() { Console.WriteLine("마법"); } } class Program { static void Main(string[] args) { Healer h = new Healer(); Magician m = new Magician(); h.Atk(); h.Heal(); m.Atk(); m.Magic(); } } } | cs |
is와 as
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | class Program { class Phone { } class Iphone : Phone { public void Os() { Console.WriteLine("ios"); } } class Galaxy : Phone { public void Os() { Console.WriteLine("android"); } } static void Main(string[] args) { Phone new1 = new Iphone(); Iphone i; //is는 객체가 해당 형식인지 검사해 bool로 return if(new1 is Iphone) { i = (Iphone)new1; i.Os(); } Phone new2 = new Galaxy(); //as는 형식 변환을 하되, 변환이 실패시 null이 됨 Galaxy g = new2 as Galaxy; if (g != null) g.Os(); //new1은 Iphone이었기에 변환이 실패 Galaxy g2 = new1 as Galaxy; if (g2 != null) g2.Os(); else Console.WriteLine("error"); } } | cs |
override
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | class Iphone { public virtual void Init() { // virtual로 오버라이딩을 통해 재정의 가능 Console.WriteLine("ios"); } } class Iphonex : Iphone { public override void Init() { // 오버라이딩 base.Init(); Console.WriteLine("Upgrade phone"); } } class Ipad : Iphone { public override void Init() { // 오버라이딩 base.Init(); Console.WriteLine("size up"); } } class Program { static void Main(string[] args) { Iphone i = new Iphone(); i.Init(); Console.WriteLine("---"); Iphone x = new Iphonex(); x.Init(); Console.WriteLine("---"); Iphone big = new Ipad(); big.Init(); } } | cs |
메소드 숨기기
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | class Iphone { public void Ver() { Console.WriteLine("iphone"); } } class Ipad : Iphone { public new void Ver() { Console.WriteLine("ipad"); } } class Program { static void Main(string[] args) { Iphone i1 = new Iphone(); i1.Ver(); // Iphone 클래스의 Ver Ipad i2 = new Ipad(); i2.Ver(); // Ipad 클래스의 Ver Iphone i3 = new Ipad(); i3.Ver(); // Iphone 클래스의 Ver } } | cs |
중첩 클래스
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | class Program { private int a = 1; class Inner { public void PrivateBreak() { Program p = new Program(); Console.WriteLine("{0}", p.a); //중첩 클래스를 이용하면 //상위 클래스의 프라이빗 멤버임에도 //접근이 가능해진다. } } static void Main(string[] args) { Inner i = new Inner(); i.PrivateBreak(); } } | cs |
중첩 클래스를 사용할 때 : 클래스 외부에 공개하고 싶지 않은 형식을 만들 때와 현재 클래스의 일부분처럼 표현하고픈 클래스를 만들 때. 은닉성은 파괴되지만 더 유연한 표현성을 제공.
분할 클래스
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | partial class A { // 분할 클래스. 클래스의 구현이 길어질 경우 // 여러 파일에 나눠 구현할 수 있도록 함 public void Say() { Console.WriteLine("1"); } public void Say2() { Console.WriteLine("2"); } } partial class A { public void Say3() { Console.WriteLine("3"); } } class Program { static void Main(string[] args) { A a = new A(); a.Say(); a.Say2(); a.Say3(); } } | cs |
확장 메소드
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | /* 확장 메소드 : 기존의 클래스를 확장하는 기법. * 아래는 int에 제곱 연산 기능을 추가한 것이다. * static 한정자를 사용해야 하고, 메소드를 선언하고 * 첫 매개 변수는 this 키워드와 함께 * 확장하고픈 클래스(형식)의 인스턴스를 사용한다. */ public static class IntPower { public static int Power(this int i, int e) { int r = i; for (int j = 1; j < e; ++j) r *= i; return r; } } class Program { static void Main(string[] args) { Console.WriteLine("{0}^2 : {1}", 3, 3.Power(2)); Console.WriteLine("{0}^{1} : {2}", 3, 3, 3.Power(3)); Console.WriteLine("{0}^{1} : {2}", 2, 10, 2.Power(10)); } } | cs |
Comments