怎么可以自己做网站被百度收到,wordpress编辑模板,wordpress怎么制作搜索框,酒店为什么做网站在面向对象编程中#xff0c;继承是一种机制#xff0c;允许一个类#xff08;称为子类或派生类#xff09;从另一个类#xff08;称为父类或基类#xff09;继承属性和方法。继承使我们能够创建一个通用类#xff0c;然后根据需要扩展或修改它以创建更具体的类。以下是…在面向对象编程中继承是一种机制允许一个类称为子类或派生类从另一个类称为父类或基类继承属性和方法。继承使我们能够创建一个通用类然后根据需要扩展或修改它以创建更具体的类。以下是一些关于父类和子类以及它们之间继承关系的基本概念和解释。
一、父类、子类及类的继承关系
1.父类基类
父类是一个通用的类它定义了一组属性和方法这些属性和方法可以被多个子类共享。父类通常是较抽象的表示一个更通用的概念。例如Animal动物类可以是一个父类表示所有动物的共同属性和行为。
2.子类派生类
子类是从父类派生出来的类。它继承了父类的所有属性和方法但也可以添加自己特有的属性和方法。子类表示一个更具体的概念。例如Cat猫类和Dog狗类可以是从Animal类派生出来的子类表示具体的动物种类。
3.继承关系
3.1 继承属性和方法
子类继承父类的所有非私有属性和方法。这意味着子类可以直接使用父类中的代码。子类可以覆盖父类的方法即方法重写以提供特定于子类的实现。
3.2 继承的语法
在C#中使用冒号:来表示继承。
下面是一个示例
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;}
}3.3 示例解释
在上面的示例中
Animal类是父类定义了三个属性Sound叫声、Age年龄和Color颜色。Cat类和Dog类是从Animal类继承的子类。它们各自有一个构造函数在创建对象时设置Sound属性的默认值。
3.4 使用继承的优点 代码重用: 继承允许子类重用父类中的代码减少了代码的重复提高了代码的可维护性。 扩展性: 可以通过继承扩展现有类而不需要修改原有的代码。这样系统更容易扩展和维护。 多态性: 继承是实现多态性的一种手段。通过继承可以使用父类的引用来指向子类的对象从而实现不同的行为。
二、代码实现
本次代码基于WPF代码实现观察现象
1.C#代码MainWindow.xaml.cs
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窗口的DataContextDataContext new { Cat cat, Dog dog, Sheep sheep };}}
}2.XAML代码MainWindow.xaml
Window x:ClassWpfApp.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlTitleAnimals Height300 Width300GridGrid.RowDefinitions!-- 定义三个行每个行用于显示一种动物的信息 --RowDefinition HeightAuto/RowDefinition HeightAuto/RowDefinition HeightAuto//Grid.RowDefinitions!-- 猫的边框 --Border Grid.Row0 BorderBrushBlack BorderThickness1 Margin5StackPanel!-- 显示动物种类名称 --TextBlock TextCat FontWeightBold HorizontalAlignmentCenter/!-- 绑定并显示猫的叫声 --TextBlock Text{Binding Cat.Sound} /!-- 绑定并显示猫的年龄 --TextBlock Text{Binding Cat.Age} /!-- 绑定并显示猫的颜色 --TextBlock Text{Binding Cat.Color} //StackPanel/Border!-- 狗的边框 --Border Grid.Row1 BorderBrushBlack BorderThickness1 Margin5StackPanel!-- 显示动物种类名称 --TextBlock TextDog FontWeightBold HorizontalAlignmentCenter/!-- 绑定并显示狗的叫声 --TextBlock Text{Binding Dog.Sound} /!-- 绑定并显示狗的年龄 --TextBlock Text{Binding Dog.Age} /!-- 绑定并显示狗的颜色 --TextBlock Text{Binding Dog.Color} //StackPanel/Border!-- 羊的边框 --Border Grid.Row2 BorderBrushBlack BorderThickness1 Margin5StackPanel!-- 显示动物种类名称 --TextBlock TextSheep FontWeightBold HorizontalAlignmentCenter/!-- 绑定并显示羊的叫声 --TextBlock Text{Binding Sheep.Sound} /!-- 绑定并显示羊的年龄 --TextBlock Text{Binding Sheep.Age} /!-- 绑定并显示羊的颜色 --TextBlock Text{Binding Sheep.Color} //StackPanel/Border/Grid
/Window3.代码解释 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 对象的属性值。
4.现象显示