• 属性,构造函数的基本应用


    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();
    
            }
        }
    }
    

      

  • 相关阅读:
    项目选题报告答辩总结
    项目UML设计(团队)
    项目选题报告答辩总结
    第七次作业
    结对第二次
    第四次作业
    alpha冲刺4
    alpha冲刺3
    alpha冲刺2
    alpha冲刺1
  • 原文地址:https://www.cnblogs.com/my-cat/p/7234861.html
Copyright © 2020-2023  润新知