目录
一、涉及到的知识点
1.泛型类的继承
2.泛型类的可视化
(1)用UML图说明泛型类的可视化
(2)用实例说明泛型类的可视化
二、实例1
1.源码
2.生成效果
三、实例2
再发一个继承泛型类的应用案例。
一、涉及到的知识点
1.泛型类的继承
泛型类的继承是指一个泛型类(也称为参数化类)可以被另一个泛型类继承。这允许创建一个具有共同特征的类层次结构,同时保持类型安全和避免运行时错误。
泛型类的继承与普通类的继承是类似的,只是在继承时多了一个类型参数T。语法格式如下:
class DerivedClass <类型参数T>:BaseClass <类型参数T>
2.泛型类的可视化
泛型类型也可以可视化。
泛型类的可视化是一种以图形形式表示泛型类及其与其他类之间关系的方法。这可以帮助人们更好地理解代码的工作原理并更容易地维护和改进它。
泛型类的可视化指的是以图形形式展示泛型类的结构、属性和关系。这可以帮助人们更好地理解泛型类的工作原理及其与其他类之间的关系。可视化可以使用诸如UML(统一建模语言)图、代码示例或伪代码等形式来表示。
在UML图中,您可以表示泛型类及其继承关系。例如,您可以绘制一个方框来表示GenericClass<T>,并在方框内列出其属性和方法。然后,您可以绘制一个类似的方框来表示SubGenericClass<T>,并在其内列出其附加属性和方法。您还可以在UML图中显示这两个类之间的继承关系。
在代码示例或伪代码中,您可以显示如何创建泛型类及其子类的实例,以及如何访问它们的属性和方法。这将有助于说明泛型类及其继承关系在实际代码中的工作原理。
(1)用UML图说明泛型类的可视化
下面是一个简单的UML类图,展示了如何表示一个泛型类及其子类:
+-----------------+ +------------------+
| GenericClass | | SubGenericClass |
+-----------------+ +------------------+
| - Value: T | | - OtherValue: T |
+-----------------+ +------------------+
| + Value {get,set}| | + OtherValue {get,set} |
+-----------------+ +------------------+
在这个UML图中,GenericClass<T>具有一个名为Value的属性,类型 T。 SubGenericClass<T>继承自GenericClass<T>,并添加了一个名为OtherValue的属性,类型也为T。这两个类都定义了相应的属性访问器(get 和 set)。
UML图并未显示泛型参数T,因为它与具体实现有关,而不是UML图的一部分。UML图主要关注类的结构和关系,而不是实际代码实现。
(2)用实例说明泛型类的可视化
以下是一个关于如何创建这些类的实例并使用它们的代码示例:
// 泛型类的可视化的代码实现
namespace _128_2
{
class Program
{
static void Main(string[] args)
{
ArgumentNullException.ThrowIfNull(args);
SubGenericClass<int> subGenericClass = new(42, 1337);
Console.WriteLine("Value: " + subGenericClass.Value);
Console.WriteLine("OtherValue: " + subGenericClass.OtherValue);
}
}
public class GenericClass<T>(T value)
{
public T Value { get; set; } = value;
}
public class SubGenericClass<T>(T value, T otherValue) : GenericClass<T>(value)
{
public T OtherValue { get; set; } = otherValue;
}
}
//运行结果:
/*
Value: 42
OtherValue: 1337
*/
示例创建了一个SubGenericClass<int>的实例,它继承自GenericClass<int>。先使用整数值42和1337来初始化Value和OtherValue属性。然后,再在控制台上打印这两个属性的值。
二、实例1
1.源码
// 通过继承泛型类来分块显示学生的个人信息以及成绩信息。
namespace _128
{
public partial class Form1 : Form
{
private GroupBox? groupBox1;
private GroupBox? groupBox2;
private TextBox? textBox6;
private TextBox? textBox5;
private TextBox? textBox4;
private TextBox? textBox3;
private TextBox? textBox2;
private TextBox? textBox1;
private Label? label6;
private Label? label5;
private Label? label4;
private Label? label3;
private Label? label2;
private Label? label1;
private TextBox? textBox9;
private TextBox? textBox7;
private Label? label9;
private TextBox? textBox8;
private Label? label8;
private Label? label7;
private Button? button1;
public Form1()
{
InitializeComponent();
StartPosition = FormStartPosition.CenterScreen;
Load += Form1_Load;
}
private void Form1_Load(object? sender, EventArgs e)
{
//
// label1
//
label1 = new Label
{
AutoSize = true,
Location = new Point(16, 25),
Name = "label1",
Size = new Size(44, 17),
TabIndex = 0,
Text = "编号:"
};
//
// label2
//
label2 = new Label
{
AutoSize = true,
Location = new Point(178, 25),
Name = "label2",
Size = new Size(44, 17),
TabIndex = 1,
Text = "姓名:"
};
//
// label3
//
label3 = new Label
{
AutoSize = true,
Location = new Point(16, 54),
Name = "label3",
Size = new Size(44, 17),
TabIndex = 2,
Text = "性别:"
};
//
// label4
//
label4 = new Label
{
AutoSize = true,
Location = new Point(178, 54),
Name = "label4",
Size = new Size(44, 17),
TabIndex = 3,
Text = "年龄:"
};
//
// label5
//
label5 = new Label
{
AutoSize = true,
Location = new Point(16, 83),
Name = "label5",
Size = new Size(44, 17),
TabIndex = 4,
Text = "生日:"
};
//
// label6
//
label6 = new Label
{
AutoSize = true,
Location = new Point(178, 83),
Name = "label6",
Size = new Size(44, 17),
TabIndex = 5,
Text = "班级:"
};
//
// textBox1
//
textBox1 = new TextBox
{
Location = new Point(59, 19),
Name = "textBox1",
Size = new Size(100, 23),
TabIndex = 6
};
//
// textBox2
//
textBox2 = new TextBox
{
Location = new Point(218, 19),
Name = "textBox2",
Size = new Size(100, 23),
TabIndex = 7
};
//
// textBox3
//
textBox3 = new TextBox
{
Location = new Point(59, 48),
Name = "textBox3",
Size = new Size(100, 23),
TabIndex = 8
};
//
// textBox4
//
textBox4 = new TextBox
{
Location = new Point(218, 48),
Name = "textBox4",
Size = new Size(100, 23),
TabIndex = 9
};
//
// textBox5
//
textBox5 = new TextBox
{
Location = new Point(59, 77),
Name = "textBox5",
Size = new Size(100, 23),
TabIndex = 10
};
//
// textBox6
//
textBox6 = new TextBox
{
Location = new Point(218, 77),
Name = "textBox6",
Size = new Size(100, 23),
TabIndex = 11
};
//
// label7
//
label7 = new Label
{
AutoSize = true,
Location = new Point(16, 24),
Name = "label7",
Size = new Size(44, 17),
TabIndex = 0,
Text = "语文:"
};
//
// label8
//
label8 = new Label
{
AutoSize = true,
Location = new Point(116, 24),
Name = "label8",
Size = new Size(44, 17),
TabIndex = 1,
Text = "数学:"
};
//
// label9
//
label9 = new Label
{
AutoSize = true,
Location = new Point(216, 24),
Name = "label9",
Size = new Size(44, 17),
TabIndex = 2,
Text = "英语:"
};
//
// textBox7
//
textBox7 = new TextBox
{
Location = new Point(62, 18),
Name = "textBox7",
Size = new Size(52, 23),
TabIndex = 3
};
//
// textBox8
//
textBox8 = new TextBox
{
Location = new Point(162, 18),
Name = "textBox8",
Size = new Size(52, 23),
TabIndex = 4
};
//
// textBox9
//
textBox9 = new TextBox
{
Location = new Point(262, 18),
Name = "textBox9",
Size = new Size(52, 23),
TabIndex = 5
};
//
// groupBox1
//
groupBox1 = new GroupBox
{
Location = new Point(12, 12),
Name = "groupBox1",
Size = new Size(333, 109),
TabIndex = 0,
TabStop = false,
Text = "学生信息"
};
groupBox1.Controls.Add(textBox6);
groupBox1.Controls.Add(textBox5);
groupBox1.Controls.Add(textBox4);
groupBox1.Controls.Add(textBox3);
groupBox1.Controls.Add(textBox2);
groupBox1.Controls.Add(textBox1);
groupBox1.Controls.Add(label6);
groupBox1.Controls.Add(label5);
groupBox1.Controls.Add(label4);
groupBox1.Controls.Add(label3);
groupBox1.Controls.Add(label2);
groupBox1.Controls.Add(label1);
groupBox1.SuspendLayout();
//
// groupBox2
//
groupBox2 = new GroupBox
{
Location = new Point(12, 125),
Name = "groupBox2",
Size = new Size(330, 49),
TabIndex = 0,
TabStop = false,
Text = "学生成绩"
};
groupBox2.Controls.Add(textBox9);
groupBox2.Controls.Add(textBox7);
groupBox2.Controls.Add(label9);
groupBox2.Controls.Add(textBox8);
groupBox2.Controls.Add(label8);
groupBox2.Controls.Add(label7);
groupBox2.SuspendLayout();
//
// button1
//
button1 = new Button
{
Location = new Point(267, 180),
Name = "button1",
Size = new Size(75, 23),
TabIndex = 1,
Text = "获取",
UseVisualStyleBackColor = true
};
button1.Click += Button1_Click;
//
// Form1
//
AutoScaleDimensions = new SizeF(7F, 17F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(359, 211);
Controls.Add(button1);
Controls.Add(groupBox2);
Controls.Add(groupBox1);
Name = "Form1";
Text = "继承泛型类输出学生信息";
groupBox1.ResumeLayout(false);
groupBox1.PerformLayout();
groupBox2.ResumeLayout(false);
groupBox2.PerformLayout();
}
/// <summary>
/// 通过派生类对象引用学生信息相关的字段,并为其赋值
/// 实例化派生类对象
/// </summary>
private void Button1_Click(object? sender, EventArgs e)
{
HStuInfo<object> Execute = new()
{
ID = 1,
Name = "小王",
Sex = "男",
Age = 16,
Birthday = Convert.ToDateTime("1993-11-29"),
Grade = "三年五班",
Chinese = 145,
Math = 140,
English = 137
};
//将学生信息显示在TextBox文本框中
textBox1!.Text = Execute.ID.ToString();
textBox2!.Text = Execute.Name.ToString();
textBox3!.Text = Execute.Sex.ToString();
textBox4!.Text = Execute.Age.ToString();
textBox5!.Text = Execute.Birthday.ToString();
textBox6!.Text = Execute.Grade.ToString();
textBox7!.Text = Execute.Chinese.ToString();
textBox8!.Text = Execute.Math.ToString();
textBox9!.Text = Execute.English.ToString();
}
/// <summary>
/// 学生信息泛型类
/// </summary>
class BStuInfo<T>
{
public T? ID; //声明学生编号字段
public T? Name; //声明姓名字段
public T? Sex; //声明性别字段
public T? Age; //声明年龄字段
public T? Birthday; //声明生日字段
public T? Grade; //声明班级字段
}
/// <summary>
/// 学生成绩泛型类
/// 继承自BStuInfo泛型类
/// </summary>
class HStuInfo<T> : BStuInfo<T>
{
public T? Chinese; //声明语文成绩字段
public T? Math; //声明数学成绩字段
public T? English; //声明英语成绩字段
}
}
}
2.生成效果
三、实例2
// 泛型类的继承
namespace _128_1
{
class Program
{
static void Main(string[] args)
{
ArgumentNullException.ThrowIfNull(args);
SubGenericClass<string> subGenericClass = new("Hello, World!", "Hello, again!");
Console.WriteLine(subGenericClass.GetValue());
Console.WriteLine(subGenericClass.GetOtherValue());
Console.WriteLine("**************");
subGenericClass.SetValue("New value");
subGenericClass.SetOtherValue("New other value");
Console.WriteLine(subGenericClass.GetValue());
Console.WriteLine(subGenericClass.GetOtherValue());
}
}
public class GenericClass<T>(T value)
{
private T Value = value;
public T GetValue()
{
return Value;
}
public void SetValue(T value)
{
Value = value;
}
}
public class SubGenericClass<U>(U value, U otherValue) : GenericClass<U>(value)
{
private U OtherValue = otherValue;
public U GetOtherValue()
{
return OtherValue;
}
public void SetOtherValue(U otherValue)
{
OtherValue = otherValue;
}
}
}
//运行结果:
/*
Hello, World!
Hello, again!
**************
New value
New other value
*/
在这个例子中,GenericClass是一个带有类型参数T的泛型类。SubGenericClass继承了GenericClass并具有自己的类型参数U。SubGenericClass可以使用 GenericClass的Value字段和方法,同时还添加了它自己的OtherValue字段和方法。
示例创建了一个SubGenericClass<string>的实例,将"Hello, World!"作为Value,将"Hello, again!"作为OtherValue。然后我们访问了GetValue()和GetOtherValue()方法以显示值。之后,我们使用SetValue("New value")和SetOtherValue("New other value")方法更新值,并再次使用GetValue()和GetOtherValue()方法显示更新后的值。