zoukankan      html  css  js  c++  java
  • 面向对象中构造函数的小练习

                //写一个Ticket类,有一个距离属性(本属性只读,在构造方法中赋值),不能为负数
               //有一个价格属性,价格属性为只读,并且根据距离distance计算价格Price(1元/公里)
               //0--100公里   票价不打折
               //101-200公里  票价总额9.5折
               //201-300公里  票价总额9折
               //301公里以上  票价总额8折
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace 面向对象构造函数
    {        
           public class Ticket
        {      //写一个Ticket类,有一个距离属性(本属性只读,在构造方法中赋值),不能为负数
               //有一个价格属性,价格属性为只读,并且根据距离distance计算价格Price(1元/公里)
               //0--100公里   票价不打折
               //101-200公里  票价总额9.5折
               //201-300公里  票价总额9折
               //301公里以上  票价总额8折
               private double _distance;
               public double Distance
               {
                   get { return _distance; }//只读属性意味着只有get 没有set
               }
               public Ticket(double distance)
               {
                   if (distance < 0)
                   {
                       distance = 0;
                   }
                   this._distance = distance;
               }
               private double _price;
    
               public double Price
               {
                   get
                   {
                       if (_distance > 0 && _distance <= 100)
                       {
                           return _distance * 1.0;
                       }
                       else if (_distance > 101 && _distance < 200)
                       {
                           return _distance * 0.95;
                       }
                       else if (_distance > 201 && _distance < 300)
                       {
                           return _distance * 0.9;
                       }
                       else
                       {
                           return _distance * 0.8;
                       }
                   }
               }
               public void ShowTicket()
               {
                   Console.WriteLine("{0}公里需要{1}元",this.Distance,Price);
               
               }
    
    
    
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace 面向对象构造函数
    {
        class Program
        {
            static void Main(string[] args)
            {
                Ticket t = new Ticket(400);
                t.ShowTicket();
                Console.ReadLine();
            }
        }
    }
  • 相关阅读:
    javascript静态页面传值的三种方法分享
    分布式缓存HttpRuntime.cache应用到单点登陆中_优化登陆
    C# 委托的三种调用示例(同步调用 异步调用 异步回调)
    使用SQL Server视图的优缺点
    浅析深究什么是SOA?
    IIS启用GZip压缩
    BI
    ASP.NET运行原理
    力扣算法:N 皇后
    力扣算法:基本计算器Ⅱ
  • 原文地址:https://www.cnblogs.com/kangshuai/p/4679319.html
Copyright © 2011-2022 走看看