목록전체 글 (240)
S_pot
using System; namespace _210525_011 { class Car { } class HyunDai : Car { } class Sonata : HyunDai { } class KIA : Car { } class K5 : KIA { } class Program { static void Main(string[] args) { Car aCar = new Sonata(); bool aBool; aBool = aCar is Sonata; // is연산자: aCar가 Sonata를 가지고 있는가 Console.WriteLine(aBool); // True를 반환 Console.WriteLine(aCar is Sonata); // True를 반환 Console.WriteLine(aCar is K5..
override: 상속관계의 클래스 사이에서 부모클래스의 메소드를 자식클래스가 부모의 메소드를 받아서 다른 형태로 바꾸어 사용 using System; namespace _210525_010 { class Instrument { public virtual void Play() // virtual: 부모클래스에서 자식클래스의 함수를 사용할 수 있게한다. { Console.WriteLine("연주하다."); } } class Piano : Instrument { public override void Play() { Console.WriteLine("따라란딴딴"); } } class Guitar : Instrument { public override void Play() { Console.WriteLine("..
using System; // 박싱, 언박싱 namespace _210525_008 { class MainApp { static void Main(string[] args) { int a = 123; object b = (object)a; // a에 담긴 값을 박싱해서 힙에 저장 int c = (int)b; // b에 담긴 값을 언박싱해서 스택에 저장 Console.WriteLine(a); Console.WriteLine(b); Console.WriteLine(c); double x = 3.1414213; object y = x; // x에 담긴 값을 박싱해서 힙에 저장 double z = (double)y; // y에 담긴 값을 언박싱해서 스택에 저장 Console.WriteLine(x); Consol..
using System; namespace _210525_004 { class A { public int ANum; public A(int Num) { ANum = Num; } public A(int Num1, int Num2) { ANum = Num1 + Num2; } } class B : A { int ANum; public B() : base(100,200) // base(): 부모의 생성자를 호출 { int ANum = 100; // 메소드의 지역변수 this.ANum = 100; // B클래스(자식)의 인스턴스 변수 base.ANum = 300; // A클래스(부모)의 인스턴스 변수 Console.WriteLine("B 디폴트 생성자"); } } class Program { static void..
using System; // 상속으로 코드 활용 // class Parents { public int pNum; public Parents() { Console.WriteLine("Parents 디폴트 생성자 호출"); } ~Parents() { Console.WriteLine("Parents 소멸자 호출"); } } // Parent, Base(기반), super // Child, Derived(파생), sub class Child : Parents // Child는 Parents를 상속 받았다. { public int cNum; public Child() { Console.WriteLine("Child 디폴트 생성자 호출"); } ~Child() // Parents class보다 Child clas..

// this()생성자 사용전 public Smart(string Name) { this.Name = Name; this.Address = "미기입"; } public Smart(string Name, string Address) { this.Name = Name; this.Address = Address; } public Smart() { this.Name = "무명"; this.Address = "미기입"; } } class Program { static void Main(string[] args) { Smart aSmart = new Smart("홍길동", "부산"); } } // this() 생성자 적용 using System; // this() 생성자: 생성자에서 다른 생성자를 호출 // 메서드..

using System; // 얕은복사, 깊은복사 예제 namespace _210525_001 { class MyClass { public int MyField1; // 인스턴스 변수 public int MyField2; // 인스턴스 변수 public MyClass DeepCopy() // 반환값: MyClass 래퍼런스 { MyClass newCopy = new MyClass(); // 새로운 MyClass를 생성 newCopy.MyField1 = this.MyField1; // this는 source.MyField1 newCopy.MyField2 = this.MyField2; // this는 source.MyField2 return newCopy; } ~MyClass() { Console.Write..

using System; namespace _210524_002 { class Global { // static 변수/ 정적 변수 public static int Count = 0; } class ClassA { public ClassA() { Global.Count++; } } class ClassB { public ClassB() { Global.Count++; } } class MainApp { static int INum; // 정적필드 int TestNum; // 동적필드 static void StaticMethod() { } void DynamicMethod() { } static void Main() // 정적 메소드 // 정적 메소드는 // 정적 필드 혹은 // 정적 메소드에만 접근가능 {..

using System; namespace _210524_001 { class Program { static void Main1(string[] args) { Cat Test1 = new Cat("1"); // 생성은 1234순이지만 소멸은 4321이다. Cat Test2 = new Cat("2"); // 이와같은 이유는 stack형이기 때문에 위쪽에 쌓여있는 것 먼저 소멸된다. Cat Test3 = new Cat("3"); Cat Test4 = new Cat("4"); } static void Main(string[] args) { Cat aCat; // 객체참조 변수생성: 참조대상은 Cat Type // 객체생성방법: new 사용 + 생성자호출 aCat = new Cat(); // Cat의 기본 생성..

class Smart { } class Cat { public string Name; public string Color; //생성자 실습: Cat class public Cat() // Cat class의 생성자 { Console.WriteLine("Cat class 생성자 구동"); } static void Main(string[] args) { Cat Test; // Cat 객체를 참조하는 참조변수 선언 Test = new Cat(); // Cat 클래스의 생성자를 이용한 // Cat 객체생성 Smart Temp; // Smart 클래스의 생성자를 만들지 않아도 오류가 나지 않는 이유는 Temp = new Smart(); // C# 컴파일러가 자동으로 만들어주었기 때문이다. } 출력값