언어 자료구조 알고리즘/프로그래밍 실습
[C#] 캡슐화 실습 - 복소수 정의(멤버 필드와 멤버 속성)
언제나휴일
2019. 8. 8. 11:23
반응형
/* * 캡슐화 실습1 - 다음을 만족하는 클래스를 정의하시오. * 실습 목적: 멤버 필드와 멤버 속성을 이해하고 활용하기 * 클래스: 복소수 * 멤버 필드: * -실수부:int * -허수부:int * 멤버 메서드: * +생성자() * +생성자(실수부) * +생성자(실수부, 허수부) * +접근자_실수부():int * +설정자_실수부(real:int):void * +접근자_허수부():int * +설정자_허수부(image:int):void * +재정의_ToString():string */ |
*2019년 공주대에서 |
소스 예) Complex.cs |
using System; namespace 캡슐화_실습1_복소수 { /// /// 복소수 클래스 - Complex /// class Complex { //멤버 필드: //int real;//-실수부:int(자동 속성으로 제공) //int image;//-허수부:int(자동 속성으로 제공) //멤버 메서드: public Complex()//* +생성자() { } public Complex(int real)//* +생성자(실수부) { Real = real; } public Complex(int real,int image) //+생성자(실수부, 허수부) { Real = real; Image = image; } public int Real { get;//+접근자_실수부() :int set;//+설정자_실수부(real:int) :void } public int Image { get;//+접근자_허수부() :int set;//+설정자_허수부(image:int) :void } public override string ToString()//+재정의_ToString() :string { return string.Format("{0}+{1}i", Real, Image); } } } |
Program.cs |
using System;
namespace 캡슐화_실습1_복소수 { class Program { static void Main(string[] args) { Complex c1 = new Complex(); Console.WriteLine(c1);
Complex c2 = new Complex(3); Console.WriteLine(c2);
Complex c3 = new Complex(3,4); Console.WriteLine(c3);
c3.Real = 9; Console.WriteLine(c3);
c3.Image = 7; Console.WriteLine(c3);
Console.WriteLine("실수부:{0}, 허수부:{1}", c3.Real, c3.Image); } } }
|
반응형