Menu
CoddyTech

抽象クラス

Coddyの「Swift開発者向けC#」コースのレッスン 39/48。

Swiftには抽象クラスのキーワードがありません。プロトコルは、抽象クラスのストレージ継承を伴わずに必須の振る舞いを規定します。C#の抽象クラスは、フィールド、具象メソッド、抽象メンバーを組み合わせられます。具象実装を試すには、基本契約を使用します。

抽象クラスは共通の構造を定義し、派生クラスに振る舞いを要求できます。直接インスタンス化することはできません。抽象メソッドは基底クラスでは本体を持たず、派生した具象クラスでオーバーライドする必要があります。多態的なコレクションを使えば、呼び出し側は具象クラスの型を一つずつ確認せずに面積を求められます。

Swiftの参考例

import Foundation
protocol Shape{func area()->Double}
struct Triangle:Shape{let base:Double;let height:Double;func area()->Double{base*height/2}}
let shape:any Shape=Triangle(base:3,height:2)
print(String(format:"%.1f",shape.area()))

C#の補助宣言

abstract class Named { public abstract string Describe(); }
class NamedCode : Named { private string code; public NamedCode(string code) { this.code = code; } public override string Describe() { return "code:" + code; } }

Main内のC#の文

Named item = new NamedCode("A");
Console.WriteLine(item.Describe());

これらは学習に焦点を当てた抜粋です。課題ファイルには実行可能な完全なラッパーが含まれており、上記のスニペットには関連する宣言と文が示されています。

参考出力:

3.0

C#の出力:

code:A
challenge icon

チャレンジ

簡単

抽象クラスShapeを継承するRectangleとTriangleを実装してください。Rectangleの面積はwidth * heightです。Triangleの面積はbase * altitude / 2.0です。Shapeの参照を通じて合計を返してください。各寸法は0以上の整数です。

ロックされていないC#ファイルを編集してください。表示されているロック済みのMain.csは説明された行を読み込み、Exercise.Solveを呼び出し、入力が変更されていないことを検証して、最後に改行を1つ付けて結果を出力します。宣言されているAPIを維持してください。2つ目のMainを追加したり、診断用の出力を表示したりしないでください。

ロックされたランチャーは、Solveラッパーだけでなく、エクスポートされたヘルパーまたはクラスのAPIも個別に検査します。

自分で試してみよう

using System;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;
public class Program {
static int I(string text) { return int.Parse(text,CultureInfo.InvariantCulture); }
static int[] Ints(string text) { return text.Length==0 ? new int[0] : text.Split(',').Select(I).ToArray(); }
static string[] Words(string text) { return text.Length==0 ? new string[0] : text.Split(','); }
static string Snapshot(object value) { if (value==null) return "<null>"; if (value is string) return (string)value; var sequence=value as System.Collections.IEnumerable; if (sequence!=null) { var items=new List<string>(); foreach (object item in sequence) items.Add(Snapshot(item)); return string.Join("|",items); } return Convert.ToString(value,CultureInfo.InvariantCulture); }
public static void Main() {
System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
var rows = new List<string>(); string line; while ((line=Console.ReadLine()) != null) rows.Add(line);
string[] input=rows.ToArray();
var a0 = I(input[0]); string before0=Snapshot(a0);
var a1 = I(input[1]); string before1=Snapshot(a1);
var a2 = I(input[2]); string before2=Snapshot(a2);
var a3 = I(input[3]); string before3=Snapshot(a3);
Shape sh1 = new Rectangle(2,3); Shape sh2 = new Triangle(1,1); if (sh1.Area() != 6 || sh2.Area() != 0.5) throw new Exception("Shape API");
var result = Exercise.Solve(a0,a1,a2,a3);
if (Snapshot(a0) != before0) throw new Exception("Input mutation");
if (Snapshot(a1) != before1) throw new Exception("Input mutation");
if (Snapshot(a2) != before2) throw new Exception("Input mutation");
if (Snapshot(a3) != before3) throw new Exception("Input mutation");
Console.WriteLine(result.ToString("0.0", CultureInfo.InvariantCulture));
}
}
quiz icon腕試し

このレッスンには短いクイズがあります。レッスンを始めて解答し、進捗を記録しましょう。

Swift開発者向けC#のすべてのレッスン

自分で練習してみよう: C#オンラインコンパイラ