開発環境
- Microsoft Windows 8 Pro 64bit 日本語 (OS)
- Microsoft Visual Studio Express 2012 for Windows Desktop (IDE)
- プログラミング言語: C#
『初めてのC# 第2版』(Jesse Liberty+Brian MacDonald著、日向俊二訳、オライリー・ジャパン、2006年、ISBN978-487311-294-7)の 第8章(メソッド)8.5(練習問題)問題8-2を解いてみる。
その他参考書籍
問題8-2.
コード
using System; class Tester { public void DoublerAndTripler(int a, ref int b, ref int c) { b = 2 * a; c = 3 * a; } public void Run() { int a = 10; int b = 0; int c = 0; DoublerAndTripler(a, ref b, ref c); Console.WriteLine("a = {0}\na * 2 = {1}\na * 3 = {2}", a, b, c); } static void Main() { Tester t = new Tester(); t.Run(); } }
入出力結果(Console Window)
a = 10 a * 2 = 20 a * 3 = 30 続行するには何かキーを押してください . . .
ちなみにJavaScriptの場合。
コード(BBEdit)
// 参照型のオブジェクト型を使ってみる事に function doubler_and_tripler(a, o){ o.b = 2 * a; o.c = 3 * a; } var a = 10; var o = new Object(); doubler_and_tripler(a, o); var result = "a = " + a + "\n" + "a * 2 = " + o.b + "\n" + "a * 3 = " + o.c + "\n"; $('#pre0').text(result);
pythonの場合。
sample.py
コード(BBEdit)
#!/usr/bin/env python3.3 # -*- coding: utf-8 -*- def doubler_and_tripler(a, l): # 参照型のリストを利用してみることに l[0] = 2 * a l[1] = 3 * a a = 10 l = [0,0] doubler_and_tripler(a,l) print("a = {0}\n2 * a = {1}\n3 * a = {2}".format(a, l[0], l[1]))
入出力結果(Terminal)
$ ./sample.py a = 10 2 * a = 20 3 * a = 30 $
0 コメント:
コメントを投稿