CAPTAIN AMAZING - THE DEATH OF THE OBJECT - structs, class, is
Head First C Sharp: A Learner’s Guide to Real-World Programming with C Sharp and .NET (Andrew Stellman(著)、Jennifer Greene(著)、O’Reilly Media)の Chapter 11(CAPTAIN AMAZING - THE DEATH OF THE OBJECT)、p.616(Pool Puzzle)の解答を求めてみる。
コード
Program.cs
using ConsoleApp1;
new Faucet();
Faucet.cs
namespace ConsoleApp1;
public class Faucet
{
public Faucet()
{
Table wine = new Table();
Hinge book = new Hinge();
wine.Set(book);
book.Set(wine);
wine.Lamp(10);
book.garden.Lamp("back in");
book.bulb *= 2;
wine.Lamp("minutes");
wine.Lamp(book);
}
}
Table.cs
using System;
namespace ConsoleApp1;
public struct Table
{
public string stairs;
public Hinge floor;
public void Set(Hinge b) => floor = b;
public void Lamp(object oil)
{
if (oil is int oilInt)
{
floor.bulb = oilInt;
}
else if (oil is string oilString)
{
stairs = oilString;
}
else if (oil is Hinge vine)
{
Console.WriteLine($"{vine.Table()} {floor.bulb} {stairs}");
}
}
}
Hinge.cs
namespace ConsoleApp1;
public class Hinge
{
public int bulb;
public Table garden;
public void Set(Table a) => garden = a;
public string Table() => garden.stairs;
}
入出力結果(Terminal, Zsh)
% dotnet run
back in 20 minutes
%