# 1교시 C#
& 일반화 메소드
-데이터 형식을 일반화한 메소드
-일반화 예제p.345
line 7 CopyArray 일반화
line 18 int 로 선언
line 26 string 로 선언
& 일반화 클래스
& 형식 매개 변수 제약시키기
p.352
where: T : U
:부모가 인트면 자식도 인트라는뜻
p.353 예제
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConstraintsOnTypeParameters
{
class StructArray<T> where T : struct
{
public T[] Array { get; set; }
public StructArray(int size)
{
Array = new T[size];
}
}
class RefArray<T> where T : class
{
public T[] Array { get; set; }
public RefArray(int size)
{
Array = new T[size];
}
}
class Base { }
class Derived : Base { }
class BaseArray<U> where U : Base
{
public U[] Array { get; set; }
public BaseArray(int size)
{
Array = new U[size];
}
public void CopyArray<T>(T[] Source) where T : U
{
Source.CopyTo(Array, 0);
}
}
class MainApp
{
public static T CreateInstance<T>() where T : new()
{
return new T();
}
static void Main(string[] args)
{
StructArray<int> a = new StructArray<int>(3);
a.Array[0] = 0;
a.Array[1] = 1;
a.Array[2] = 2;
RefArray<StructArray<double>> b = new RefArray<StructArray<double>>(3);
b.Array[0] = new StructArray<double>(5);
b.Array[1] = new StructArray<double>(10);
b.Array[2] = new StructArray<double>(1005);
BaseArray<Base> c = new BaseArray<Base>(3);
c.Array[0] = new Base();
c.Array[1] = new Derived();
c.Array[2] = CreateInstance<Base>();
BaseArray<Derived> d = new BaseArray<Derived>(3);
d.Array[0] = new Derived();
d.Array[1] = CreateInstance<Derived>();
d.Array[2] = CreateInstance<Derived>();
BaseArray<Derived> e = new BaseArray<Derived>(3);
e.CopyArray<Derived>(d.Array);
}
}
}
-소스를 작성할때는 형을 고민하지 않아도 되서 편할수있고
사용할때 선언을 해야함
& p.368 예외처리하기
-인덱스가 배열 범위를 벗어나서 예외발생
& try~catch로 예외받기
-앞의 실행에서 오류난부분을 try~catch로 해결할수있습니다.
using System;
public class MainApp
{
public static void Main()
{
try
{
int zero = 0;
int j = 3/zero; // 예외 발생!
}
catch(Exception e) // Exception e라는 객체생성
{
Console.WriteLine("예외 발생 : {0}", e.Message); // 부모객체의 Meggage를 호출
}
}
}
& System.Exception 클래스
-System.Exception 클래스는 모든 예외의 조상입니다.
프로그래머가 발생할것으로 계산한 예외말고도 다른예외까지 받아낼수있기때문에 사용을 자제하는것이 좋을것같다
& TRY ~CATCH와 FINALLY
-try 블록에서 코드를 실행하다가 예외가 던져지면 프로그램의 실행이 catch 절로 바로 뛰어넘어옵니다.
예외때문에 try블록의 자원 해제같은 중요한 코드를 미처 실행하지 못한다면이는 곧 버그를 만드는 원인이 되기때문에 finally사용
[9.예외처리 (Exception Handling)예제 \ finally.cs]
using System;
public class MainApp
{
public static void Main()
{
try
{
Console.WriteLine("여기는 try 블록 시작");
int zero = 0;
int j = 3/zero; // 예외 발생!
Console.WriteLine("여기는 try 블록 끝");
}
catch(Exception e)
{
Console.WriteLine("예외 발생 : {0}", e.Message);
}
finally
{
Console.WriteLine("여기는 finally 블록");
}
}
}
& 예외던지기
public static void Main()
{
try
{
rethrow();
}
catch(Exception e)
{
Console.WriteLine("111여기는 Main() 입니다.");
Console.WriteLine(e.Message);
Console.WriteLine("222예외가 발생한 곳은 {0} 입니다.", e.Source);
}
}
public static void rethrow()
{
int one = 0;
try
{
if(one == 0)
throw(new Exception("333one은 0이 아니라 1입니다.") );
}
catch(Exception e)
{
Console.WriteLine("444여기는 rethrow()입니다.");
Console.WriteLine(e.Message);
Console.WriteLine("555예외가 발생한 곳은 {0} 입니다.", e.Source);
throw; //예외 처리를 마친 후 다시 예외를 던집니다.
}
}
& p.379 여기서잠깐
msdn 참고
https://msdn.microsoft.com/ko-kr/library/ms173160.aspx
# 2교시 C++
& 객체지향 프로그래밍
& 클래스와 객체
& 멤버함수에 대한 포인터 -ppt오류잡기
& 멤버함수에 대한 포인터 타입을 정의하는 예
& 문제
수고하셨습니다.
갈수록어려워 지네요 공부를해야겠습니다. ^^
'코스웨어 > 15년 스마트컨트롤러' 카테고리의 다른 글
2015-06-11 32번 천정호 (5) | 2015.06.13 |
---|---|
20150612 (바뀐 번호)31번 - 홍준모 - c# : 델리게이트의 기본 개념, windowform 활용 c++ : (4) | 2015.06.12 |
음햐햐 스킨 바꿨음 (3) | 2015.06.12 |
Car 클래스 수업자료 (1) | 2015.06.11 |
2015.06.09(화) 주재민 (5) | 2015.06.09 |
20150609 - c++ 파일분활 (0) | 2015.06.09 |
20150609 - 27 주보건 콜렉션 - ArrayList - Queue - Indexer (4) | 2015.06.09 |
20150605 - 26번 임현수 배열, default 매개변수, 복사생성자 (5) | 2015.06.07 |