zoukankan      html  css  js  c++  java
  • 属性,构造函数的基本应用

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Test2
    {
        class Ticket
        {
            decimal distance;
    
            public decimal Price
            {
                get 
                {
                    if (distance > 0 && distance < 10)
                        return 1.0m * distance;
                    else if (distance >= 10 && distance < 20)
                        return 0.9m * distance;
                    else
                        return 0.8m * distance;
                }
            }
    
            public decimal Distance
            {
                get { return distance; }
                set 
                { 
                    if(distance <0)
                    {
                        throw new Exception("距离不能为负数");
                    }
                   
                    distance = value;
                }
            }
    
            public void ShowPrice()
            {
                Console.WriteLine("距离:{0}  票价:{1}", distance, Price);
            }
    
            public Ticket(decimal distance)
            {
                this.distance = distance;
            }
            public Ticket()
            {
    
            }
        }
    }
    

      

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Test2
    {
        class Program
        {
            static void Main(string[] args)
            {
                Ticket t1 = new Ticket(9);
                t1.ShowPrice();
    
                Ticket t2 = new Ticket();
                t2.Distance = 10;
    
                t2.ShowPrice();
    
                Console.ReadKey();
    
            }
        }
    }
    

      

  • 相关阅读:
    第一阶段-意见评论
    团队冲刺第15天
    团队冲刺第14天
    第十三周进度报告
    团队冲刺第13天
    团队冲刺第12天
    团队冲刺第11天
    SCRUM第二阶段第九天
    SCRUM第二阶段第八天
    SCRUM第二阶段第七天
  • 原文地址:https://www.cnblogs.com/my-cat/p/7234861.html
Copyright © 2011-2022 走看看