본문 바로가기
Coding/C#

C# 예제로 배우기 customer

by 찡콩찡 2022. 12. 20.
Customer.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace AnimalShelter
{

    //internal은 같은 namespace안에 있는 class에서만 사용가능!
    internal class Customer
    {
        public string FirstName;
        public string LastName;
        private int _Age;
        private bool _IsQualified;
        public string Address;
        public string Description;

       
        //이름과 나이를 사용하는 생성자를 생성하기
        //return type 선언하지 않음, 메서드의 명은 클래스명과 같아야 함.필요한 매개변수 넣기
        public Customer(string firstName, string lastName, int age) {
        
            this.FirstName = firstName;
            this.LastName = lastName;   
            this._Age = age;

            this._IsQualified = age >= 18;

        }
        //private인 age와 isqualified에 접근하여 사용할 수 있는  function을 만들어본다.

        //1.age에 접근할 function 로직 짜기
        public int GetAge() {
            return _Age;
        }
        //age에 값을 넣는 function,값을 넣는 용도로만 사용할 것이므로 return할 필요가 없음
        public void SetAge(int age)        {
            _Age = age;
            _IsQualified = age >= 18;
        }

        //AGE 속성 정의
        public int Age
        {
            get {  return _Age;   }
            //value는 int형으로 들어오는 매개변수임 값을 set하기 위해서
            set
            {
                _Age = value;
                _IsQualified = value >= 18;
            }
        }

        //isqualified의 변수를 읽을 수 있는 function을 추가해준다. 
        //
       

        public bool IsQualified
        {
            get
            {
                return _IsQualified;
            }  
            
        }
        //fullname은 읽기전용
        public string 
FullName
        {
            get {
                return FirstName + "" + LastName;
            }
        }
        public string GetFullName() {
            return  FirstName + "" + LastName;
        }

    }
}
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;



namespace AnimalShelter
{
    //partial 클래스 정의가 여러 파일로 나뉘어져있다고 생각하면 됨
    public partial class Form1 : Form  //Form1이 form을 상속받는다는 의미
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void CreateCustomer_Click(object sender, EventArgs e)
        {
            Customer cus = new Customer("Ian", "Na", 10);
            cus.Address = "123 wilshire Bla.";
            
            CustFullName.Text = cus.FullName;            
            //AGE는 숫자라서 INT타입이고 TEXT는 STRING 타입 그러므로
            //모든 데이터타입을 STRING으로 만들어주는 FUNCTION으로 TOSTRING을 써준다.
            CusAge.Text = cus.Age.ToString();
            CusAddress.Text = cus.Address;
            CusDescription.Text = cus.Description;

            //필드처럼 사용가능
            bool test = cus.IsQualified;

        }
    }
}

 

Form1.cs

실행했을 때