학습 일자 : 2023.04.04


클래스 (class)

데이터와 관련 기능을 캡슐화할 수 있는 참조 형식 (참조형식 설명 : 값형식 VS 참조형식)

객체지향 프로그래밍에 객체를 만들기 위한 설계도이다

만들어진 객체는 인스턴스라고 한다.

<aside> 💡 class 기본 구조 [접근제한자] class (클래스이름) { …클래스내용… }

</aside>

	class UserClass
	{
		public void StudyClass()
		{
			Student std = new Student(); //new라는 키워드를 통해 클래스 인스턴스를 생성
			std.Name = "Kim"; //인스턴스 변수 접근 시 점을 사용
			std.Math = 90;
			std.English = 86;
			std.GetAverage();  //인스턴스 메소드 접근 시 점을 사용
		}
	}

	class Student 
	{
		public string Name;
		public int Math;
		public int English;

		public float GetAverage() { return (Math + English) / 2f; }
	}

클래스의 생성자

	class Student 
	{
		public string Name;
		public int Math;
		public int English;

		 //기본 생성자 : 생성자 없으면 자동 생성, 있으면 안 만들어짐
		public Student() { }
							
		//생성자 오버로딩 1
		public Student(string name) 
		{ 
			this.Name = name;
		}

		public Student(string name, int math, int eng) //생성자 오버로딩 2 
		{
			this.Name = name;
			this.Math = math;
			this.English = eng;
		}
	}

값형식 VS 참조형식

struct는 값형식이고, class는 참조형식이다.