2.用class 實作介面,並實現簡易的多型
3.最後在用介面去呈現結果。
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace 外幣兌換
- {
- interface Exchange //介面
- {
- double Rate { get; } //匯率
- void Convert(); //轉換
- }
- {
- public double Rate
- {
- get { return 29.96; }
- }
- public void Convert()
- {
- Console.Write("請輸入要兌換的美元 : ");
- double dollar = double.Parse(Console.ReadLine());
- double nt = dollar * Rate;
- Console.WriteLine("美元{0},可兌換台幣 {1}元", dollar, nt.ToString("#.#") );
- }
- }
- class JP_to_NT : Exchange
- {
- public double Rate
- {
- get { return 3.5868; }
- }
- public void Convert()
- {
- Console.Write("請輸入要兌換的日幣 : ");
- double dollar = double.Parse(Console.ReadLine());
- double nt = dollar / Rate;
- Console.WriteLine("日幣{0},可兌換台幣 {1}元", dollar, nt.ToString("#.#"));
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Exchange exchange;
- US_to_NT USD = new US_to_NT();
- JP_to_NT JPD = new JP_to_NT();
- while (true)
- {
- Console.Write("選擇要兌換的外幣 : 1.美元 2.日幣 3.QUIT : ");
- int option = int.Parse(Console.ReadLine());
- if (option == 1)
- {
- exchange = USD; //衍生物件指定給介面 類似向上轉型ok
- }
- else if (option == 2)
- {
- exchange = JPD;
- }
- else
- break;
- exchange.Convert();
- Console.WriteLine();
- }
- }
- }
- }
沒有留言:
張貼留言