C# 之属性和泛型运用实例
属性(Property)
是什么:
https://www.runoob.com/csharp/csharp-property.html
属性分为抽象属性和非抽象
泛型(generic)
优点:可以增加代码的重用性,且泛型在编译时有效,运行时没有。
泛型是什么:
https://www.runoob.com/csharp/csharp-generic.html
我的代码实例:
//泛型和属性的使用
using System;
namespace cs2_1
{
class Program
{
static void Main(string[] args)
{
//创建CommonDate对象
CommonDate<String> name = new CommonDate<string>();
CommonDate<float> version = new CommonDate<float>();
//运用属性,是赋值不是(值)
name.Value = "张三";
version.Value = 2.0F;
//输出到控制台的方法,向下面这样是按字符串输出
Console.WriteLine(name.Value);
Console.WriteLine(version.Value);
//使运行界面固定
Console.ReadKey();//记住这个方法
}
}
class CommonDate<T> {
private T _date;
//属性(跟方法类似,只是在属性名后没有())
public T Value{
get {
return this._date;
}
set {
this._date = value;
}
}
}
}
/*技术总结:
IDEA中的Shift + enter 任意位置开下一行
ctrl+d 复制改行都能在vs中用,可以探索其他快捷键*/