본문 바로가기
공부,일/C#

210604

by fromnothing1 2021. 6. 4.

menu

 

도구 상자의 MenuStrip 를 이용해서 메뉴를추가 할 수 있다 .

또한 속성을 통해서 각 메뉴 별로 이벤트를 할당 할수도 있다.

 

열기 누르면 messagebox 호출

그외 다양하 기능이 많지만 알아서 해라 

 

인터페이스

 

인터페이스 예제 1

-IComparable

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class Dummy :IDisposable
{

    public void Dispose()
    {
        Console.WriteLine("Dispose()를 호출합니다.");
    }
}

class Program
{
    static void Main(string[] args)
    {
        // 괄호 안의 class 가 IDisposable 상속받은 경우 블록을 벗어날때 클래스내의 Dispose()메서드 호출
        using (Dummy dummy = new Dummy()) 
        {
            Console.WriteLine("1");

        }// 
        Console.WriteLine("2");

    }
    static void Main1(string[] args)
    {
        
        Dummy aDummy = new Dummy();
        aDummy.Dispose();

    }
}

인터페이스를 이용해서 정렬 기준 추가하기 

 

인터페이스 예제 2

IDisposable 사용

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class Dummy :IDisposable
{

    public void Dispose()
    {
        Console.WriteLine("Dispose()를 호출합니다.");
    }
}

class Program
{
    static void Main(string[] args)
    {
        // 괄호 안의 class 가 IDisposable 상속받은 경우 블록을 벗어날때 클래스내의 Dispose()메서드 호출
        using (Dummy dummy = new Dummy()) 
        {
            Console.WriteLine("1");

        }// 
        Console.WriteLine("2");

    }
    static void Main1(string[] args)
    {
        
        Dummy aDummy = new Dummy();
        aDummy.Dispose();

    }
}

 

인터페이스 생성

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

interface IBasic
{
    // 인터페이스 특징
    //int iNum;  인스턴스 변수 선언 불가능 
    int iNum { get; set; }  // 속성은 허용
    //int iNum2 {set { } get { }}  속성 세부 설정은 불가능
    void test(); // 추상 함수 선언 허용 abstract 를 명시 하지 않는다. `

    //void test() { }; 불가능 

}
abstract class  IBasic1
{
    // 추상 클래스 
    int iNum1;
    int iNum2 { get; set; }
    int iNum3 { set { } get { return 100; } }
    void test1() { }
    public abstract void test();
   

}
interface IBasic3
{
    int TestInstanceMethod();
    int TestProperty { get; set; }

}

// 인터페이스 상속 받기 위해서는 인터페이스 안의 멤버들을 구현해야 한다.
class Testclass : IBasic3
{
    public int TestProperty
    {
        get
        {
            return 100;
        }
        set
        {
        }
    }

    public int TestInstanceMethod()
    {
        return 100;
    }
}

class Program
{
    static void Main(string[] args)
    {
        Testclass aTestclass = new Testclass();

    }
}

 

다중 상속 

c# 은 1개의 클래스만 상속 받을 수 있지만 

인터페이스는 여러개 상속 받을 수 있다.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class Parent { }

// 다중 상속 

// 클래스는 1개만 상속 가능
// 인터페이스는 여러개 상속 받을 수 있다.
class Child : Parent, IDisposable, IComparable
{ 
    public void Dispose()
    {
        throw new NotImplementedException();
    }

    public int CompareTo(object obj)
    {
        throw new NotImplementedException();
    }
}
class Program
{
    static void Main(string[] args)
    {
    }
}

 

 

인터페이스 객체참조 변수 사용

using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
interface Phone
{
    void call();

    void Recv();

}


class Sami : Phone
{
    public void call()
    {
        Console.WriteLine("삼성삼성삼성");

    }

    public void Recv()
    {
        Console.WriteLine("ㄹ삼성삼성삼성");
    }
}


class Helgi : Phone
{

    public void call()
    {
        Console.WriteLine("헬지헬지헬지");

    }
    public void Recv()
    {
        Console.WriteLine("ㄹ헬지헬지헬지");
    }

}

class Hong : Phone
{

    public void call()
    {
        throw new NotImplementedException(); // 예외 처리 에러 발생

    }

    public void Recv()
    {
        throw new NotImplementedException();
    }

}



class Program
{
    static void Main(string[] args)
    {


        Sami aSmai = new Sami();
        Helgi aHelgi = new Helgi();
        Hong aHong = new Hong();

        //인터페이스의 객체참조 변수 또한 하위 클래스의 객체를 받을 수 있다.
        Phone[] aPhone = new Phone[] { aSmai,aHelgi,aHong}; 

        foreach (var item in aPhone)
        {
            item.call();
        }

        foreach (var item in aPhone)
        {
            item.Recv();
        }

    }
}

'공부,일 > C#' 카테고리의 다른 글

210607  (0) 2021.06.07
window forms  (0) 2021.06.04
210603_2  (0) 2021.06.03
List class  (0) 2021.06.03
210602  (0) 2021.06.02

댓글