gnu

블로그 이미지
by 군우

TAG CLOUD

  • Total hit
  • Today hit
  • Yesterday hit

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace PrivateAccessMain
{
    class Program
    {
        class Person
        {
            public int age;
            public float height;
            private float weight;
        }
 
        static void Main(string[] args)
        {
            Person brorher = new Person();
            brorher.age = 100;
            brorher.height = 170.0F;
            brorher.weight = 67.0F;
 
            Console.WriteLine("age: " + brorher.age);
            Console.WriteLine("height: " + brorher.height);
            Console.WriteLine("weight: " + brorher.weight);
        }
    }
}
cs

err발생  private 보호 수준때문에 접근 불가함.


/// 

private로 선언된 멤버변수에 접근하는법

public 으로 선언된 메소드를 통해 private 변수에 접근한다. 

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace TopSecretMethod
{
    class Program
    {
        static void Main(string[] args)
        {
            TopSecret t = new TopSecret();
            t.SetSecret(1000);
 
            int s = t.GetSecret();
            Console.WriteLine("secret = " + s);
        }
 
        class TopSecret
        {
            private int secret;
 
            public void SetSecret(int x) { secret = x; }
            public int GetSecret() { return secret; }
        }
    }
}
cs


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

[C#] 다형성 (polymorphism)  (0) 2018.03.22
[C#] 파생 클래스  (0) 2018.03.22
[C#] Class의 인스턴스  (0) 2018.03.22
[C#] break. continue  (0) 2018.03.22
[C#] thread 쓰레드  (0) 2018.03.19
AND

ARTICLE CATEGORY

분류 전체보기 (197)
C (0)
HTML (7)
C# (88)
Python (27)
IT (0)
Android (2)
Java (65)
Study (5)
JavaScript (1)
JSP (2)

RECENT ARTICLE

RECENT COMMENT

CALENDAR

«   2025/04   »
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

ARCHIVE

LINK