開発環境
- OS: Windows 10 Pro
- IDE(統合開発環境): Visual Studio Community 2017
Head First C# ―頭とからだで覚えるC#の基本 (Andrew Stellman (著)、Jennifer Greene (著)、佐藤 嘉一 (監修, 監修)、木下 哲也 (翻訳)、オライリージャパン)の6章(インタフェースと抽象クラス - クラスに約束を守らせる)、エクササイズ(p. 241)を取り組んでみる。
コード
MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:App2"
x:Class="App2.MainPage">
<StackLayout>
<Button x:Name="button1"
Text="button1"
Clicked="button1_Clicked"/>
</StackLayout>
</ContentPage>
MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace App2
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private void button1_Clicked(object sender, EventArgs e)
{
ScaryScary fingersTheClown = new ScaryScary("大きな靴", 32);
FunnyFunny someFunnyClown = fingersTheClown;
IScaryClown someOtherScarClown = someFunnyClown as ScaryScary;
if (someOtherScarClown != null)
{
someOtherScarClown.Honk();
}
}
}
public interface IClown
{
string FunnyThingIHave { get; }
void Honk();
}
public interface IScaryClown: IClown
{
string ScaryThingHave { get; }
void ScareLittleChildren();
}
public class FunnyFunny : ContentPage, IClown
{
private string funnyThingIHave;
public FunnyFunny(string funnyThingIHave)
{
this.funnyThingIHave = funnyThingIHave;
}
public string FunnyThingIHave => funnyThingIHave;
public void Honk()
{
DisplayAlert("", "プップー!私は" + funnyThingIHave, "cancel");
}
}
public class ScaryScary : FunnyFunny, IScaryClown
{
private int numberOfScaryThings;
public ScaryScary(string funnyThingIHave, int numberOfScaryThings):
base(funnyThingIHave)
{
this.numberOfScaryThings = numberOfScaryThings;
}
public string ScaryThingHave => numberOfScaryThings + "匹のクモを持っています";
public void ScareLittleChildren()
{
DisplayAlert("", "ブー!残念!", "canel");
}
}
}
App.xaml
<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="App1.App">
<Application.Resources>
</Application.Resources>
</Application>
App.xaml.cs
using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
[assembly: XamlCompilation(XamlCompilationOptions.Compile)]
namespace App1
{
public partial class App : Application
{
public App()
{
InitializeComponent();
// MainPage = new MainPage();
MainPage = new NavigationPage(
new MainPage() { Title = "" });
}
protected override void OnStart()
{
// Handle when your app starts
}
protected override void OnSleep()
{
// Handle when your app sleeps
}
protected override void OnResume()
{
// Handle when your app resumes
}
}
}
0 コメント:
コメントを投稿