static
메모리에 가장 먼저 잡힘
1. no data, static method만 있는 경우
- 전역함수 대용으로
static Console
{
public static void WriteLine()
{
}
}
객체가 아무리 많이 생겨도 메모리를 공유한다.
2. static data <---- 공유하고 싶은 데이터 : 전역 변수 대용
private static double interest; // 이자율 <=== 항상 똑같은 값을 가진다. 공유하고 싶다.
// static data Area에 할당 됨
static으로 선언한 멤버는 객체로 접근하지 못한다. Class.StaticMember 로 접근
static Method 는 static data만 사용 가능. this*키워드 사용 불가
*this는 이 Method를 호출한 객체
class
- field(data) : private
- method : public
- constructor
- overloading or optional parameter
- prorperty
- static data
- static method
- this
- event
- operate overloading
int[] arr = {1,2,3};
int[] copy = arr; // 얕은 복사
int[] d = (int[]) arr.Clone(); // 깊은 복사
얕은 복사: reference만 복사 (주소 정보만 복사)
Interface
객체 생성 불가, 데이터 선언 불가
Method 선언만
1. 표준화
2. 다중 상속
3. 보안 - 분산 환경 프로그래밍
암시적 구현 implicit 과 명시적 구현이 있는데
대부분 암시적으로 구현함
명시적 구현의 경우
- 서로 다른 인터페이스에서 같은 이름의 메서드를 선언한 경우
IComparable
public interface IComparable
{
int CompareTo(Object obj);
}
class Employee : IComparable
{
public int ID
{
get; set;
}
public string Name
{
get; set;
}
public int CompareTo(object obj)
{
Employee e = (Employee)obj;
if (this.ID > e.ID)
return 1;
else if (this.ID == e.ID)
return 0;
else
return -1;
}
public override string ToString()
{
return ID + "\t" + Name;
}
}
class Program
{
static void Main(string[] args)
{
// 객체 초기화
Employee e1 = new Employee
{
ID = 1,
Name = "홍길동"
}; // 생성자 구현 없이, 속성이 있어야 함
int[] a = { 4, 8, 3, 2, 1 }; // System.Array
Array.Sort(a); // Quick Sort
//foreach (int i in a)
// Console.WriteLine(i);
Employee[] em = new Employee[3]
{
new Employee { ID = 1, Name = "홍길동" },
new Employee { ID = 3, Name = "이순신" },
new Employee { ID = 2, Name = "김연아" }
};
Employee[] em1 = new Employee[3]
{
new Employee { ID = 1, Name = "홍길동" },
new Employee { ID = 3, Name = "이순신" },
new Employee { ID = 2, Name = "김연아" }
};
Array.Sort(em);
foreach (Employee e in em)
Console.WriteLine(e);
}
}
Generic
1. Generic Method
매개변수로 Generic을 사용 할 때는 타입을 명시하지 않아도 됨
2. Generic Class
클래스이름<타입> 명시
class Stack <T>
{
private T[] data;
private int top;
public Stack(int size = 10)
{
data = new T[size];
top = -1;
}
public void Push(T n)
{
if (top >= data.Length - 1)
throw new ArgumentOutOfRangeException("Stack Overflow");
data[++top] = n;
}
public T Pop()
{
if(top < 0 )
throw new ArgumentOutOfRangeException("Stack Underflow");
return data[top--];
}
}
Stack<int> s1 = new Stack<int>(3);
s1.Push(10);
s1.Push(20);
s1.Push(30);
Console.WriteLine(s1.Pop());
Console.WriteLine(s1.Pop());
Console.WriteLine
Stack<double> s2 = new Stack<double>(3);
s2.Push(10.1);
s2.Push(20.2);
s2.Push(30.3);
Console.WriteLine(s2.Pop());
Console.WriteLine(s2.Pop());
Console.WriteLine(s2.Pop());
Generic 타입 제한
where 뒤에 명시한 타입 내에서 사용 가능
public class CustomList<T> where T : IBeverage
{
}
IEnumerable <------ foreach, indexer
Indexer
foreach를 쓰려면 아래 인터페이스를 구현해줘야해서 번거롭다.
class CustomCollection<T> : IEnumerable<T>
{
public IEnumerator<T> Backwards()
{
// This method returns an alternative enumerator.
// The implementation details are not shown.
}
#region IEnumerable<T> Members
public IEnumerator<T> GetEnumerator()
{
// This method returns the default enumerator.
// The implementation details are not shown.
}
#endregion
#region IEnumerable Members
IEnumerator IEnumerable.GetEnumerator()
{
// This method is required because IEnumerable<T> inherits from IEnumerable
throw new NotImplementedException();
}
#endregion
}
실습교재 40p
Macth matchNames =
Regex.Mactch(SessionContext.CurrentStudent,@"([^ ]+) ([^ ]+)");
section 4 at paragraph 187
//string path = "c:\\temp\\test.txt";
string path = @"c:\temp\test.txt";
Console.WriteLine(path);
Escape Sequence
\n
\t
\\
Regular Expression 정규표현식=정규식
System.Text.RegularExpression
Regex <--- Perl 5.0
교재
section 4 at paragraph 187
\d 숫자 [0-9]
\w 문자나 숫자 [0-9A-Za-z]
\D 숫자가 아닌것 ^\d
\W 문자나 숫자가 아닌것 ^\w
\. .
\s 공백
\S 공백이 아닌것 ^\s
+ 1번이상 to+ to too tooo
* 0번이상 to* t to too
? 0번 or 1번 to? t to
^정규식시작
정규식끝$
"hello"
^정규식$
{n} 자리수
....
우편번호 형식 98004
\d{5} [0-9]{5}
휴대폰 번호 형식
010-2222-3333
019-222-3333
01\d{1}-\d{3,4}-d{4}
^01[0|1|6|7|8|9]-\d{3,4}-d{4}$
이메일형식
hjyi@hotmail.com
(\w)+@(\w)+\.(\w)+
hj-yi
hj'yi
^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$
Regular Expression Test
Module 5 상속
Base Class 기본 클래스
↑
Derived Class 파생클래스
class Base
{// privare, pulbic, internal, protected
}
class Derived : Base
{
}
1. 무엇을 상속 받는가? all(private + pulbic +data+method)
private은 접근이 안되지만 상속은 받음
2. protected
3. 생성자가 호출되는 순서
Derived d= new Derived(); // 1. Base() 생성자 - 2. Derived() 생성자
4. 재정의 (Overriding)
- polymophism(다형성)고려
5. abstract
sealed 더 이상 상속하지 않겠다.
기본 클래스 생성자 호출
public class Beverage
{
public Beverage()
{
// Implementation details not shown.
}
public Beverage(string name, bool isFairTrade, int servingTemp)
{
// Implementation details are not shown.
}
// Other class members are not shown.
}
public class Coffee : Beverage
{
public Coffee()
{
// This constructor implicitly calls the default Beverage constructor.
// The declaration is implicitly equivalent to public Coffee() : base()
// You can include additional code here.
}
public Coffee(string name, bool isFairTrade, int servingTemp, string bean, string
roast)
: base(name, isFairTrade, servingTemp)
{
// This calls the Beverage(string, bool, int) constructor.
// You can include additional code here:
Bean = bean;
Roast = roast;
}
public string Bean { get; set; }
public string Roast { get; set; }
public string CountryOfOrigin { get; set; }
}
생성자 호출 순서
base클래스의 생성자를 먼저 호출하기 때문에 base 매개변수 있는 생성자를 명시한다
메서드 재정의
public class Beverage
{
protected int servingTemperature;
public virtual int GetServingTemperature()
{
return servingTemperature;
}
// Other class members not shown.
}
public class Coffee : Beverage
{
protected bool includesMilk;
private int servingTempWithMilk;
private int servingTempWithoutMilk;
public override int GetServingTemperature()
{
if(includesMilk) return servingTempWithMilk
else return servingTempWithoutMilk;
}
}
upcasting 상위클래스로 암시적 변환 가능 반대는 안됨
Dynamoc Binding(Runtime Binding) virtual - override
추상 클래스 - 추상 메서드가 하나라도 있는 클래스
객체를 생성 못 함
확장 메서드
partial