iii) 형 검사 연산자(type testing operator) is – 호환 가능한지를 검사 as – 지정한 형으로 변환
iiii) 배열 int[] vector = new int[100]; short[,] matrix = new short[10,100]; long[][] arrayOfArray; object[] myArray1, myArray2; -> myArray1 = new Point[3];
# 클래스
클래스의 구성 – 클래스 멤버 필드 계통 – 상수 정의, 필드, 이벤트 메소드 계통 – 메소드, 생성자, 소멸자, 프로퍼티, 인덱서, 연산자 중복
# 프로퍼티
클래스의 private 필드를 형식적으로 다루는 일종의 메소드
클래스의 private 필드는 정보 은닉 (Information Hiding)을 위해 다른 클래스에서는 임의 접근이 허용되지 않지만 대응되는 프로퍼티를 통하여 다른 클래스에서 형식적으로 접근할 수 있는 방법이 제공되는 것
값 지정 셋-접근자(set-accessor)
값 참조 겟-접근자(get-accessor)
using System;
namespace HelloWorld
{
class PropertyClass
{
private int privateValue;
public int PrivateValue
{
get { return privateValue; }
set { privateValue = value; }
}
public void PrintValue()
{
Console.WriteLine("Hidden Value + " + privateValue);
}
}
class Program
{
static void Main(string[] args)
{
int n;
PropertyClass obj = new PropertyClass();
obj.PrivateValue = 100;
obj.PrintValue();
n = obj.PrivateValue;
Console.WriteLine(" Value = " + n);
}
}
}
P.26 PropertyApp.cs
# 실습
P.27 1-5 연산자 중복
using System;
using System.Collections.Generic;
using System.Text;
namespace HelloWorld {
class Even {
int evenNumber;
public Even(int n) { // 생성자
evenNumber = (n % 2 == 0) ? n : n + 1;
}
public static Even operator++(Even e) { // ++ 연산자
e.evenNumber += 2;
return e;
}
public static Even operator--(Even e) { // -- 연산자
e.evenNumber -= 2;
return e;
}
public void PrintEven() { // 출력 메소드
Console.WriteLine("Even Number = " + evenNumber);
}
}
class OperatorOverloadingApp {
public static void Main() {
Even e = new Even(4);
e.PrintEven();
++e;
e.PrintEven();
--e;
e.PrintEven();
}
}
}
P.27 OperatorOverloadingApp.cs
P.112 3-2 혼합문
using System;
using System.Collections.Generic;
using System.Text;
namespace HelloWorld {
class CompoundStApp {
public static void Main() {
int n;
Console.Write("Enter on digit = ");
n = Console.Read() - '0';
if (n < 0) {
Console.WriteLine("Negative number !");
} else {
Console.WriteLine(n + " squared is " + (n * n));
Console.WriteLine(n + " cubed is " + (n * n * n));
}
}
}
}
P.113 3-3 블록
using System;
using System.Collections.Generic;
using System.Text;
namespace HelloWorld {
class AnotherBlockApp {
public static void Main() {
int x = 1;
{
int y = 2;
Console.WriteLine("Block 1: x = " + x + ", y = " + y);
}
{
int y = 3;
Console.WriteLine("Block 2: x = " + x + ", y = " + y);
}
}
}
}
P.62 2-9 열거형
using System;
using System.Collections.Generic;
using System.Text;
namespace HelloWorld {
enum Color { Red, Green, Blue };
class EnumTypeApp {
public static void Main() {
Color c = Color.Red;
c++;
int i = (int) c;
Console.WriteLine("Cardinality of " + c + " = " + i);
}
}
}
P.81 2-21 조건 연산자
using System;
using System.Collections.Generic;
using System.Text;
namespace HelloWorld {
class ConditionalOperatorApp {
public static void Main() {
int a, b, c;
int m;
Console.Write("Enter three numbers : ");
a = Console.Read() - '0';
b = Console.Read() - '0';
c = Console.Read() - '0';
m = (a > b) ? a : b;
m = (m > c) ? m : c;
// m = a > b ? (c > a ? c : a) : (c > b ? c : b);
Console.WriteLine("The largest number = " + m);
}
}
}