2014년 7월 15일 화요일

[C#객체지향예제구현,C#,Polygon]상속,추상클래스,인터페이스를 이용한 다형성 구현예제

[C#객체지향예제구현,C#,Polygon]상속,추상클래스,인터페이스를 이용한 다형성 구현예제

Polygon이란?
-A regular polygon is one in which all the sides have the same length and all of the interior angles are equal. For example, a square is a regular polygon.
- Polygon class에서 제공되어야 하는 method.
area(), which returns the area of the polygon;
perimeter(), which returns the distance around the polygon;
- Polygon class를 설계하라. 추상화 시킬 필요가 있는 함수는 추상화 하고 하위 클래스에서 구현하도록 하자.
- Polygon class를 상속/구현해서 triangle 과square클래스를 설계하고 main함수를 통해 객체를 생성하고 Test해 보라. (추상클래스, 인터페이스, 상속을 이용하여 다형성을 구현하시오)


1. 상속을 이용한 다형성


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

namespace ConsoleApplication12
{
    public class Polygon 
    {
        public virtual int area(int a) { return 0;  }
        public virtual int perimeter(int a) { return 0; }
    } 

    public class Square : Polygon 
    { 
        public override  int area(int a) 
        { 
            return a * a; 
        } 

        public override int  perimeter(int a) 
        { 
            return 4 * a; 
        } 
    }

    public class triangle : Polygon
    {
        public override int area(int a)
        {
            return a * a / 2;
        }

        public override int perimeter(int a)
        {
            return 3 * a;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            string readVal = Console.ReadLine();

            int val = Convert.ToInt32(readVal);

            Polygon sq = new Square();
            Polygon tri = new triangle();

            Console.WriteLine("area:{0} , perimeter:{1}", sq.area(val), sq.perimeter(val));
            Console.WriteLine("area:{0} , perimeter:{1}", tri.area(val), tri.perimeter(val));
        }
    } 


}



2. 추상클래스를 이용한 다형성

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

namespace ConsoleApplication12
{
    public abstract class Polygon 
    { 
        public abstract int area(int a);
        public abstract int perimeter(int a); 
    } 

    public class Square : Polygon 
    { 
        public override  int area(int a) 
        { 
            return a * a; 
        } 

        public override int  perimeter(int a) 
        { 
            return 4 * a; 
        } 
    }

    public class triangle : Polygon
    {
        public override int area(int a)
        {
            return a * a / 2;
        }

        public override int perimeter(int a)
        {
            return 3 * a;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            string readVal = Console.ReadLine();

            int val = Convert.ToInt32(readVal);

            Polygon sq = new Square();
            Polygon tri = new triangle();

            Console.WriteLine("area:{0} , perimeter:{1}", sq.area(val), sq.perimeter(val));
            Console.WriteLine("area:{0} , perimeter:{1}", tri.area(val), tri.perimeter(val));
        }
    } 


}



3. 인터페이스를 이용한 다형성

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

namespace ConsoleApplication12
{
    interface Polygon 
    { 
        int area(int a); 
        int perimeter(int a); 
    } 

    public class Square : Polygon 
    { 
        public int area(int a) 
        { 
            return a * a; 
        } 

        public int perimeter(int a) 
        { 
            return 4 * a; 
        } 
    }

    public class triangle : Polygon
    {
        public int area(int a)
        {
            return a * a / 2;
        }

        public int perimeter(int a)
        {
            return 3 * a;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            string readVal = Console.ReadLine();

            int val = Convert.ToInt32(readVal);

            Polygon sq = new Square();
            Polygon tri = new triangle();

            Console.WriteLine("area:{0} , perimeter:{1}", sq.area(val), sq.perimeter(val));
            Console.WriteLine("area:{0} , perimeter:{1}", tri.area(val), tri.perimeter(val));
        }
    } 


}


[결과]
3
area:9 , perimeter:12
area:4 , perimeter:9
계속하려면 아무 키나 누르십시오 . . .

오라클자바커뮤니티교육센터, 개발자전문교육, 개인80%환급
www.oraclejavacommunity.com


평일주간(9:30~18:10) 개강
(7/21)[기업100%환급]자바기초에서 JDBC, Servlet/JSP까지
(7/21)[기업100%환급]Spring ,MyBatis,Hibernate실무과정
(7/21)[기업100%환급]PL/SQL,ORACLE HINT,TUNING
(7/21)[채용예정교육]오라클자바개발잘하는신입뽑기프로젝트,교육전취업확정
(7/28)[기업100%환급]C#4.0,WinForm,ADO.NET프로그래밍
(7/28)[기업100%환급]안드로이드개발자과정
(7/28)[기업100%환급]SQL기초에서 Schema Object까지

평일야간(19:00~21:50) 개강
(7/16)Spring3.X, MyBatis, Hibernate실무과정
(7/21)웹퍼블리싱 마스터
(7/22)자바기초에서JSP,Ajax,jQuery,Spring3.2,MyBatis까지
(7/22)닷넷(C#,Network,ADO.NET,ASP.NET)마스터과정
(7/24)SQL기초에서실무까지
(7/29)안드로이드개발자과정
(8/05)MyBatis3.X, Hibernate4.X ORM실무과정

주말(10:00~17:50) 개강
(7/19)JSP,Ajax,jQUERY,Spring,MyBatis,Hibernate속성과정
(7/19)SQL초보에서 Schema Object까지
(7/19)C#,ASP.NET마스터
(7/19)Spring3.X, MyBatis, Hibernate실무과정
(7/19)웹퍼블리싱 마스터
(7/19)안드로이드개발자과정
(7/26)개발자를위한PLSQL,SQL튜닝,힌트
(8/02)MyBatis3.X, Hibernate4.X ORM실무과정
(8/09)자바기초에서JSP,Ajax,jQuery,Spring3.2,MyBatis까지
(8/23)JAVA,Network&WEB&Framework(자바기초에서웹스프링까지)

주말저녁(18:30~22:20) 개강
(8/02)JAVA,Network&WEB&Framework
(8/09)SQL기초에서실무까지

댓글 없음:

댓글 쓰기