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();
            }
        }
    }
  • 相关阅读:
    python 适配器
    python 装饰器
    实测 《Tensorflow实例:利用LSTM预测股票每日最高价(二)》的结果
    TFRecord 存入图像和标签
    TFRecord 读取图像和标签
    CONDA常用命令
    sotfmax的通俗理解
    sigmoid的通俗理解
    查看日志,定位错误_常用的操作
    工作中Git实操详解_看完这篇直接上手!
  • 原文地址:https://www.cnblogs.com/kangshuai/p/4679319.html
Copyright © 2011-2022 走看看