赞
踩
在面向对象编程中,继承是一种机制,允许一个类(称为子类或派生类)从另一个类(称为父类或基类)继承属性和方法。继承使我们能够创建一个通用类,然后根据需要扩展或修改它以创建更具体的类。以下是一些关于父类和子类以及它们之间继承关系的基本概念和解释。
Animal
(动物)类可以是一个父类,表示所有动物的共同属性和行为。Cat
(猫)类和Dog
(狗)类可以是从Animal
类派生出来的子类,表示具体的动物种类。:
来表示继承。下面是一个示例:
- public class Animal
- {
- public string Sound { get; set; }
- public int Age { get; set; }
- public string Color { get; set; }
- }
-
- public class Cat : Animal
- {
- public Cat()
- {
- Sound = "Meow";
- }
- }
-
- public class Dog : Animal
- {
- public Dog()
- {
- Sound = "Woof";
- }
- }
在上面的示例中:
Animal
类是父类,定义了三个属性:Sound
(叫声)、Age
(年龄)和Color
(颜色)。Cat
类和Dog
类是从Animal
类继承的子类。它们各自有一个构造函数,在创建对象时设置Sound
属性的默认值。代码重用:
扩展性:
多态性:
本次代码基于WPF代码实现观察现象
- using System;
- using System.ComponentModel;
- using System.Windows;
-
- namespace WpfApp
- {
- // 定义动物基类
- public class Animal : INotifyPropertyChanged
- {
- private string sound; // 动物的叫声
- private int age; // 动物的年龄
- private string color; // 动物的颜色
-
- // 动物的叫声属性
- public string Sound
- {
- get { return sound; }
- set
- {
- sound = value;
- OnPropertyChanged("Sound");
- }
- }
-
- // 动物的年龄属性
- public int Age
- {
- get { return age; }
- set
- {
- age = value;
- OnPropertyChanged("Age");
- }
- }
-
- // 动物的颜色属性
- public string Color
- {
- get { return color; }
- set
- {
- color = value;
- OnPropertyChanged("Color");
- }
- }
-
- // 实现INotifyPropertyChanged接口,用于属性改变通知
- public event PropertyChangedEventHandler PropertyChanged;
-
- protected virtual void OnPropertyChanged(string propertyName)
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
- }
- }
-
- // 定义猫类,继承自动物类
- public class Cat : Animal
- {
- public Cat()
- {
- Sound = "Meow"; // 初始化猫的叫声
- }
- }
-
- // 定义狗类,继承自动物类
- public class Dog : Animal
- {
- public Dog()
- {
- Sound = "Woof"; // 初始化狗的叫声
- }
- }
-
- // 定义羊类,继承自动物类
- public class Sheep : Animal
- {
- public Sheep()
- {
- Sound = "Baa"; // 初始化羊的叫声
- }
- }
-
- // WPF窗口的后台代码
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
-
- // 创建动物对象,并设置它们的属性
- Animal cat = new Cat { Age = 3, Color = "Gray" };
- Animal dog = new Dog { Age = 5, Color = "Brown" };
- Animal sheep = new Sheep { Age = 2, Color = "White" };
-
- // 将动物对象绑定到WPF窗口的DataContext
- DataContext = new { Cat = cat, Dog = dog, Sheep = sheep };
- }
- }
- }
- <Window x:Class="WpfApp.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Title="Animals" Height="300" Width="300">
- <Grid>
- <Grid.RowDefinitions>
- <!-- 定义三个行,每个行用于显示一种动物的信息 -->
- <RowDefinition Height="Auto"/>
- <RowDefinition Height="Auto"/>
- <RowDefinition Height="Auto"/>
- </Grid.RowDefinitions>
-
- <!-- 猫的边框 -->
- <Border Grid.Row="0" BorderBrush="Black" BorderThickness="1" Margin="5">
- <StackPanel>
- <!-- 显示动物种类名称 -->
- <TextBlock Text="Cat" FontWeight="Bold" HorizontalAlignment="Center"/>
- <!-- 绑定并显示猫的叫声 -->
- <TextBlock Text="{Binding Cat.Sound}" />
- <!-- 绑定并显示猫的年龄 -->
- <TextBlock Text="{Binding Cat.Age}" />
- <!-- 绑定并显示猫的颜色 -->
- <TextBlock Text="{Binding Cat.Color}" />
- </StackPanel>
- </Border>
-
- <!-- 狗的边框 -->
- <Border Grid.Row="1" BorderBrush="Black" BorderThickness="1" Margin="5">
- <StackPanel>
- <!-- 显示动物种类名称 -->
- <TextBlock Text="Dog" FontWeight="Bold" HorizontalAlignment="Center"/>
- <!-- 绑定并显示狗的叫声 -->
- <TextBlock Text="{Binding Dog.Sound}" />
- <!-- 绑定并显示狗的年龄 -->
- <TextBlock Text="{Binding Dog.Age}" />
- <!-- 绑定并显示狗的颜色 -->
- <TextBlock Text="{Binding Dog.Color}" />
- </StackPanel>
- </Border>
-
- <!-- 羊的边框 -->
- <Border Grid.Row="2" BorderBrush="Black" BorderThickness="1" Margin="5">
- <StackPanel>
- <!-- 显示动物种类名称 -->
- <TextBlock Text="Sheep" FontWeight="Bold" HorizontalAlignment="Center"/>
- <!-- 绑定并显示羊的叫声 -->
- <TextBlock Text="{Binding Sheep.Sound}" />
- <!-- 绑定并显示羊的年龄 -->
- <TextBlock Text="{Binding Sheep.Age}" />
- <!-- 绑定并显示羊的颜色 -->
- <TextBlock Text="{Binding Sheep.Color}" />
- </StackPanel>
- </Border>
- </Grid>
- </Window>
C#代码:
Animal
类是一个基类,定义了三个属性:Sound
(叫声)、Age
(年龄)、Color
(颜色)。这些属性通过INotifyPropertyChanged
接口来通知属性变化。Cat
、Dog
和 Sheep
类是从 Animal
类继承的子类。它们在构造函数中设置了各自的默认叫声。MainWindow
类是WPF窗口的后台代码。在构造函数中,创建了 Cat
、Dog
和 Sheep
对象,并设置它们的属性。然后,将这些对象绑定到窗口的 DataContext
。XAML代码:
Grid
布局,其中包含三个行,每行用于显示一种动物的信息。Border
中, Border
内部是一个 StackPanel
。StackPanel
包含一个显示动物种类名称的 TextBlock
和三个绑定到动物属性的 TextBlock
。Binding
),TextBlock
显示来自 DataContext
中 Cat
、Dog
和 Sheep
对象的属性值。Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。