- 请创建一个人类Person,该类包括姓名、年龄、性别字段,包括一个可以进行初始化的有参的构造函数。请创建一个学生类Student继承与Person类,该类还包含学号字段,包含可以设置和读取学号字段的属性、显示信息(displayInfo)的成员方法、以及可以进行初始化的有参的构造函数。在Main函数中实例化一个学生类的对象,并给其各个字段进行赋值。


using System;
namespace ConsoleApp2
{
internal class Program
{
private static void Main(string[] args)
{
Student zhangSan = new Student("张三", 18u, true, 10001u);
zhangSan.DisplayInfo();
Console.ReadKey();
}
}
internal abstract class Person
{
protected string name;
protected uint age;
protected bool gender;
protected Person(string name, uint age, bool gender)
{
this.name = name;
this.age = age;
this.gender = gender;
}
}
internal class Student : Person
{
private uint id;
public uint Id { get => id; set => id = value; }
public Student(string name, uint age, bool gender, uint id) : base(name, age, gender)
{
Id = id;
}
public void DisplayInfo()
{
Console.WriteLine(ToString());
}
public override string ToString()
{
string genderValue = gender ? "男" : "女";
return $"姓名:{name}{Environment.NewLine}"
+ $"年龄:{age}岁{Environment.NewLine}"
+ $"性别:{genderValue}{Environment.NewLine}"
+ $"学号:{id}";
}
}
}

<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp1"
Title="个人简历"
Height="400"
Width="400"
FontSize="18">
<Window.DataContext>
<local:MainWindowViewModel />
</Window.DataContext>
<StackPanel HorizontalAlignment="Center"
VerticalAlignment="Center">
<GroupBox Header="个人简历">
<StackPanel Orientation="Vertical">
<StackPanel.Resources>
<Style TargetType="{x:Type StackPanel}">
<Setter Property="Orientation" Value="Horizontal" />
<Setter Property="Margin" Value="10" />
</Style>
</StackPanel.Resources>
<StackPanel>
<TextBlock Text="姓名:" />
<TextBox Width="150"
Text="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>
<StackPanel>
<TextBlock Text="地址:" />
<TextBox Width="150"
TextWrapping="Wrap"
AcceptsReturn="True"
VerticalScrollBarVisibility="Visible"
Text="{Binding Address, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>
<StackPanel>
<TextBlock Text="职业:" />
<TextBox Width="150"
Text="{Binding Occupation, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>
<StackPanel>
<TextBlock Text="年龄:" />
<TextBox Width="150"
Text="{Binding Age, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>
<StackPanel>
<TextBlock Text="信息汇总:" />
<TextBox Width="115"
TextWrapping="Wrap"
AcceptsReturn="True"
VerticalScrollBarVisibility="Visible"
IsReadOnly="True"
Text="{Binding Summary, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>
</StackPanel>
</GroupBox>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.Resources>
<Style TargetType="{x:Type Button}">
<Setter Property="Margin" Value="10" />
</Style>
</Grid.Resources>
<Button Content="确定"
Command="{Binding OkCommand, Mode=OneWay}" />
<Button Grid.Column="1"
Content="结束"
Command="{Binding EndCommand, Mode=OneWay}" />
<Button Grid.Column="2"
Content="清空"
Command="{Binding ClearCommand, Mode=OneWay}" />
</Grid>
</StackPanel>
</Window>
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Input;
namespace WpfApp1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
internal class MainWindowViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private string name;
public string Name
{
get => name;
set { name = value; OnPropertyChanged(); }
}
private string address;
public string Address
{
get => address;
set { address = value; OnPropertyChanged(); }
}
private string occupation;
public string Occupation
{
get => occupation;
set { occupation = value; OnPropertyChanged(); }
}
private uint age;
public uint Age
{
get => age;
set
{
if (value < 0 || value > 120)
{
MessageBox.Show("年龄在0到120之间!", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
age = value;
OnPropertyChanged();
}
}
private string summary;
public string Summary
{
get => summary;
set { summary = value; OnPropertyChanged(); }
}
public RelayCommand OkCommand { get; }
public RelayCommand EndCommand { get; }
public RelayCommand ClearCommand { get; }
public MainWindowViewModel()
{
OkCommand = new RelayCommand(Ok);
EndCommand = new RelayCommand(End);
ClearCommand = new RelayCommand(Clear);
}
private void Clear(object obj)
{
Name = default;
Address = default;
Occupation = default;
Age = default;
Summary = default;
}
private void End(object obj)
{
Application.Current.Shutdown();
}
private void Ok(object obj)
{
if (string.IsNullOrEmpty(Name))
{
MessageBox.Show("姓名不能为空!", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
if (string.IsNullOrEmpty(Address))
{
MessageBox.Show("地址不能为空!", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
if (string.IsNullOrEmpty(Occupation))
{
MessageBox.Show("职业不能为空!", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
Summary = $"姓名为:{Name}{Environment.NewLine}" +
$"地址为:{Address}{Environment.NewLine}" +
$"职业为:{Occupation}{Environment.NewLine}" +
$"年龄为:{Age}{Environment.NewLine}";
}
}
internal class RelayCommand : ICommand
{
public Action<object> Command { get; }
public RelayCommand(Action<object> command)
{
Command = command;
}
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
Command?.Invoke(parameter);
}
}
}