[C#] Why and when should we use an abstract class && 인스터스 생성과 동시에 프로퍼티값 넣기 > C#/.Net/Blazor/IIS

본문 바로가기
사이트 내 전체검색

C#/.Net/Blazor/IIS

[C#] Why and when should we use an abstract class && 인스터스 생성과 동시에 프로퍼티…

페이지 정보

작성자 sbLAB 댓글 0건 조회 4,427회 작성일 18-07-25 17:33

본문

Why and when should we use an abstract class ? 


1 . abstract 클래스나 인터페이스는 인스턴스 생성이 불가능 하다.

2.  abstract 클래스 안에 선언된 깡통 abstract 메서드는 자식클래스에서 필히 override 로 구현되어야 한다. 


[결론]  

서로 중복되거나 연관된 클래스들에서 공통변수, 공통항목을 abstract  베이스클래스로 뽑아내서 슬림화된 각 자식클래스들에서 각 작업을 하게 한다. 또한  abstract  로 선언된 베이스 클래스는 인스턴스 생성이 불가능 하게 된다.


※ 아래와 같이 베이스(부모) 클래스형 변수를 참조하게 하여, 다형성을 갖을 수 있다.



 //BaseEmployee <- 베이스클래스 참조
     BaseEmployee bte = fte;
     Console.WriteLine(bte.ID+"-"+bte.GetMonthSalary());
     bte = cte;
     Console.WriteLine(bte.ID+"-"+bte.GetMonthSalary());


[Source]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace why_abstract
{
    class Program
    {
        static void Main(string[] args)
        {
 
//Error	1	Cannot create an instance of the abstract class or interface
//BaseEmployee bte = new BaseEmployee();
                        
	    //인스터스 생성과 동시에 프로퍼티값 넣기            
            FullTimeEmployee fte = new FullTimeEmployee()
            {
                ID = 101,
                FirstName = "Mark",
                LastName = "May",
                AnnualSalary = 60000
            };
 
            ContractEmployee cte = new ContractEmployee()
            {
                ID = 102,
                FirstName = "Brus",
                LastName = "Oh",
                HourlySalary=100,
                TotalHoursWorked=500
            };
 
            //FullTimeEmployee
            Console.WriteLine(fte.GetFullName());
            Console.WriteLine(fte.GetMonthSalary());
 
 
            //ContractEmployee
            Console.WriteLine(cte.GetFullName());
            Console.WriteLine(cte.GetMonthSalary());
 
            
            //BaseEmployee <- 베이스클래스 참조
            Console.WriteLine("BaseEmployee <- 베이스클래스 참조");
            BaseEmployee bte = fte;
            Console.WriteLine(bte.ID+"-"+bte.GetMonthSalary());
            bte = cte;
            Console.WriteLine(bte.ID+"-"+bte.GetMonthSalary());
            
            Console.ReadKey();
               
        }
    }
 
 
    //BaseEmployee  BASE CLASS
    public abstract class BaseEmployee
    {
        public int ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set;}
 
        public string GetFullName(){
            return this.FirstName + " "+this.LastName;
        }
 
        public abstract int GetMonthSalary();
    }
 
 
    //FullTimeEmployee EACH CLASS
    public class FullTimeEmployee:BaseEmployee
    {
        public int AnnualSalary{get; set;}
 
        public override int GetMonthSalary()
        {
 	        return this.AnnualSalary/12;     
        }
    }
    
    //ContractEmployee EACH CLASS
    public class ContractEmployee : BaseEmployee
    {
        public int HourlySalary{get; set;}
        public int TotalHoursWorked{get; set;}
 
        public override int GetMonthSalary()
        {
 	        return this.HourlySalary*this.TotalHoursWorked;     
        }
    }
 
 
}

4cb3f0ba517befeb9b243ed5915443ee_1532507621_7742.jpg

댓글목록

등록된 댓글이 없습니다.

회원로그인

접속자집계

오늘
27
어제
513
최대
1,279
전체
222,590

그누보드5
Copyright © sebom.com All rights reserved.