PRACTIQUEMOS

Si recuerdan el Juego de ajedrez del que hablamos en el capítulo 1 de Herencia, encontramos allí una forma de aplicar clases abstractas.

Diagrama de clases:

Vemos el siguiente WEBCAST

Código:

Clase Abstracta Pieza C#

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Silverlight_CPOO_02_HerenciaClaseAbstracta_02.Ajedrez
{
    public abstract class Pieza : Grid
    {
        protected Image ImagenPieza { get; set; }

        public Pieza()
        {
            ImagenPieza = new Image();
            ImagenPieza.Width = 50;
            ImagenPieza.Height = 50;
            this.Children.Add(ImagenPieza);
        }

        public abstract void Mover();

        public void Reiniciar()
        {
            this.SetValue(Canvas.TopProperty, 0.0);
            this.SetValue(Canvas.LeftProperty, 0.0);
        }
    }
}

Clase Caballo y Torre C#


using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Silverlight_CPOO_02_HerenciaClaseAbstracta_02.Ajedrez
{
    public class Caballo : Pieza
    {
        public Caballo()
        {
            ImagenPieza.Source = new System.Windows.Media.Imaging.BitmapImage(
                new Uri("Imagenes/CaballoNegro.png",
                    UriKind.RelativeOrAbsolute));
        }

        public override void Mover()
        {
            //////////////////////////////
            //    Marca un recorrido en L
            //
            //    //////
            //        //
            //
            //////////////////////////////
            this.SetValue(Canvas.LeftProperty, 150.0);
            this.SetValue(Canvas.TopProperty, 50.0);
        }
    }
}

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Silverlight_CPOO_02_HerenciaClaseAbstracta_02.Ajedrez
{
    public class Torre : Pieza
    {
        public Torre()
        {
            ImagenPieza.Source =
                new System.Windows.Media.Imaging.BitmapImage(
                    new Uri("Imagenes/TorreNegra.png",
                        UriKind.RelativeOrAbsolute));
        }

        public override void Mover()
        {
            this.SetValue(Canvas.LeftProperty, 150.0);
        }
    }
}

UserControl MainPage.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Silverlight_CPOO_02_HerenciaClaseAbstracta_02.Ajedrez;

namespace Silverlight_CPOO_02_HerenciaClaseAbstracta_02
{
    public partial class MainPage : UserControl
    {
        Pieza piezaCaballo;
        Pieza piezaTorre;
        public MainPage()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            piezaCaballo  = new Caballo();
            LayoutCaballo.Children.Add(piezaCaballo);

            piezaTorre = new Torre();
            LayoutTorre.Children.Add(piezaTorre);
        }

        private void BtCaballo_Click(object sender, RoutedEventArgs e)
        {
            piezaCaballo.Mover();
        }

        private void BtCaballo_R_Click(object sender, RoutedEventArgs e)
        {
            piezaCaballo.Reiniciar();
        }

        private void BtTorre_M_Click(object sender, RoutedEventArgs e)
        {
            piezaTorre.Mover();
        }

        private void BtTorre_R_Click(object sender, RoutedEventArgs e)
        {
            piezaTorre.Reiniciar();
        }
    }
}

UserControl XAML MainPage.xaml

<UserControl x:Class="Silverlight_CPOO_02_HerenciaClaseAbstracta_02.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="150*" />
            <RowDefinition Height="150*" />
        </Grid.RowDefinitions>
        <Canvas x:Name="LayoutCaballo" Background="White" Grid.Row="0">
            <Button Canvas.Left="280" Canvas.Top="12" Content=" Mover Caballo"
                    Height="22" Name="BtCaballo_M" Width="108" Click="BtCaballo_Click" />
            <Button Canvas.Left="282" Canvas.Top="48" Content="Reiniciar Caballo"
                    Height="23" Name="BtCaballo_R" Width="106" Click="BtCaballo_R_Click" />
        </Canvas>
        <Canvas x:Name="LayoutTorre" Background="White" Grid.Row="1">
            <Button Canvas.Left="280" Canvas.Top="12" Content=" Mover Torre"
                    Height="22" Name="BtTorre_M" Width="108" Click="BtTorre_M_Click" />
            <Button Canvas.Left="282" Canvas.Top="48" Content="Reiniciar Torre"
                    Height="23" Name="BtTorre_R" Width="106" Click="BtTorre_R_Click" />
        </Canvas>
    </Grid>
</UserControl>
Anuncios

Páginas: 1 2