본문 바로가기
C#/수업내용

[ C# 8일차 ] Struct 구조체

by 왹져박사 2023. 1. 10.
728x90

App Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace Study09
{
    class App
    {
        //생성자
        public App()
        {
            Point3D point3d;
            Console.WriteLine("App");

            //Console.WriteLine(point3d);
            point3d.x = 10;
            point3d.y = 10;
            point3d.z = 10;
            Console.WriteLine("{0} {1} {2}",point3d.x, point3d.y, point3d.z);

            Point3D point3d2 = new Point3D(100, 200, 300);
            Point3D point3d3 = point3d2;                //깊은 복사
            point3d3.z = 400;                                   //Class라면 point3d2도 같이 바뀜

            Console.WriteLine("{0} {1} {2}", point3d2.x, point3d2.y, point3d2.z);
            Console.WriteLine("{0} {1} {2}", point3d3.x, point3d3.y, point3d3.z);

        }
    }
}

 

Struct Point3D

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Study09
{
    //구조체 정의
    //값형식
    //스택저장
    //기본생성자 못씀
    //매개변수 모두 멤버변수에 할당해야함
    //상속 불가능 
    //new 키워드로 인스턴스 생성가능
    //Point3D point3D = new Point3D(10, 10, 10);
    //new 없이 모든 필드를 채우면서 인스턴스 생성가능
    //ex)
    //Point3D point3D;
    //point3D.x = 10;
    //point3D.y = 10;
    //point3D.z = 10;

    struct Point3D
    {
        //멤버 변수
        public int x;
        public int y;
        public int z;

        //생성자(매개변수가 있어야 함
        public Point3D(int x, int y, int z)
        {
            //매개변수값은 반드시 모두!!!!!!! 멤버 변수에 할당해야 한다. 
            this.x = x;
            this.y = y;
            this.z = z;
        }

        //멤버 메서드
        public  int GetX()
        {
            return this.x;
        }
    }
}
728x90