当前位置:   article > 正文

WPF学习(1)--类与类的继承

WPF学习(1)--类与类的继承

面向对象编程中,继承是一种机制,允许一个类(称为子类或派生类)从另一个类(称为父类或基类)继承属性和方法。继承使我们能够创建一个通用类,然后根据需要扩展或修改它以创建更具体的类。以下是一些关于父类和子类以及它们之间继承关系的基本概念和解释。

一、父类、子类及类的继承关系

1.父类(基类)

  • 父类是一个通用的类,它定义了一组属性和方法,这些属性和方法可以被多个子类共享。
  • 父类通常是较抽象的,表示一个更通用的概念。例如,Animal(动物)类可以是一个父类,表示所有动物的共同属性和行为。

2.子类(派生类)

  • 子类是从父类派生出来的类。它继承了父类的所有属性和方法,但也可以添加自己特有的属性和方法。
  • 子类表示一个更具体的概念。例如,Cat(猫)类和Dog(狗)类可以是从Animal类派生出来的子类,表示具体的动物种类。

3.继承关系

3.1 继承属性和方法

  • 子类继承父类的所有非私有属性和方法。这意味着子类可以直接使用父类中的代码。
  • 子类可以覆盖父类的方法(即方法重写),以提供特定于子类的实现。

3.2 继承的语法

  • 在C#中,使用冒号:来表示继承。

下面是一个示例:

  1. public class Animal
  2. {
  3. public string Sound { get; set; }
  4. public int Age { get; set; }
  5. public string Color { get; set; }
  6. }
  7. public class Cat : Animal
  8. {
  9. public Cat()
  10. {
  11. Sound = "Meow";
  12. }
  13. }
  14. public class Dog : Animal
  15. {
  16. public Dog()
  17. {
  18. Sound = "Woof";
  19. }
  20. }

3.3 示例解释

在上面的示例中:

  • Animal类是父类,定义了三个属性:Sound(叫声)、Age(年龄)和Color(颜色)。
  • Cat类和Dog类是从Animal类继承的子类。它们各自有一个构造函数,在创建对象时设置Sound属性的默认值。

3.4 使用继承的优点

  1. 代码重用:

    • 继承允许子类重用父类中的代码,减少了代码的重复,提高了代码的可维护性。
  2. 扩展性:

    • 可以通过继承扩展现有类,而不需要修改原有的代码。这样,系统更容易扩展和维护。
  3. 多态性:

    • 继承是实现多态性的一种手段。通过继承,可以使用父类的引用来指向子类的对象,从而实现不同的行为。

二、代码实现

本次代码基于WPF代码实现观察现象

1.C#代码:MainWindow.xaml.cs

  1. using System;
  2. using System.ComponentModel;
  3. using System.Windows;
  4. namespace WpfApp
  5. {
  6. // 定义动物基类
  7. public class Animal : INotifyPropertyChanged
  8. {
  9. private string sound; // 动物的叫声
  10. private int age; // 动物的年龄
  11. private string color; // 动物的颜色
  12. // 动物的叫声属性
  13. public string Sound
  14. {
  15. get { return sound; }
  16. set
  17. {
  18. sound = value;
  19. OnPropertyChanged("Sound");
  20. }
  21. }
  22. // 动物的年龄属性
  23. public int Age
  24. {
  25. get { return age; }
  26. set
  27. {
  28. age = value;
  29. OnPropertyChanged("Age");
  30. }
  31. }
  32. // 动物的颜色属性
  33. public string Color
  34. {
  35. get { return color; }
  36. set
  37. {
  38. color = value;
  39. OnPropertyChanged("Color");
  40. }
  41. }
  42. // 实现INotifyPropertyChanged接口,用于属性改变通知
  43. public event PropertyChangedEventHandler PropertyChanged;
  44. protected virtual void OnPropertyChanged(string propertyName)
  45. {
  46. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  47. }
  48. }
  49. // 定义猫类,继承自动物类
  50. public class Cat : Animal
  51. {
  52. public Cat()
  53. {
  54. Sound = "Meow"; // 初始化猫的叫声
  55. }
  56. }
  57. // 定义狗类,继承自动物类
  58. public class Dog : Animal
  59. {
  60. public Dog()
  61. {
  62. Sound = "Woof"; // 初始化狗的叫声
  63. }
  64. }
  65. // 定义羊类,继承自动物类
  66. public class Sheep : Animal
  67. {
  68. public Sheep()
  69. {
  70. Sound = "Baa"; // 初始化羊的叫声
  71. }
  72. }
  73. // WPF窗口的后台代码
  74. public partial class MainWindow : Window
  75. {
  76. public MainWindow()
  77. {
  78. InitializeComponent();
  79. // 创建动物对象,并设置它们的属性
  80. Animal cat = new Cat { Age = 3, Color = "Gray" };
  81. Animal dog = new Dog { Age = 5, Color = "Brown" };
  82. Animal sheep = new Sheep { Age = 2, Color = "White" };
  83. // 将动物对象绑定到WPF窗口的DataContext
  84. DataContext = new { Cat = cat, Dog = dog, Sheep = sheep };
  85. }
  86. }
  87. }

2.XAML代码:MainWindow.xaml

  1. <Window x:Class="WpfApp.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. Title="Animals" Height="300" Width="300">
  5. <Grid>
  6. <Grid.RowDefinitions>
  7. <!-- 定义三个行,每个行用于显示一种动物的信息 -->
  8. <RowDefinition Height="Auto"/>
  9. <RowDefinition Height="Auto"/>
  10. <RowDefinition Height="Auto"/>
  11. </Grid.RowDefinitions>
  12. <!-- 猫的边框 -->
  13. <Border Grid.Row="0" BorderBrush="Black" BorderThickness="1" Margin="5">
  14. <StackPanel>
  15. <!-- 显示动物种类名称 -->
  16. <TextBlock Text="Cat" FontWeight="Bold" HorizontalAlignment="Center"/>
  17. <!-- 绑定并显示猫的叫声 -->
  18. <TextBlock Text="{Binding Cat.Sound}" />
  19. <!-- 绑定并显示猫的年龄 -->
  20. <TextBlock Text="{Binding Cat.Age}" />
  21. <!-- 绑定并显示猫的颜色 -->
  22. <TextBlock Text="{Binding Cat.Color}" />
  23. </StackPanel>
  24. </Border>
  25. <!-- 狗的边框 -->
  26. <Border Grid.Row="1" BorderBrush="Black" BorderThickness="1" Margin="5">
  27. <StackPanel>
  28. <!-- 显示动物种类名称 -->
  29. <TextBlock Text="Dog" FontWeight="Bold" HorizontalAlignment="Center"/>
  30. <!-- 绑定并显示狗的叫声 -->
  31. <TextBlock Text="{Binding Dog.Sound}" />
  32. <!-- 绑定并显示狗的年龄 -->
  33. <TextBlock Text="{Binding Dog.Age}" />
  34. <!-- 绑定并显示狗的颜色 -->
  35. <TextBlock Text="{Binding Dog.Color}" />
  36. </StackPanel>
  37. </Border>
  38. <!-- 羊的边框 -->
  39. <Border Grid.Row="2" BorderBrush="Black" BorderThickness="1" Margin="5">
  40. <StackPanel>
  41. <!-- 显示动物种类名称 -->
  42. <TextBlock Text="Sheep" FontWeight="Bold" HorizontalAlignment="Center"/>
  43. <!-- 绑定并显示羊的叫声 -->
  44. <TextBlock Text="{Binding Sheep.Sound}" />
  45. <!-- 绑定并显示羊的年龄 -->
  46. <TextBlock Text="{Binding Sheep.Age}" />
  47. <!-- 绑定并显示羊的颜色 -->
  48. <TextBlock Text="{Binding Sheep.Color}" />
  49. </StackPanel>
  50. </Border>
  51. </Grid>
  52. </Window>

3.代码解释

  1. C#代码:

    • Animal 类是一个基类,定义了三个属性:Sound(叫声)、Age(年龄)、Color(颜色)。这些属性通过INotifyPropertyChanged接口来通知属性变化。
    • CatDogSheep 类是从 Animal 类继承的子类。它们在构造函数中设置了各自的默认叫声。
    • MainWindow 类是WPF窗口的后台代码。在构造函数中,创建了 CatDogSheep 对象,并设置它们的属性。然后,将这些对象绑定到窗口的 DataContext
  2. XAML代码:

    • 定义了一个 Grid 布局,其中包含三个行,每行用于显示一种动物的信息。
    • 每种动物的信息显示在一个 Border 中, Border 内部是一个 StackPanel
    • StackPanel 包含一个显示动物种类名称的 TextBlock 和三个绑定到动物属性的 TextBlock
    • 通过绑定 (Binding),TextBlock 显示来自 DataContextCatDogSheep 对象的属性值。

4.现象显示

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/745587
推荐阅读
相关标签
  

闽ICP备14008679号