開発環境
- OS: Windows 10 Pro
- IDE(統合開発環境): Visual Studio Community 2017
Head First C# ―頭とからだで覚えるC#の基本 (Andrew Stellman (著)、Jennifer Greene (著)、佐藤 嘉一 (監修, 監修)、木下 哲也 (翻訳)、オライリージャパン)の5章(継承 - オブジェクトの系図)、エクササイズ(p. 198)を取り組んでみる。
コード
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:App1"
x:Class="App1.MainPage">
<StackLayout>
<!-- Place new controls here -->
<!--
<Label Text="Welcome to Xamarin.Forms!"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand" />
-->
</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 App1
{
public partial class MainPage
{
public MainPage()
{
InitializeComponent();
A a = new A();
B b = new B();
C c = new C();
A a2 = new C();
string q = "";
q += b.m1();
q += c.m2();
q += a.m3();
DisplayAlert(
"",
(q == "Bのm1, Aのm2, Aのm3, ").ToString(),
"cancel");
q = "";
q += c.m1();
q += c.m2();
q += c.m3();
DisplayAlert(
"",
(q == "Bのm1, Aのm2, Cのm3, 13").ToString(),
"cancel");
q = "";
q += a.m1();
q += b.m2();
q += c.m3();
DisplayAlert(
"",
(q == "Aのm1, Aのm2, Cのm3, 13").ToString(),
"canel");
q = "";
q += a2.m1();
q += a2.m2();
q += a2.m3();
DisplayAlert(
"",
(q == "Bのm1, Aのm2, Cのm3, 13").ToString(),
"cancel");
}
}
public class A
{
public int ivar = 7;
public virtual string m1()
{
return "Aのm1, ";
}
public string m2()
{
return "Aのm2, ";
}
public virtual string m3()
{
return "Aのm3, ";
}
}
public class B : A
{
public override string m1()
{
return "Bのm1, ";
}
}
public class C:B
{
public override string m3()
{
return "Cのm3, " + (ivar + 6);
}
}
}
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 コメント:
コメントを投稿