• C# Head.First 里面的例子 (中文名:深入浅出C#)


    //看了原来的例子,我真的是弄昏了。。结果还是按照自己写的。

    // DinnerParty.CS 源码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace DinnerPary.HeadFirst.Book
    {
        public class DinnterParty
        {
            public const int CostOfFoodPerson = 25;
            /// <summary>
            /// 人数,总共花多少钱,用了多少钱。
            /// </summary>
            public int NumberOfPeople;
            public decimal CostOfBeveragesPerson;
            public decimal CostOfDecorations;
            //Decoration 装饰;


            /// <summary>
            /// 如果选择了健康套餐,计算出单价是多少
            /// </summary>
            public void SetHealthyOption(bool IsHealthOption)
            {
                if (IsHealthOption)
                {
                    this.CostOfBeveragesPerson = 5.00M; //健康套餐每人5美元+5%折扣.

                }
                else
                {
                    this.CostOfBeveragesPerson = 20.00m;//酒品每人20元.
                }
     
            }
            /// <summary>
            /// 如果选择的是特色装饰。这里只计算了特色装饰.
            /// </summary>
            /// <param name="IsDecoration"></param>
            public void CalculateCostOfDecorations(bool IsDecoration)
            {
                if (IsDecoration)
                {
                    this.CostOfDecorations = (NumberOfPeople * 15.00M) + 50M;
                }
                else
                {
                    this.CostOfDecorations = (NumberOfPeople * 7.50M) + 30M;
                }
     
            }
            /// <summary>
            /// 如果选择的是健康套餐
            /// </summary>
            /// <param name="isHealthy"></param>
            /// <returns></returns>

            public  decimal CacluateCost(bool isHealthy)
            {
                decimal totalCost=this.CostOfDecorations+(CostOfBeveragesPerson+CostOfFoodPerson)*NumberOfPeople;
                if (isHealthy)
                {
                    return totalCost * 0.95M;

                }
                else
                {
                    return totalCost;
     
                }
            }

        }
    }

    ///然后是Form1  说明下,里面有两个checkBox 选择 Health 套餐,或者 Decoration  (健康套餐 或者 特色装饰) 一个NumbericUpDown 控件.还有个Label 用来显示用了多少钱。

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace DinnerPary.HeadFirst.Book  //看到171了
    {
        public partial class Form1 : Form
        {
            DinnterParty dinnerPatty;
            public Form1()
            {
                InitializeComponent();
                dinnerPatty=new DinnterParty(){NumberOfPeople=5};
                dinnerPatty.SetHealthyOption(false);
                dinnerPatty.CalculateCostOfDecorations(true);
                this.checkBoxHealthOption.Checked = false;
                this.checkBoxDecoration.Checked = true;
          
                DisplayDinnerCost();

            }

           /// <summary>
           /// 在Label标签上面显示出要花多少钱。
           /// </summary>
            private void DisplayDinnerCost()
            {
                decimal Cost = dinnerPatty.CacluateCost(checkBoxHealthOption.Checked);
                this.labelMoneyDispaly.Text= Cost.ToString("c");


            }
            /// <summary>
            /// numberircUpDown 值变化过后对人数的影响
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void numericUpDown1_ValueChanged(object sender, EventArgs e)
            {
                dinnerPatty.NumberOfPeople = (int)this.numericUpDown1.Value;
                DisplayDinnerCost();

            }

            /// <summary>
            /// 选择健康套餐
            ///
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void checkBoxHealthOption_CheckedChanged(object sender, EventArgs e)
            {
                DisplayDinnerCost();
               
            }
            /// <summary>
            /// 选择装饰品
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void checkBoxDecoration_CheckedChanged(object sender, EventArgs e)
            {
                dinnerPatty.CalculateCostOfDecorations(this.checkBoxDecoration.Checked);
               
                DisplayDinnerCost();
            }
        }
    }

    ////接下来是自己写的类。

    //DinnerParty.CS

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace DinnerParty
    {
        public class DinnerParty
        {
            private decimal totalCost;

            public decimal TotalCost
            {
                get { return totalCost; }
               
            }
            //private decimal Price = 0;
            private const int _固定Price = 25;
           
            public DinnerParty()
            {

            }
            public DinnerParty(int peopleOfNumber,bool isHealth,bool isDecoration)

            {
                this.totalCost = 0;

                if (isHealth)
                {
                    this.totalCost = (_固定Price + 5.00M) * peopleOfNumber;


                }
                else
                {
                    this.totalCost = (_固定Price + 20.00M) * peopleOfNumber;
                }


                if (isDecoration)
                {
                    this.totalCost += peopleOfNumber * 15.00M + 50;

                }
                else
                {
                    this.totalCost += peopleOfNumber * 0.75M + 30;
     
                }

                if (isHealth)
                {
                    totalCost = totalCost * 0.95M;
                }

               


            }
            public override string ToString()
            {
                return this.TotalCost.ToString("c");
            }
        
        }
    }

    ///Form1.CS

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace DinnerParty
    {
        public partial class Form1 : Form
        {
            DinnerParty dinner;
            public Form1()
            {
                InitializeComponent();
            }

            private void numericUpDown1_ValueChanged(object sender, EventArgs e)
            {
                _计算总额((int)numericUpDown1.Value, this.checkBoxHealthOption.Checked, this.checkBoxDecoration.Checked);


            }

            private void checkBoxHealthOption_CheckedChanged(object sender, EventArgs e)
            {
                _计算总额((int)numericUpDown1.Value, this.checkBoxHealthOption.Checked, this.checkBoxDecoration.Checked);
            }

            private void checkBoxDecoration_CheckedChanged(object sender, EventArgs e)
            {
                _计算总额((int)numericUpDown1.Value, this.checkBoxHealthOption.Checked, this.checkBoxDecoration.Checked);
            }

            private void _计算总额(int PeoleOfNumber, bool checkHealth, bool checkDecoration)
            {
                dinner = new DinnerParty(PeoleOfNumber, checkHealth, checkDecoration);
                this.labelMoneyDispaly.Text=dinner.ToString();
     
            }
        }
    }

  • 相关阅读:
    Kubernetes 部署Mysql 主从复制集群
    Kubernetes Kube-proxy
    vRO Extend VirtualDisk Workflow
    Kubernetes Horizontal Pod Autoscaler
    vRO 7 添加RestHost证书报错
    Kubernetes Resource Qoutas
    Kubernetes 命令补全
    Kubernetes 部署Weave Scope监控
    Kubernetes Yaml
    Go reflect反射
  • 原文地址:https://www.cnblogs.com/fat_li/p/1879468.html
Copyright © 2020-2023  润新知