C#/C# Concept
[C#] 접근제한자
군우
2018. 3. 22. 10:19
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 |