标题: 为什么在Ticket类中将距离设为只读,还可以在主函数中输... [打印本页] 作者: 邢秀兰 时间: 2012-11-18 07:45 标题: 为什么在Ticket类中将距离设为只读,还可以在主函数中输... using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 构造方法
{
class Ticket
{
public Ticket(int distance)
{
try
{
if (distance < 0)
{
throw new Exception("距离不能为负数");
}
else
{
this.distance = distance;
}
}
catch
{
}
}
int distance;
public int Distance
{
get { return distance; }
}
//decimal price;
public double Price
{
get
{
if (distance >= 300)
{
return 1.0 * distance * 0.8;
}
else if (distance > 200)
{
return 1.0 * distance * 0.9;
}
else if (distance >= 100)
{
return 1.0 * distance * 0.95;
}
else
{
return 1.0 * distance;
}
}
}
public void ShowPrice()
{
Console.WriteLine("距离为{0}的票,票价为{1}", distance, Price);
}
}
}
主函数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 构造方法
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入行驶的距离90");