练习题36

写一个Ticket类,有一个距离属性(本属性只读,在构造方法中赋值),不能为负数,有一个价格属性,价格属性只读,并且根据距离distance计算价格Price(1元/公里)
0-100公里 票价不打折
101-200公里 总额打9.5折
201-300公里 总额打9折
300公里以上 总额打8折

有一个方法,可以显示这张票的信息。测试上面的类。

Ticket类:

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

namespace 练习题36
{
    public class Ticket
    {
        private int _distance;
        public int Distance
        {
            get
            { return _distance;}
        }

        private double _price;
        public double Price
        {
            get
            {
                if (_distance >= 0 && _distance <= 100)
                    return _distance;
                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 Ticket(int distance)
        {
            if (distance < 0)
                distance = 0;
            this._distance = distance;
        }

        public void ShowTicket()
        {
            Console.WriteLine("{0}公里的钱是{1}元", Distance, Price);
        }

    }
}

main函数:

using System;

namespace 练习题36
{
    class Program
    {
        static void Main(string[] args)
        {
            Ticket t = new Ticket(150);
            t.ShowTicket();
            Console.ReadKey(); 
        }
    }
}

在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值