'프로그래밍/C#'에 해당하는 글 9건

추상클래스

프로그래밍/C# 2020. 3. 22. 16:47

추상 클래스, 추상 메서드

   virtual 예약어로는 자식클래스가 반드시 재정의(override)를 하도록 강제 할 수 없음

   추상메서드를 통해 자식들이 반드시 재정의 하도록 강요 

   부모 클래스에서 abstract 예약어를 지정

   자식 클래스에서 override 예약어를 사용하여 재정의

   구현코드가 없음

   추상클래스는 new를 사용해 인스턴스를 만들 수 없음

   추상메서드는 추상클래스 안에서만 선언

   다중상속 불가능

 

using System;

namespace Example
{
    class Program
    {
        static void Main(string [] args)
        {
            //Food food = new Food();/*오류: 추상클래스 new 사용 불가능*/
            Food coke = new Coke();
            coke.Eat(); //마신다
            Console.WriteLine(coke.Sell(1000));// 1000원을 낸다.
        }
    }
    abstract class Food
    {
        public abstract string Sell(int won);
        virtual public void Eat()
        {
            Console.WriteLine("먹는다");
        }
    }    
    class Coke : Food
    {
        public override string Sell(int won)
        {
            return won + "원을 낸다.";
        }
        new public void Eat()
        {
            Console.WriteLine("마신다");
        }
    }
}

 

'프로그래밍 > C#' 카테고리의 다른 글

다형성  (0) 2020.03.22
Object 의 메서드 - ToString, GetType, Equals, GetHashCode  (0) 2020.03.22
as, is 연산자  (0) 2020.03.20
객체지향 - 3  (0) 2020.03.20
객체지향 -2  (0) 2020.03.20

WRITTEN BY
beautifulhill

,

다형성

프로그래밍/C# 2020. 3. 22. 16:11

다형성 

   하나의 클래스나 메서드가 다양한 방식으로 동작하는 것

   override

   overrode

 

 

override

   부모 클래스의 메소드에는 virtual 예약어를 통해 가상 메서드 생성

   자식 클래스의 메소드에는 override를 통해 다형성 구현

   new 예약어는 override를 사용해 재정의한 것이 아니라 독립적인 메서드이므로 

       자식 클래스를 부모클래스로 암시적 형변환시 부모클래스의 메서드가 불러와진다.

   base 예약어는 부모클래스의 메서드를 호출함

   base 예약어는 부모클래스와 자식클래스 간 중복 된 코드가 있을 경우에 사용

using System;

namespace Example
{
    class Program
    {
        static void Main(string [] args)
        {
            Food food = new Food();
            food.Eat(); //먹는다
            Food bibim = new Bibim();
            bibim.Eat(); //비빈다
            Food coke = new Coke();
            coke.Eat(); //먹는다
            Food bbq = new Bbq();
            bbq.Eat();//굽는다 먹는다
        }
    }
    class Food
    {
        virtual public void Eat() { 
            Console.WriteLine("먹는다"); 
        }
    }
    class Bibim : Food
    {
        override public void Eat()
        {
            Console.WriteLine("비빈다");
        }
    }
    class Coke : Food
    {
        new public void Eat()
        {
            Console.WriteLine("마신다");
        }
    }
    class Bbq : Food
    {
        override public void Eat()
        {
            Console.WriteLine("굽는다");
            base.Eat();
        }
    }

}

 

Object 기본메서드 확장 - ToString 확장

   ovveride(재정의) 안했을 때

using System;

namespace Example
{
    class Program
    {
        static void Main(string [] args)
        {
            Food food = new Food();
            Console.WriteLine(food.ToString());
        }
    }
    class Food
    {
    }
}

   ovveride(재정의) 했을 때

using System;

namespace Example
{
    class Program
    {
        static void Main(string [] args)
        {
            Food food = new Food();
            Console.WriteLine(food.ToString());
        }
    }
    class Food
    {
        public override string ToString()
        {
            return  "먹는다.";
        }
    }
}

 

 

overrode

   메서드의 이름만 같음

   매개변수의 수, 개별 매개변수 타입, 반환값 등은 다를 수 있음

   ex) 다중생성자

'프로그래밍 > C#' 카테고리의 다른 글

추상클래스  (0) 2020.03.22
Object 의 메서드 - ToString, GetType, Equals, GetHashCode  (0) 2020.03.22
as, is 연산자  (0) 2020.03.20
객체지향 - 3  (0) 2020.03.20
객체지향 -2  (0) 2020.03.20

WRITTEN BY
beautifulhill

,

System.Object

   System이라는 네임스페이스의 Object라는 클래스

   모든 타입의 조상

   기본적으로 object에서 상속받는 가정하에 코드를 생성한다.

 

Object의 메서드 - ToString

   해당 인스턴스가 속한 클래스의 전체 이름 FQDN을 반환

   기본타입의 경우 클래스의 전체 이름이 아닌 값을 반환

   하위클래스에서 재정의 가능

using System;
namespace Example
{
    class Program
    {
        static void Main(string [] args)
        {
            Parent p = new Parent();
            string s = "sample";
            int n = 0;
            Console.WriteLine(p.ToString());
            Console.WriteLine(s.ToString());
            Console.WriteLine(n.ToString());
        }
    }
    class Parent{ }
    class Children : Parent {
        public void Method1()
        {
        }
    }
}

 

Object의 메서드 - GetType

  상속 Object -> MemberInfo -> Type 이므로 class 로 타입을 정의할 경우 System.Type 인스턴스를 보유한다.

   GetType은 타입 전체 이름을 반환

   FullName, IsClass, IsArray 프로퍼티를 가지고 있음

using System;
namespace Example
{
    class Program
    {
        static void Main(string [] args)
        {
            Parent p = new Parent();
            string s = "sample";
            int n = 0;
            Console.WriteLine(p.GetType());
            Console.WriteLine(s.GetType());
            Console.WriteLine(n.GetType());
        }
    }
    class Parent{ }
    class Children : Parent {
        public void Method1()
        {
        }
    }
}

 

Object의 메서드 - Equals

   stack에 할당된 값(참조형식의 경우 힙메모리 위치)를 비교

   값형식 : 값을 대상으로 비교

   참조형식 : 할당된 메모리의 위치를 가리키는 식별자의 값 비교

using System;
namespace Example
{
    class Program
    {
        static void Main(string [] args)
        {
            Parent p = new Parent();
            Parent p1 = new Parent();
            string s = "sample";
            string s1 = "sample";
            int n = 0;
            int n1 = 0;
            Console.WriteLine(p.Equals(p1));
            Console.WriteLine(s.Equals(s1));
            Console.WriteLine(n.Equals(n1));
        }
    }
    class Parent{ }
    class Children : Parent {
        public void Method1()
        {
        }
    }
}

 

 

Object의 메서드 - GetHashCode

   인스턴스를 식별할 수 있는 4바이트의 int값을 반환

   타입이 int인 경우는 값을 반환

   long 타입의 경우 8바이트이기 때문에 값이 다름에도 동일한 해시코드가 반환될 수 있음

   해시 충돌이 발생할 수 있으므로 Equals 와 같이 사용

   GetHashCode 의 반환값이 같으면 Equals로 재확인

 

'프로그래밍 > C#' 카테고리의 다른 글

추상클래스  (0) 2020.03.22
다형성  (0) 2020.03.22
as, is 연산자  (0) 2020.03.20
객체지향 - 3  (0) 2020.03.20
객체지향 -2  (0) 2020.03.20

WRITTEN BY
beautifulhill

,

오류를 발생시키지 않고 형변환 할 수 있는 방법

as

   형변환 결과값 반환 

   참조형 변수만 사용가능

 

is

   형변환 가능성 반환

   값 형식도 사용가능

namespace Example
{
    class Program
    {
        static void Main(string [] args)
        {
            int n = 1;
            if(n is string)
            {
                Console.WriteLine("int to string 가능");
            }
            Parent parent = new Parent();
            Children children = new Children();
            if(children is Parent)
            {
            	/*as없이도 parent = children 가능 children = parent 불가능*/
            	children = parent as Children;
                Console.WriteLine("children to Parent 가능");
            }
            if (parent is Children)
            {
                Console.WriteLine("Parent to Children 가능");
            }

        }
    }
    class Parent{ }
    class Children : Parent { }
}

'프로그래밍 > C#' 카테고리의 다른 글

다형성  (0) 2020.03.22
Object 의 메서드 - ToString, GetType, Equals, GetHashCode  (0) 2020.03.22
객체지향 - 3  (0) 2020.03.20
객체지향 -2  (0) 2020.03.20
Main 함수를 알아보자  (0) 2020.03.20

WRITTEN BY
beautifulhill

,

상속

   private으로 선언된 멤버는 상속 불가능

   protected 사용시 자식클래스가 부모의 멤버에 접근 가능

   추상클래스는 다중상속 불가능

using System;
using Product;

namespace Example
{
    class Program
    {
        static void Main(string [] args)
        {
            Idol idol = new Idol();
            idol.Numb = 0;
            idol.Numb = 0;
            idol.Numb = 0;
            bool getPrize = idol.Prize();
            if(getPrize)
                Console.WriteLine("경품받기");

        }
    }   
}
namespace Product {
    class Album
    {
        private int numb;
        private string singer;
        private string album_name;
        public Album()
        {
        }
        
        public int Numb
        {
            get { return numb; }
            set { numb += 1; }
        }
    }

    class Idol : Album
    {
        public bool Prize()
        {
            if (Numb > 2)
                return true;
            else
                return false;
        }
    }
}

protected 사용

using System;
using Product;

namespace Example
{
    class Program
    {
        static void Main(string [] args)
        {
            Idol idol = new Idol();
            idol.Numb = 0;
            idol.Numb = 0;
            idol.Numb = 0;
            bool getPrize = idol.Prize();
            if(getPrize)
                Console.WriteLine("경품받기");

        }
    }   
}
namespace Product {
    class Album
    {
        protected int numb;
        private string singer;
        private string album_name;
        public Album()
        {
        }
        
        public int Numb
        {
            get { return numb; }
            set { numb += 1; }
        }
    }

    class Idol : Album
    {
        public bool Prize()
        {
            if (numb > 2)
                return true;
            else
                return false;
        }
    }
}

'프로그래밍 > C#' 카테고리의 다른 글

Object 의 메서드 - ToString, GetType, Equals, GetHashCode  (0) 2020.03.22
as, is 연산자  (0) 2020.03.20
객체지향 -2  (0) 2020.03.20
Main 함수를 알아보자  (0) 2020.03.20
객체지향-1  (0) 2020.03.19

WRITTEN BY
beautifulhill

,

객체지향 -2

프로그래밍/C# 2020. 3. 20. 15:39

캡슐화

   관련이 있는 변수와 메소드들을 클래스로 묶어 외부로부터 내부 멤버를 은닉하는 것을 캡슐화라고 한다.

   숨겨야 할 변수, 메소드는 private 접근제한자로, 외부에 노출한 기능은 public 접근제한자를 사용한다.

 

접근제한자

   private                : 내부에서만 접근 가능

   protected            : 내부 OR 파생클래스에서 접근 가능

   public                 : 외부에서도 접근 가능

   internal               : 동일한 어셈블리 내에서 접근 가능

   internal protected : 동일 어셈블리 내에서 접근 가능, 다른 어셈블리의 경우 파생클래시인 경우에 접근  

 

default 접근제한자

   class : internal

   class내부 멤버 : private

 

접근자 메서드 & 설정자 메서드 ( getter, setter)

class Program
{
	static void Main(string [] args)
    {
    	Album album = new Product.Album();
        album.SetAlbum("아이들", "I am");
        album.SetAlbum("아이들", "I made");
        album.SetAlbum("아이들", "Uh-Oh");
        Console.WriteLine(album.GetAlbum() + "개");
    }
}
class Album
{
	private int numb;
	private string singer;
	private string album_name;
	public int GetAlbum()
	{
		return numb;
	}
	public void SetAlbum(string singer, string album_name)
	{
		numb += 1;
		this.singer = singer;
		this.album_name = album_name;
	}
}

 

프로퍼티

   setter, getter를 편리하게 사용할 수 있도록하는 문법

using System;
using Product;

namespace Example
{
    class Program
    {
        static void Main(string [] args)
        {
            Album album = new Album();
            album.Numb = 0;
            album.Singer = "아이들";
            album.Album_name = "Uh-Oh";

            album.Numb = 0;
            album.Singer = "아이들";
            album.Album_name = "I am";
            Console.WriteLine(album.Num+"개");
        }
    }   
}
namespace Product {
    class Album
    {
        private int numb;
        private string singer;
        private string album_name;

        public int Numb {
            get {
                return numb;
            }

            set
            {
            numb += 1;
            }
        }
        public string Singer
        {
            get
            {
                return singer;
            }

            set
            {
                singer = value;
            }
        }
        public string Album_name
        {
            get
            {
                return album_name;
            }

            set
            {
                album_name = value;
            }
        }


    }
}

'프로그래밍 > C#' 카테고리의 다른 글

as, is 연산자  (0) 2020.03.20
객체지향 - 3  (0) 2020.03.20
Main 함수를 알아보자  (0) 2020.03.20
객체지향-1  (0) 2020.03.19
break, continue, goto  (0) 2020.03.19

WRITTEN BY
beautifulhill

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

c# 프로그램 실행시 가장 먼저 실행되는 메소드

메소드의 이름은 반드시 Main이어야 한다

정적메소드여야 한다.

매개변수 : string 배열 or 없음

반환값 : void or int

class Program{
    static int Main(){
    	Console.WriteLine("실행");
        return 0;
    }
}

 

'프로그래밍 > C#' 카테고리의 다른 글

as, is 연산자  (0) 2020.03.20
객체지향 - 3  (0) 2020.03.20
객체지향 -2  (0) 2020.03.20
객체지향-1  (0) 2020.03.19
break, continue, goto  (0) 2020.03.19

WRITTEN BY
beautifulhill

,

객체지향-1

프로그래밍/C# 2020. 3. 19. 18:00

객체지향프로그래밍이란?

   상태와 행위로 이루어진 객체라는 단위로 파악하고자 하는 프로그래밍 패러다임이다. 

 

객체의 구성요소

   속성(멤버변수) + 기능/행위(메소드)

 

클래스

   속성과 행위를 변수와 메소드로 정의한 것을 클래스라고 한다.

 

인스턴스(객체)

   클래스로 정의된 타입을 new 연산자로 메모리에 할당한 것을 인스턴스라고 한다.

 

추상화

   데이터를 속성으로, 코드를 메소드로 정의한 것을 추상화라고 한다.

 

생성자

   객체가 생성될때 자동으로 호출되는 메소드

 

종료자

   객체가 제거되는 시점에 실행되는 메소드

class Album
{
    public album(){/*생성자*/
    	Console.WriteLine("생성자");
    }
    ~album(){
    	Console.WriteLine("종료자");
    }
}

 

인스턴스 멤버 

   new연산자를 거쳐 메모리에 할당된 객체에 속한 멤버

   개별 인스턴스 내에서만 유효

   스택에는 인스턴스 필드, 힙에는 값이 할당

 

정적 멤버

   모든 객체가 공유하는 멤버, 전역적으로 적용될 필드, 메소드, 생성자

   static으로 선언

   인스턴스를 만들지 않고(new연산자 사용안하고도) 바로 접근 가능

 

인스턴스 필드 예시

class Test{
    static void Main(string[] args){
    	Album a1 = new Album();
    	Album a2 = new Album();
    	Album a3 = new Album();  
    }
}
class Album{
    public int numb;
    public string singer;
    public string album_name;
    
    public Album(){
    	numb++;
       	Console.WriteLine(numb);
    }
}

 

정적필드 예시

class Test{
    static void Main(string[] args){
    	Album a1 = new Album();
    	Album a2 = new Album();
    	Album a3 = new Album();  
    }
}
class Album{
    static public int numb; //정적필드
    public string singer;
    public string album_name;
    
    public Album(){
    	numb++;
       	Console.WriteLine(numb);
    }
}

 

정적필드는 인스턴스를 만들지 않고도 접근 가능 ([클래스명].[정적필드])

class Test{
    static void Main(string[] args){
    	Album a1 = new Album();
    	Album a2 = new Album();
    	Album a3 = new Album();  
        Console.WriteLine(Album.numb);
    }
}
class Album{
    static public int numb; //정적필드
    public string singer;
    public string album_name;
    
    public Album(){
    	numb++;
    }
}

 

싱글톤 패턴

   인스턴스가 최초 한번만 메모리에 할당되어 전역적으로 접근 가능하도록 하는 디자인패턴

   메모리 낭비 방지

 

 

싱글톤 클래스 예제 (생성자가 단 한번만 실행되기 때문에 numb의 값이 한번만 증가함)

class Test{
    static void Main(string[] args){
    	Album.album.Count();
    }
}
class Album{
    static public Album album = new Album();
    private int numb;
    private string singer;
    private string album_name;
    private Album(){
    	numb++;
    }
    public void Count(){
    	Console.WriteLine(numb);
    }
}

 

정적메소드

   static 예약어가 붙은 메소드

   정적메소드 안에서는 인스턴스 멤버에 접근이 불가능하다.(new로 할당된 객체가 없기 때문)

class Album{
    private int numb;
    private string singer;
    private string album_name;
    private Album(){
    	numb++;
    }
    static public void Count(){
    	Console.WriteLine(numb);
        //에러 : 정적메소드에서 인스턴스 멤버에 접근 불가
    }
}
class Album{
    static private int numb;
    private string singer;
    private string album_name;
    private Album(){
    	numb++;
    }
    static public void Count(){
    	Console.WriteLine(numb);
        //정적메소드에서 정적필드 접근 가능
    }
}

 

Console.WriteLine

   Console 클래스에 정의된 WriteLine 정적 메소드

 

 

정적 생성자 type initializer

   static 예약어를 붙임

   클래스에 단 한개만 존재할 수 있음

   정적 데이터를 초기화하거나 한 번만 수행하는 작업 시 사용

   처음 인스턴스가 생성될 때 OR 정적 멤버가 참조되기 전에 자동으로 호출

 

정적필드를 참조할 때 정적 생성자 호출

class Test{
    static void Main(string[] args){
    	int numb = Album.numb;
    }
}
class Album{
    static public int numb;
    private string singer;
    private string album_name;
    public Album(){
    	numb++;
    	Console.WriteLine("생성자");
    }
    static Album(){
    	Console.WriteLine("정적생성자");
    }
}

 

 

인스턴스가 생성될 때 정적생성자 호출 

    정적생성자는 처음 한번만 호출된다.

class Test{
    static void Main(string[] args){
    	Album album1 = new Album();
    	Album album2 = new Album();
    	Album album3 = new Album();
    	int numb = Album.numb;
    }
}
class Album{
    static public int numb;
    private string singer;
    private string album_name;
    public Album(){
    	numb++;
    	Console.WriteLine("생성자");
    }
    static Album(){
    	Console.WriteLine("정적생성자");
    }
}

 

 

네임스페이스

   이름이 똑같은 클래스를 사용할 때 사용

   클래스의 소속을 구분할 때 사용

   using 예약어를 사용하여 네임스페이스를 생략할 수 있음

 

   단 using 예약어는 파일 첫 부분에 있어야함

 

using 사용 안할때

using System;
namespace Example
{
    class Program
    {
        static void Main(string [] args)
        {
            Product.Album album1 = new Product.Album();         
        }
    }   
}
namespace Product{
    class Album
    {        
    }
}

 

using 사용할 때

using System;
using Product;
namespace Example
{
    class Program
    {
        static void Main(string [] args)
        {
            Album album1 = new Album();            
        }
    }   
}
namespace Product{
    class Album
    {        
    }
}

 

using System

   Console 클래스가 System이라는 namespace 내부에 정의됨

   Consel.WriteLine = System.Console.WriteLine

   네임스페이스 : System

   클래스         : Console

   정적메소드   : WriteLine

'프로그래밍 > C#' 카테고리의 다른 글

as, is 연산자  (0) 2020.03.20
객체지향 - 3  (0) 2020.03.20
객체지향 -2  (0) 2020.03.20
Main 함수를 알아보자  (0) 2020.03.20
break, continue, goto  (0) 2020.03.19

WRITTEN BY
beautifulhill

,