클래스 (Class) : 학급
| 사물 | 상태(필드) | 행동(메소드) |
|---|---|---|
| 사람 | 피부색, 키, 나이, 성별, 이름 | 생각한다. 공부한다. 말한다. 걷는다. |
| 차 | 배기량, 차종, 연료의 종류 | 달린다. 멈춘다. 짐을 싣는다. |
| 노트북 | CPU, 메모리 크기, 하드디스크 용량 | 부팅한다. 충전한다. CD-ROM 읽는다. |
class 키워드를 쓰고, 그 뒤에 클래스 이름을 붙인다.class 클래스 이름 // 클래스 선언
{
접근 지정자 클래스 이름() { ... } // 생성자
접근 지정자 ~클래스 이름() { ... } // 소멸자
접근 지정자 데이터형 멤버 변수(필드); // 변수 선언
접근 지정자 데이터형 메소드() { ... }; // 메소드 선언
}

class Dog
{
public Dog() {}
private int eyes, nose, mouse, ears;
public void bark() {}
}
| 접근 지정자 | 의미 |
|---|---|
| public | 누구나 접근 가능하다. |
| protected | 상속 관계에 있을 때 상속 받은 자식 클래스에서 접근 가능하다. 그 외에는 접근 불가하다. |
| internal | 같은 어셈블리(프로젝트) 내의 모든 클래스가 접근 가능하다. |
| protected internal | protected와 internal의 의미를 모두 포함한다. |
| private | 내 클래스 내부에서만 접근 가능하고, 외부에서는 절대 접근할 수 없다. |
Dog a;
Dog a= new Dog();
a는 주소값을 갖고있는 포인터 변수..로 생각하면 됨
using System;
class Dog {
private int eyes, nose, mouse, ears;
public void bark() {
Console.WriteLine("멍멍");
}
}
class HelloWorld {
static void Main() {
Dog a = new Dog();
a.bark();
}
}
public 타입이어야 함. using System;
class Dog {
private int eyes, nose, mouse, ears;
public void bark() {
Console.WriteLine("멍멍");
}
public Dog() {
eyes = 0;
nose = 0;
mouse = 0;
ears = 0;
}
}
class HelloWorld {
static void Main() {
Dog a = new Dog();
a.bark();
}
}
extends를 붙인 후 부모클래스의 이름을 적는다. 접근 지정자 클래스 이름 : 부모 클래스
{
// 멤버 목록
}
using System;
class Dog {
protected int eyes, nose, mouse, ears;
public void bark() {
Console.WriteLine("멍멍");
}
public Dog() {
eyes = 0;
nose = 0;
mouse = 0;
ears = 0;
}
}
class Retriever : Dog {
public Retriever() {
base.eyes = 2;
Console.WriteLine("리트리버 눈 : {0}", eyes);
}
}
class HelloWorld {
static void Main() {
Dog a = new Dog();
a.bark();
Retriever rt = new Retriever();
}
}

protected: 외부에서는private처럼 보임. 그러나 상속 관계에서 자기 자식에게는public처럼 접근할 수 있게 허용해줌
base: 부모 객체를 나타냄.
int Plus(int a, int b) {
return a + b;
}
char Plus(char a, char b) {
return a + b;
}
double Plus(double a, double b) {
return a + b;
}
using System;
public class Zerg {
public void Overload(int zerggling) {
Console.WriteLine("저글링 {0}마리", zerggling);
}
public void Overload(int zerggling, int hydra) {
Console.WriteLine("저글링 {0}마리 + 히드라 {1}마리", zerggling, hydra);
}
public void Overload(int zerggling, int hydra, int lurker) {
Console.WriteLine("저글링 {0}마리 + 히드라 {1}마리 + 럴커 {2}마리", zerggling, hydra, lurker);
}
public void Overload(char zerggling) {
Console.WriteLine("저글링 {0}등급", zerggling);
}
}
class HelloWorld {
static void Main() {
Zerg zerg = new Zerg();
zerg.Overload(10);
zerg.Overload(10, 20);
zerg.Overload(10, 20, 30);
zerg.Overload('A');
}
}

using System;
class Dog {
protected int eyes, nose, mouse, ears;
virtual public void bark() {
Console.WriteLine("멍멍");
}
public Dog() {
eyes = 0;
nose = 0;
mouse = 0;
ears = 0;
}
}
class Retriever : Dog {
public Retriever() {
base.eyes = 2;
Console.WriteLine("리트리버 눈 : {0}", eyes);
}
public override void bark() {
Console.WriteLine("월월");
}
}
class Chihuahua : Dog {
public Chihuahua() {
base.eyes = 2;
Console.WriteLine("치와와 눈 : {0}", eyes);
}
public override void bark() {
Console.WriteLine("캉캉");
}
}
class HelloWorld {
static void Main() {
Dog dog = new Dog();
dog.bark();
Retriever rt = new Retriever();
rt.bark();
dog = new Chihuahua();
dog.bark();
}
}

접근지정자 interface 이름 : 기반 인터페이스
{
}
접근지정자 class 자식클래스이름 : 인터페이스
{
}
interface 통화기능 {}
interface 문자메시지기능 {}
interface 와이파이기능 {}
interface 멀티미디어기능 {}
interface 블루투스기능 {}
class 스마트폰_기능 통화기능, 문자메시지기능, 와이파이기능, 멀티미디어기능, 블루투스기능
{
통화기능구현
문자메시지구현
와이파이구현
멀티미디어구현
블루투스구현
}
implements를 사용함using System;
public interface IUnit {
void Attack();
void Move();
}
public class Zergling : IUnit {
public void Attack() {
Console.WriteLine("저글링 : 공격한다.");
}
public void Move() {
Console.WriteLine("저글링 : 이동한다.");
}
}
public class Dragoon : IUnit {
public void Attack() {
Console.WriteLine("드라군 : 공격한다.");
}
public void Move() {
Console.WriteLine("드라군 : 이동한다.");
}
}
class HelloWorld {
static void Main() {
Zergling zerg = new Zergling();
zerg.Attack();
zerg.Move();
Dragoon dragoon = new Dragoon();
dragoon.Attack();
dragoon.Move();
}
}

void func() {
int[] ar = new int[10];
DateTime Now = new DateTime(2002, 6, 25);
// 객체를 실컷 사용한다
}
using System;
class HelloWorld {
static int Add(int a) {
return a + 1;
}
static void Main() {
Console.WriteLine(Add(3));
}
}
using System;
class HelloWorld {
static int Add(int a) {
return a + 1;
}
delegate int CalcDele(int x);
static void Main() {
Console.WriteLine(Add(3));
CalcDele d = delegate (int x) {
return x + 1;
};
Console.WriteLine(d(3));
}
}
(인수) => 표현식 또는 명령문
(x, y) => x + y람다식은 "x와 y가 x+y가 된다."로 읽는다.=> 기호로 대체된다.delegate(int x, int y) { return x + y; }
↓
(x, y) => x + y;
즉, 위의 코드를 아래와 같이 바꿀 수 있음
using System;
class HelloWorld {
static int Add(int a) {
return a + 1;
}
delegate int CalcDele(int x);
static void Main() {
Console.WriteLine(Add(3));
CalcDele d = x => x + 1;
Console.WriteLine(d(3));
}
}