Form1.cs
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 | using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Cow_calculator { public partial class Form1 : Form { Farmer farmer; public Form1() { InitializeComponent(); farmer = new Farmer(15,30); } private void button1_Click(object sender, EventArgs e) { Console.WriteLine("I need " + farmer.BagsOfFeed + " bags of feed for " + farmer.NumberOfCows + " cows"); } private void numericUpDown1_ValueChanged(object sender, EventArgs e) { farmer.NumberOfCows = (int)numericUpDown1.Value; } } } | cs |
Farmer.cs
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 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Cow_calculator { class Farmer { public int BagsOfFeed { get; private set; } private int feedMultiplier; public int FeedMultiplier { get { return feedMultiplier; } } private int numberOfCows; public int NumberOfCows { get { return numberOfCows; } set { numberOfCows = value; BagsOfFeed = numberOfCows * FeedMultiplier; } } public Farmer(int number,int mult) { feedMultiplier = mult; NumberOfCows = number; } } } [출처] c# Head First - 캡슐화|작성자 길가다주은노트북 |
'프로그래밍 > C#' 카테고리의 다른 글
(C#) get, set 접근자 (0) | 2016.07.09 |
---|---|
(C#) Head First - 캡슐화 PartyPlaner (0) | 2016.07.09 |
(C#) head first - MessageBox 사용 (0) | 2016.07.09 |
(C#) Head First - 경마 (0) | 2016.07.09 |
(C#) WPF로 만든 심플 게임 (1) | 2016.07.09 |