거의 알고리즘 일기장

클래스의 private의 접근도 편하게 _ get, set 본문

c# 문법

클래스의 private의 접근도 편하게 _ get, set

건우권 2020. 5. 13. 17:26

예제 1

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading;
using System.Diagnostics.CodeAnalysis;

namespace ConsoleApp1
{  
    class Car
    {
        private string color;
        private float speed;
        public Car(string color_in = "red", float speed_in = 0.0f)
        {
            color = color_in;
            speed = speed_in;
        }
        public string Color
        {
            get { return color; }
            set { color = value; }
        }       
        public float Speed
        {
            get { return speed; }
            set { speed += value; }
        }
        public virtual void print()
        {
            Console.WriteLine("일반소형차ㅠㅠ");
            Console.WriteLine("컬러 :{0},\n스피드 : {1}\n", color, speed);
        }
    }
    class SportsCar : Car
    {
        private string tubo;
        public SportsCar(string color_in = "red", float speed_in = 0.0f, string tubo_in ="good") : base(color_in, speed_in)
        {
            this.tubo = tubo_in;
        }
        public string Tubo
        {
            get { return tubo; }
            set { tubo = value; }
        }
        public override void print()
        {
            Console.WriteLine("스포츠카!!");
            Console.WriteLine("컬러 : {0},\n스피드 : {1},\ntubo : {2}\n", Color, Speed, tubo);
        }
    }
    class Program
    {
        //다형성을 이용한 함수
        static void print(Car car)
        {
            car.print();
        }
        static void Main(string[] args)
        {
       	//lvalue 일때 set, rvalue일때 get을 사용 하는듯??? 
            SportsCar sportscar = new SportsCar();
            sportscar.Color = "yellow";
            sportscar.Speed = 15.0f;
            sportscar.Speed+=2.0f;

            print(sportscar);
        }
    }
}

 

반응형

'c# 문법' 카테고리의 다른 글

ref, out 예제  (0) 2020.05.14
클래스의 상속과 초기화 예제 + virtual까지의 예제  (0) 2020.05.13
문자열 위치찾기  (0) 2020.05.12
박싱과 언박싱 예제  (0) 2020.05.11
class를 이용한 간단 예제 2개  (0) 2020.05.11
Comments