프로그램/C# - Study / / 2010. 9. 16. 12:53

9D - Ex16OOP7 - Interface

반응형


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

namespace Ex16OOP7
{
    class Program
    {
        static void Main(string[] args)
        {
            //Thebase b = new Thebase();

            Thebase2 b = new TheDerived();
            //추상클래스 참조 = 비추상클래스 객체

            //인터페이스는 객체 생성 불가능
            //IMyInterface obj = new IMyInterface();

            IMyInterface obj = new MyImplementayion();
            IMyInterface2 obj2 = new MyImplementayion();
            obj.Method();
            obj2.Method2();
            obj.MyMethod();
            obj2.MyMethod();
            obj.CommonMethod();

        }
    }
}
/********************************************************************
 * Interface  목차 개념
 * ******************************************************************/


******************************************************* 추가 ************************************************************

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

namespace Ex16OOP7
{
    //abstract class는 객체를 생성할 수 없는 클래스
    //참조변수를 만드는 것은 가능

    abstract class Thebase
    {
        public void MethodA()
        {

        }
    }

    abstract class Thebase2
    {
        //abstract Method (추상메서드)는 정의를 포함하지 않는 메서드
        //추상메서드를 포함하는 클래스는 추상클래스가 되어야 합니다.

        public abstract void MethodA(); // { }
    }

    //추상클래스를 상속 하는 클래스는 반드시
    //상속 받은 추상클래스의 모든 충상메서드를 재정의 해야 합니다.
    //아니면 클래스를 추상클래스로 선언해야 합니다.
    class TheDerived : Thebase2
    {
        public override void MethodA()
        {

        }
    }
}

******************************************************* 추가 ************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ex16OOP7
{
    //인터페이스는 다중 상속이 가능! 클래스는 불가능!
    interface ImyCommon
    {
        void CommonMethod();
    }
    // 인터페이스 간에는 상속이 가능
    interface IMyInterface : ImyCommon
    {
        // int x; // 오류 : 인터페이스는 필드를 포함할 수 없습니다.
        // void Method(); { } // 오류 : 메서드는 정의를 포함할 수 없습니다.
        // public void MethodB(); // 오류 : 접근지정자 사용 X

        void Method();
        void MyMethod();

    }
    interface IMyInterface2
    {
        void Method2();
        void MyMethod();
    }

   

    //인터페이스는 클래스에 의해 구협됩니다.
    //인터페이스를 구현하는 클래스는 인터페이스의 모든 메서드를 재정의 해야합니다.

    class MyImplementayion : Object, IMyInterface, IMyInterface2
    {
        public void Method()
        {
            Console.WriteLine("MyInferface.Method 구현");
        }

        public void Method2()
        {
            Console.WriteLine("MyInferface2.Method 구현");
        }

        // 명시적 인터페이스 구현 (앞에 public 붙이지 않는다)
        // 명시적 인터페이스 구현에서는 접근지정자 사용 불가능!

        void IMyInterface.MyMethod()
        {
            Console.WriteLine("MyInferface.MyMethod 구현");
        }

        void IMyInterface2.MyMethod()
        {
            Console.WriteLine("MyInferface2.MyMethod 구현");
        }
        public void CommonMethod()
        {
            Console.WriteLine("MyInferface.CommonMethod 구현");
        }

    }
}
******************************************************* 추가 ************************************************************

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

namespace Ex16OOP7
{
    class BankAccount
    {
        private static int nextAccountNo = 1;//자동 증가를 위해 static 추가

        private int accountNo;
        public int AccountNo
        {
            get { return accountNo; }
        }
        private string owner;
        public string Owner
        {
            get { return owner; }
            set { owner = value; }
        }
        private double balance;
        public double Balance
        {
            get { return balance; }
            set { balance = value; }
        }

        public BankAccount()
        {
            accountNo = nextAccountNo;
            nextAccountNo++;
        }
        public BankAccount(string owner, double balance)
            : this()
        {
            this.owner = owner;
            this.balance = balance;
        }
       
        public virtual bool Withdraw(double amount)
        {
            if (amount <= 0 || Balance - amount < 0)
                return false;
            else
                Balance -= amount;

            return true;
        }
        public virtual bool Deposit(double amount)
        {
            if (amount <= 0)
                return false;
            else
                Balance += amount;

            return true;
        }

        public override string ToString()
        {
            return
                string.Format(
                "{0}. [{1}] [{2}]",
                accountNo, owner, balance);
        }
    }
}

******************************************************* 추가 ************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ex16OOP7
{
    class MinusAccount : BankAccount
    {
        private double limit;
        public double Limit
        {
            get { return limit; }
            set { limit = value; }
        }

        public MinusAccount() { }
        public MinusAccount(
            string owner, double balance, double limit)
            : base(owner, balance)
        {
            this.limit = limit;
        }       

        public override bool Withdraw(double amount)
        {
            if (amount <= 0 || Balance - amount < limit)
                return false;
            else
                Balance -= amount;  //balance = balance - amount;

            return true;
        }

        public override string ToString()
        {
            string info = base.ToString();
            return string.Format("{0} [{1}]", info, limit);
        }
    }
}

******************************************************* 추가 ************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ex16OOP7
{
    class SavingAccount : BankAccount
    {
        private int point;
        public int Point
        {
            get { return point; }
            set { point = value; }
        }
        private bool isVIP;
        public bool IsVIP
        {
            get { return isVIP; }
            set { isVIP = value; }
        }

        public SavingAccount() { }
        public SavingAccount(
            string owner, double balance, bool isVIP)
            : base(owner, balance)
        {
            point = 0;
            this.isVIP = isVIP;
        }

        public override bool Deposit(double amount)
        {
            if (amount <= 0)
                return false;
            else
            {
                Balance += amount;

                if (isVIP)
                    point += (int)(amount * 0.01);

                return true;
            }
        }

        public override string ToString()
        {
            string info = base.ToString();
            return
                string.Format(
                "{0} [{1}] [{2}]",
                info, point,
                isVIP ? "우대고객" : "일반고객");
        }
    }
}

******************************************************* 추가 ************************************************************

반응형

'프로그램 > C# - Study' 카테고리의 다른 글

10D - Ex18WindowsForms2  (0) 2010.09.17
9D - 윈도우폼 만들기  (0) 2010.09.17
수업자료  (0) 2010.09.16
7D, 8D -  (0) 2010.09.16
7D - 다형성, 상속  (0) 2010.09.14
  • 네이버 블로그 공유
  • 네이버 밴드 공유
  • 페이스북 공유
  • 카카오스토리 공유