1つのインスタンスからオブジェクト向けプログラミングを見る
6000 ワード
プロシージャ向けからオブジェクト向けのインプリメンテーションプロシージャ(カプセル化、継承、マルチステート)
Code1:
code 2:パッケージ
code 3:ビジネス層とインタフェース層をさらに分離するパッケージ実装
code 4:(継承,マルチステートのOOP応用と実装Demo)
Code1:
class Program
{
static void Main(string[] args)
{
Console.Write(" A:");
string A = Console.ReadLine();
Console.Write(" (+、-、*、/):");
string B = Console.ReadLine();
Console.Write(" B:");
string C = Console.ReadLine();
string D = "";
if (B == "+")
D = Convert.ToString(Convert.ToDouble(A) + Convert.ToDouble(C));
if (B == "-")
D = Convert.ToString(Convert.ToDouble(A) - Convert.ToDouble(C));
if (B == "*")
D = Convert.ToString(Convert.ToDouble(A) * Convert.ToDouble(C));
if (O == "/")
D = Convert.ToString(Convert.ToDouble(A) / Convert.ToDouble(C));
Console.WriteLine(" :" + D);
}
}
code 2:パッケージ
class Program
{
static void Main(string[] args)
{
try
{
Console.Write(" A:");
string strNumberA = Console.ReadLine();
Console.Write(" (+、-、*、/):");
string strOperate = Console.ReadLine();
Console.Write(" B:");
string strNumberB = Console.ReadLine();
string strResult = "";
switch (strOperate)
{
case "+":
strResult = Convert.ToString
(Convert.ToDouble(strNumberA) + Convert.ToDouble(strNumberB));
break;
case "-":
strResult = Convert.ToString
(Convert.ToDouble(strNumberA) - Convert.ToDouble(strNumberB));
break;
case "*":
strResult = Convert.ToString
(Convert.ToDouble(strNumberA) * Convert.ToDouble(strNumberB));
break;
case "/":
if (strNumberB != "0")
strResult = Convert.ToString
(Convert.ToDouble(strNumberA) / Convert.ToDouble(strNumberB));
else
strResult = " 0";
break;
}
Console.WriteLine(" :" + strResult);
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(" :" + ex.Message);
}
}
}
code 3:ビジネス層とインタフェース層をさらに分離するパッケージ実装
class Program
{
static void Main(string[] args)
{
try
{
Console.Write(" A:");
string strNumberA = Console.ReadLine();
Console.Write(" (+、-、*、/):");
string strOperate = Console.ReadLine();
Console.Write(" B:");
string strNumberB = Console.ReadLine();
string strResult = "";
strResult = Convert.ToString(
Operation.GetResult(Convert.ToDouble(strNumberA),
Convert.ToDouble(strNumberB), strOperate));
Console.WriteLine(" :" + strResult);
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(" :" + ex.Message);
}
}
}
public class Operation
{
public static double GetResult(double numberA, double numberB, string operate)
{
double result = 0d;
switch (operate)
{
case "+":
result = numberA + numberB;
break;
case "-":
result = numberA - numberB;
break;
case "*":
result = numberA * numberB;
break;
case "/":
result = numberA / numberB;
break;
}
return result;
}
}
code 4:(継承,マルチステートのOOP応用と実装Demo)
/// <summary>
///
/// </summary>
class Operation
{
private double _numberA = 0;
private double _numberB = 0;
/// <summary>
/// A
/// </summary>
public double NumberA
{
get { return _numberA; }
set { _numberA = value; }
}
/// <summary>
/// B
/// </summary>
public double NumberB
{
get { return _numberB; }
set { _numberB = value; }
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public virtual double GetResult()
{
double result = 0;
return result;
}
}
/// <summary>
///
/// </summary>
class OperationAdd : Operation
{
public override double GetResult()
{
double result = 0;
result = NumberA + NumberB;
return result;
}
}
/// <summary>
///
/// </summary>
class OperationSub : Operation
{
public override double GetResult()
{
double result = 0;
result = NumberA - NumberB;
return result;
}
}
/// <summary>
///
/// </summary>
class OperationMul : Operation
{
public override double GetResult()
{
double result = 0;
result = NumberA * NumberB;
return result;
}
}
/// <summary>
///
/// </summary>
class OperationDiv : Operation
{
public override double GetResult()
{
double result = 0;
if (NumberB == 0)
throw new Exception(" 0。");
result = NumberA / NumberB;
return result;
}
}
/// <summary>
///
/// </summary>
class OperationFactory
{
public static Operation createOperate(string operate)
{
Operation oper = null;
switch (operate)
{
case "+":
{
oper = new OperationAdd();
break;
}
case "-":
{
oper = new OperationSub();
break;
}
case "*":
{
oper = new OperationMul();
break;
}
case "/":
{
oper = new OperationDiv();
break;
}
}
return oper;
}
}
--
Operation oper;
oper = OperationFactory.createOperate("+");
oper.NumberA = 1;
oper.NumberB = 2;
double result = oper.GetResult();