this, this()

Fruit·2023년 3월 29일

✨ Hello C#!

목록 보기
25/34
post-thumbnail

🌸 this

  • 클래스 내 메서드(객체 내부)에서 자신의 필드나 메서드에 접근할 때 사용한다.
using System;

namespace This
{
    class Employee
    {
        private string Name;
        private string Position;

        public void SetName(string Name)
        {
            this.Name = Name;
        }

        public string GetName()
        {
            return Name;		// this.Name
        }

        public void SetPosition(string Position)
        {
            this.Position = Position;
        }

        public string GetPosition()
        {
            return this.Position;		// Postion
        }

    }

    class MainApp
    {
        static void Main(string[] args)
        {
            Employee fruit = new Employee();
            fruit.SetName("Fruit");
            fruit.SetPosition("Waiter");

            Console.WriteLine($"{fruit.GetName()}, {fruit.GetPosition()}");
        }
    }
}

[실행 결과]
Fruit, Waiter



🌸 this()

  • 같은 클래스에 여러 생성자가 존재할 때, 중복되는 코드가 있을 경우 사용한다.
  • 생성자에서만 사용할 수 있다.

this() 사용 X

using System;

namespace ThisConstructor
{
    class MyCalss
    {
        int a, b, c;

        public MyCalss()
        {
            this.a = 1234;
        }

        public MyCalss(int b)
        {
            this.a = 1234;
            this.b = b;
        }

        public MyCalss(int b, int c)
        {
            this.a = 1234;
            this.b = b;
            this.c = c;
        }

        public void PrintFields()
        {
            Console.WriteLine($"{a, -7} {b, -7} {c, -7}");
        }
    }

    class MainApp
    {
        static void Main(string[] args)
        {
            Console.WriteLine($"{"a",-7} {"b",-7} {"c",-7}");

            MyCalss a = new MyCalss();			//	MyCalss()
            a.PrintFields();

            MyCalss b = new MyCalss(1);			// MyCalss(int b)
            b.PrintFields();

            MyCalss c = new MyCalss(10, 20);		// MyCalss(int b, int c)
            c.PrintFields();
        }
    }
}

[실행 결과]
a       b       c
1234    0       0
1234    1       0
1234    10      20

this() 사용 O

using System;

namespace ThisConstructor
{
    class MyCalss
    {
        int a, b, c;

        public MyCalss()
        {
            this.a = 1234;
        }

        public MyCalss(int b): this()		// MyClass() 호출
        {
            this.b = b;
        }

        public MyCalss(int b, int c): this(b)		// // MyClass(int) 호출
        {
            this.c = c;
        }

        public void PrintFields()
        {
            Console.WriteLine($"{a, -7} {b, -7} {c, -7}");
        }
    }

    class MainApp
    {
        static void Main(string[] args)
        {
            Console.WriteLine($"{"a",-7} {"b",-7} {"c",-7}");

            MyCalss a = new MyCalss();
            a.PrintFields();

            MyCalss b = new MyCalss(1);
            b.PrintFields();

            MyCalss c = new MyCalss(10, 20);
            c.PrintFields();
        }
    }
}

[실행 결과]
a       b       c
1234    0       0
1234    1       0
1234    10      20
profile
🌼인생 참 🌻꽃🌻 같다🌼

0개의 댓글