[예제1]
class
using System;
using static System.Console;
namespace ConsoleApp1
{
class Program
{
// 클래스
class Tiger
{
public string name;
public int age; // 필드
public void f1() // 메소드
{
WriteLine(name + " " + age);
}
}
static void Main(string[] args)
{
// 객체 생성시키는 논리
Tiger t1 = new Tiger();
Tiger t2 = new Tiger();
t1.f1();
t2.f1();
// int는 클래스로 봐줘야한다.
// 성립된다
int a = new int();
int b = 0;
}
}
}
[예제2]
소멸자에는 public을 사용하지 않는다
using System;
using static System.Console;
namespace ConsoleApp1
{
class Program
{
// 클래스
class Tiger
{
public Tiger()
{
WriteLine(1);
}
// 소멸자에는 public을 사용하지 않는다
~Tiger()
{
WriteLine(2);
}
}
static void Main(string[] args)
{
Tiger t1 = new Tiger();
Tiger t2 = new Tiger();
}
}
}
[예제3]
객체 카운팅 예제
using System;
using static System.Console;
namespace ConsoleApp1
{
class Global
{
public static int ct = 0;
}
class ClassA
{
public ClassA()
{
Global.ct++;
}
}
class ClassB
{
public ClassB()
{
Global.ct++;
}
}
class Program
{
static void Main(string[] args)
{
// 객체생성
new ClassA();
new ClassB();
new ClassB();
WriteLine(Global.ct);
}
}
}
[예제4]
얕은 복사
using System;
using static System.Console;
namespace ConsoleApp1
{
class Tiger
{
public int a, b;
}
class Program
{
static void Main(string[] args)
{
Tiger t1 = new Tiger();
t1.a = 10;
t1.b = 20;
// 값을 복사할려고 한것인데, 메모리 공유가 되어 버렸다.
// 이것은 얕은 복사가 일어 난것이다.(얕은복사 : 메모리 공유가 되어버린다)
Tiger t2 = t1;
// c++에서는 복사 생성자
t2.a = 30;
t2.b = 40;
// t1과 t2 메모리가 공유 되었다.
WriteLine("------t1------");
WriteLine(t1.a + " " + t1.b);
WriteLine("------t2------");
WriteLine(t2.a + " " + t2.b);
}
}
}
[추가개념]
using System;
using static System.Console;
namespace ConsoleApp1
{
class Tiger
{
public int a, b;
public Tiger f1()
{
Tiger t = new Tiger();
t.a = this.a;
t.b = this.b;
return t;
}
}
class Program
{
static void Main(string[] args)
{
Tiger t1 = new Tiger();
t1.a = 10;
t1.b = 20;
Tiger t2 = t1.f1();
t2.a = 30;
t2.b = 40;
// 메모리가 공유가 되었다
WriteLine(t1.a + " " + t1.b);
WriteLine(t2.a + " " + t2.b);
}
}
}
[예제5]
this 용법
1. 생성자 인수 전달
2. channing
3. 너에게로 나를 보낸다.
using System;
using static System.Console;
namespace ConsoleApp1
{
class Tiger
{
public int a, b;
public Tiger()
{
}
public Tiger(int a, int b)
{
//여기서 this는 t이다.
// 전달된 인수를 필드 변수에 등록하는것이 목적인데 변수명이 중복되었다.
this.a = a; // 객체.a = 인수a;
this.b = b; // 객체.b = 인수b;
}
public Tiger f1()
{
WriteLine("f1");
return this;
}
public void f2()
{
WriteLine("f2");
}
public void output()
{
WriteLine(this.a + " " + this.b);
WriteLine(a + " " + b);
}
}
class Program
{
static void Main(string[] args)
{
Tiger t = new Tiger();
t.output();
t.f1().f2();
}
}
}
[예제6]
this 용법
using System;
using static System.Console;
namespace ConsoleApp1
{
class ClassA
{
public void f1()
{
WriteLine(1);
ClassB t = new ClassB();
// this 자체가 a클래스다 여기서 this는 t다.
t.f2(this); // 너에게 나를 보내는 this
}
public void f3()
{
WriteLine(3);
}
}
class ClassB
{
public void f2(ClassA tt)
{
WriteLine(2);
tt.f3();
}
}
class Program
{
static void Main(string[] args)
{
ClassA t = new ClassA();
t.f1();
}
}
}
[예제7]
using System;
using static System.Console;
namespace ConsoleApp1
{
class Tiger
{
public int a, b;
public Tiger()
{
WriteLine(0);
}
public Tiger(int a):this() // this(): 생성자를 콜시킨다는 것이다.
{
// this() << 여기 있다고 보면 된다.
WriteLine(1);
this.a = a;
}
public Tiger(int a, int b):this(10)
{
// this(10) << 여기 있다고 보면 된다.
WriteLine(2);
this.a = a;
this.b = b;
}
public void output()
{
WriteLine(a + " " + b);
}
}
class Program
{
static void Main(string[] args)
{
Tiger t1 = new Tiger(10);
WriteLine("--------------------------------");
Tiger t2 = new Tiger(10, 20);
}
}
}
[추가내용]
using System;
using static System.Console;
namespace ConsoleApp1
{
class Tiger
{
public int a, b;
public Tiger()
{
WriteLine(0);
}
public Tiger(int a):this() // this(): 생성자를 콜시킨다는 것이다.
{
// this() << 여기 있다고 보면 된다.
WriteLine(1);
this.a = a;
}
public Tiger(int a, int b):this(10)
{
// this(10) << 여기 있다고 보면 된다.
WriteLine(2);
//this.a = a;
this.b = b;
}
public void output()
{
WriteLine(a + " " + b);
}
}
class Program
{
static void Main(string[] args)
{
Tiger t1 = new Tiger(10);
t1.output();
WriteLine("--------------------------------");
Tiger t2 = new Tiger(10, 20);
t2.output();
}
}
}
[접근 한정자]
private : 클래스 내부에서만 사용. 멤버끼리만 사용하겠다.\
public : 모든 곳에 사용할수 있게 한다.
protected : 상속에서 자식까지는 같이 사용한다.
[객체지향의 3대 특성]
상속, 당형성, data은닉
[예제8]
using System;
using static System.Console;
namespace ConsoleApp1
{
class Apple // 기반 클래스
{
public void f1()
{
WriteLine(1);
}
public void f3()
{
WriteLine("부 f3");
}
}
class Banana : Apple // 파생 클래스
{
public void f2()
{
WriteLine(2);
}
public new void f3() // 부모와 자식이 값이 똑같으면 앞에 new를 붙인다. (경고방지)
{
WriteLine("자 f3");
}
}
class Program
{
static void Main(string[] args)
{
Banana b = new Banana();
b.f1();
b.f2();
b.f3();
}
}
}
[추가개념]
using System;
using static System.Console;
namespace ConsoleApp1
{
class Apple // 기반 클래스
{
public void f1()
{
WriteLine(1);
}
public void f3()
{
WriteLine("부 f3");
}
}
class Banana : Apple // 파생 클래스
{
public void f2()
{
WriteLine(2);
}
public new void f3() // 부모와 자식이 값이 똑같으면 앞에 new를 붙인다. (경고방지)
{
WriteLine("자 f3");
}
public void f4()
{
f3();
base.f3(); // base:
}
}
class Program
{
static void Main(string[] args)
{
Banana b = new Banana();
b.f1();
b.f2();
b.f3();
WriteLine("----------f4-----------");
b.f4();
}
}
}
[예제9]
base를 이용해서 부모생성자를 선택할 수 있다.
using System;
using static System.Console;
namespace ConsoleApp1
{
class Apple // 기반 클래스
{
public Apple()
{
WriteLine(1);
}
public Apple(int a)
{
WriteLine(2);
}
}
class Banana : Apple // 파생 클래스
{
public Banana()
{
WriteLine(3);
}
// 디폴트 //public Banana(int a):base()
public Banana(int a):base(10) //base(a)
{
WriteLine(4);
}
}
class Program
{
static void Main(string[] args)
{
Banana b = new Banana(100); // 생성(부자) 소멸(자부)
}
}
}
[예제10]
* 캐스팅(형변환(명시적)) : 캐스트 연산자를 사용하여 변환 구문을 나타내고 숫자 타입간의 변환, 클래스 타입간의 변환이 가능하다.
- 숫자 타입의 변환
데이터 손실 가능성이 있는 경우의 변환
- 클래스 타입간의 변환
기본 클래스에서 파생클래스로의 변환
본래 자신의 타입에서 기본클래스로 변환이 된 클래스가 다시 파생(자신)클래스로 변환할 때 만 가능하다.
* as 연산자 : 객체를 캐스팅 할 때 사용되는 연산자.
- 캐스팅에 성공하면 캐스트 결과를 리턴하고 캐스팅에 실패하면 null값을 return 한다.
- as연산자를 사용하여 캐스팅하면 프로그램 실행을 거치지 않고도 캐스팅의 성공유무를 확인할 수 있다.(value type 불가)
* is 연산자 : 캐스팅 성공유무를 확인할 수 있는 연산자.
- as 연산자는 캐스팅 결과를 return 하지만, is 연산자는 캐스팅이 가능하면 true, 불가능하면 false를 return 한다.(캐스팅 성공 유무 판단)
using System;
using static System.Console;
namespace ConsoleApp1
{
class A
{
public void f1()
{
WriteLine(1);
}
}
class B : A // 파생 클래스
{
public void f2()
{
WriteLine(2);
}
}
class C : A // 파생 클래스
{
public void f3()
{
WriteLine(3);
}
}
class Program
{
static void Main(string[] args)
{
A a = new B(); // 부자 >> UpCasting
// 확인용도
// a.f2(); // 사용할 수 없다.
((B)a).f2(); // 바로 적용해서 사용할 수 있다.
if (a is B) // is는 true / false 가 리턴이 된다.
{
//ex1)
((B)a).f2();
//ex2)
B b = (B)a; // 자 = 부 (DownCasting)
b.f2();
WriteLine(4);
}
A a1 = new C(); // 부자 >> UpCasting
// B b = (B)a;
WriteLine("---------잘 사용하고 있는 경우-----------");
// 잘 사용하고 있는 경우
C c = a1 as C; // as는 null 발생
if (c != null)
{
c.f3();
WriteLine("not null");
}
WriteLine("---------잘못 사용하고 있는 경우----------");
// 잘못 사용하고 있는 경우
B d = a1 as B;// as는 null 발생
if (d == null)
{
WriteLine("null");
}
// 예외 상황이 발생한다.
/*try
{
B e = (B)a1;
}
catch(Exception e){ }*/
}
}
}
[예제11]
virtual, override, new
using System;
using static System.Console;
namespace ConsoleApp1
{
class Apple
{
public void f1()
{
WriteLine(1);
}
// virtual 가상함수
public virtual void f2()
{
WriteLine(2);
}
}
class Banana:Apple
{
public new void f1() // 새로운 함수를 나타나기 위해 new 사용
{
WriteLine(3);
}
// virtual과 override는 짝이다.
// f2함수가 없거나 new로 되어 있으면 안쳐다 본다.
public override void f2()
{
WriteLine(4);
}
}
class Program
{
static void Main(string[] args)
{
Banana t1 = new Banana();
t1.f1();
t1.f2();
WriteLine("------업캐스팅이 일어난 경우------");
Apple t2 = new Apple();
t2.f1();
t2.f2();
}
}
}
[Override]
using System;
using static System.Console;
namespace ConsoleApp1
{
class A
{
public virtual void f1()
{ WriteLine(1); }
}
class B:A
{
//public sealed override void f1() //sealed를 입력하면 컴파일 자체가 안된다(override 금지가 된다)
public override void f1()
{ WriteLine(2); }
}
class C:B
{
public override void f1()
{ WriteLine(3); }
}
class Program
{
static void Main(string[] args)
{
A a = new C();
a.f1();
}
}
}
'Education > Edu | .net' 카테고리의 다른 글
# 26.2) [C#] 문법7 (interface) (0) | 2021.02.19 |
---|---|
# 26.1) [C#] 문법6 (0) | 2021.02.19 |
# 25.1) [C#] 문법4 (0) | 2021.02.18 |
# 24.2) [C#] 문법3 (0) | 2021.02.17 |
# 24.1) [C#] 문법2 (0) | 2021.02.17 |