商品クラスの利用
クラス;
public class Product {
//プロパティ(set は非公開)
public int Code {get;private set;}
public string Name{get;private set;}
public int Price{get;private set;}
//コンストラクタ(特殊メソッド)
public Product(int code,string name,int price){
this.Code=code;
this.Name=name;
this.Price=price;
}
//税金メソッド
public int GetTax(){
return (int)(Price * 0.08);
}
public int GetPriceIncludingTax(){
return Price + GetTax();
}
}
プログラム;
class Program
{
static void Main(string[] args)
{
Product choco = new Product(123,"チョコチップ",110);
Product boltgum = new Product(234,"ボトルガム",500);
int chocoTax = choco.GetTax();
int boltgumTax = boltgum.GetTax();
Console.WriteLine("{0},{1},{2}",choco.Name,choco.Price,chocoTax);
Console.WriteLine("{0},{1},{2}",boltgum.Name,boltgum.Price,boltgumTax);
}
}