본문 바로가기
C#

[C#기초] 복사 생성자/ static

by Meaning_ 2021. 7. 27.
728x90
반응형

복사 생성자


자기 자신과 같은 형태의 객체를 인자로 받는 생성자를 의미한다.

학생들의 정보를 담고 있는 Student 클래스를 만들어보겠다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
 class Student
    {
        public int studentID;
        public int age;
        public String name;
        public Student(int studentID,int age,String name)
        {
            Console.WriteLine("인자를 받는 생성자");
            this.studentID = studentID;
            this.age = age;
            this.name = name;
        }
 
        public Student(Student value)
        {
            Console.WriteLine("복사생성자");
            this.studentID = value.studentID;
            this.age = value.age;
            this.name = value.name; 
 
        }
        public void printField()
        {
 
            Console.WriteLine("studentID={0}\nage={1}\nname={2}", studentID, age, name);
        }
 
    }
 
 
    class Program
    {
        static void Main(string[] args)
        {
            Student s1 = new Student(202120"Andrew");
            Student s2 = new Student(s1);
            Console.WriteLine("s1객체");
            s1.printField();
 
            Console.WriteLine("s2객체");
            s2.printField();
        }
    }
cs

 

 

public Student(int studentID,int age,String name)

        {

            Console.WriteLine("인자를 받는 생성자");

            this.studentID = studentID;

            this.age = age;

            this.name = name;

        }

--> 이렇게 인자를 받는 생성자는 자바에서도 자주 본 생성자이다.

public Student(Student value)

        {

            Console.WriteLine("복사생성자");

            this.studentID = value.studentID;

            this.age = value.age;

            this.name = value.name; 

 

        }

--> Student 클래스의 객체인 value를 매개변수로 받아줄 수 있다.

s2 객체에는 s1 객체의 값이 들어있기에 결론적으로 출력값은 같다.

 

 

Static


1.정적멤버와 정적 메서드의 특징

2. static을 사용한 전역변수 구현

 

두가지에 대해서 알아볼 것이다.

 

1.정적멤버와 정적메서드의 특징


1
2
3
4
5
6
7
8
9
10
class A
    {
        public static int count;
        public int val = 0;
        public static void print()
        {
            count = 0;
            //val=0;
        }
    }
cs

정적필드에 count가 있다.이는 클래스에 속해있다.

일반필드에는 val이 있는데, 이는 객체를 만들 때 생성된다.

정적멤버인  print 메서드는 객체를 만들지 않고도 생성된다.

 

주석처리 해놓은 val은 에러가 뜨는데 그이유는 사실상 객체가 만들어지지 않은 상태에서 변수에 접근하는 일이 생길 수 있기때문이다. 왜냐하면 Static메소드는 객체가 메모리를 할당 받기 전에 호출이 되기때문이다.

그렇기에 static은 클래스 안에 변수를 호출할때, 객체를 생성하지 않고  A.count=10; A.print(); 이런식으로 접근이 가능하다.

 

너무 당연한 얘기이지만, 정적 클래스 안에 일반 메소드는 있을 수 없다!

2. static을 사용한 전역변수 구현


C#은 전역변수가 없기 때문에(클래스 바깥에 만들 수 없음)  static을 전역변수처러 구현한다.

점프하고 뛰면 스코어를 1씩 증가시키는 게임을 만들어볼 것이다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
static class Global
    {
        public static int score = 0;
        public static int CurrentStage = 0;
    }
    static class Jump
    {
        public static void cal()
        {
            Global.score+= 1;
        }
        
    }
    static class Run
    {
        public static void cal()
        {
            Global.score += 1;
        }
 
    }
    class Program
    {
        static void Main(string[] args)
        {
            Jump.cal();
            Run.cal();
            Console.WriteLine(Global.score);
        }
    }
cs
 
 
 

 

여기서는 정적변수인 count가 전역변수의 역할을 한다.

 

728x90
반응형

'C#' 카테고리의 다른 글

[C#기초] 가상함수/ 동적바인딩,정적바인딩  (0) 2021.08.14
[C# 기초] 다형성/상속  (0) 2021.07.27
[C#기초] 클래스와 구조체의 차이  (0) 2021.07.21
[C#기초] setter/getter 함수  (0) 2021.07.19
[C#기초] 참조  (0) 2021.07.19

댓글