Quantcast
Channel: C#タグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 9551

C# 勉強(1) class

$
0
0

クラス;

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);

    }
}

Viewing all articles
Browse latest Browse all 9551

Trending Articles