1、代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace travel
{
public class Travel
{
private int tourCost =0;
private string location;
public int TourPrice
{
get
{
if ( location == "加利福尼亞")
return tourCost = 250;
if (location == "北京")
return tourCost = 190;
else
return tourCost = 0;
}
}
public string Location
{
get
{
return location;
}
set
{
if (value != "加利福尼亞" && value != "北京")
{
location = null;
}
else
location = value;
}
}
}
class Program
{
static void Main(string[] args)
{
Travel dizhi=new Travel();
Console.WriteLine("請輸入你要去旅遊的地方");
dizhi.Location=Console.ReadLine();
Console.WriteLine("費用是" + dizhi.TourPrice);
}
}
}
2、實驗結果截圖: