文章内容都是自己学习笔记。
一 基类和枚举
代码
public class TestPoint
{
public int X { get; set; }
public int Y { get; set; }
public TestPoint(int x, int y) => (X, Y) = (x,y); //元组
public void Run()
{
Console.WriteLine($"基类方法============数据1:{X},----数据2:{Y}");
}
}
//https://learn.microsoft.com/zh-cn/dotnet/csharp/tour-of-csharp/types
public class Point3D : TestPoint
{
public int Z { get; set; }
public Point3D(int x,int y,int z) : base(x, y)
{
Z = z;
}
public void Run2()
{
Console.WriteLine($"继承类自己的方法,数据1:{X},----数据2:{Y},-----数据3:{Z}");
}
}
[Flags] //https://www.cnblogs.com/GreenLeaves/p/6752822.html
public enum Seasons
{
None = 0,
Summer = 1,
Autumn = 2,
Winter = 4,
Spring = 8,
All = Summer | Autumn | Winter | Spring
}
二 内部类
代码
public class Stack<T>
{
Entry _top;//字段
public void Push(T data)
{
_top = new Entry(_top, data);//赋值
}
public T Pop() //返回泛型删除
{
if (_top == null)
{
throw new InvalidOperationException();
}
T result = _top.Data;
_top = _top.Next; //重新赋值
return result;
}
class Entry //内部类
{
public Entry Next { get; set; }//自己是自己的属性
public T Data { get; set; }//泛型参数类
public Entry(Entry next, T data)//内部类构造函数
{
Next = next;
Data = data;
}
}
}
三 有关 nameof ,typeof,@符号用法
代码
public class Student
{
public int stId { get; set; }
public string stuName { get; set; }
public int num;
private string name;
public string Name
{
get => name;
set => name = value ?? throw new ArgumentNullException(nameof(value)+"777777", $"{nameof(Name)} cannot be null");
//System.ArgumentNullException: Name cannot be null
}
}
四 测试代码
TestPoint a = new TestPoint(10, 20);
Point3D pc = new Point3D(10, 520, 30);
TestPoint b = pc;
a.Run();
Console.WriteLine("================分割线1================");
b.Run();//基类还是只能显示自己有的属性和功能
// b.Run2(); 不可用
Console.WriteLine("===============分割线2=================");
pc.Run();
Console.WriteLine("===============分割线3=================");
pc.Run2();
var spring = Seasons.Spring;
Console.WriteLine(spring);
var startingOnEquinox = Seasons.Spring | Seasons.Autumn;
Console.WriteLine(startingOnEquinox);
var theYear = Seasons.All;
Console.WriteLine(theYear);
Console.WriteLine("==========分割线===========");
int? optionalInt = default;
optionalInt=88;
Console.WriteLine(optionalInt);
Console.WriteLine("==========分割线===========");
var sD = new Stack<int>();
sD.Push(1); // stack contains 1
sD.Push(10); // stack contains 1, 10
sD.Push(100); // stack contains 1, 10, 100
//原来可以无限包含呀
Console.WriteLine(sD.Pop()); // stack contains 1, 10
Console.WriteLine("==========分割线1===========");
Console.WriteLine(sD.Pop()); // stack contains 1
Console.WriteLine("==========分割线2===========");
Console.WriteLine(sD.Pop()); // stack is empty
Console.WriteLine("==========分割线88888===========");
Student p1 = new Student();
string s1 = nameof(p1);
Console.WriteLine(s1);
string s2 = nameof(Student);
string s3 = nameof(p1.stuName);
string s4 = nameof(Student.stuName);
string s5 = nameof(p1.num);
try
{
p1.Name = null;
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
Console.WriteLine(p1.Name);
Console.WriteLine(s2);
Console.WriteLine(s3);
Console.WriteLine(s4);
Console.WriteLine(s5);
Console.WriteLine("==========分割线2===========");
var @dataTest = 5;
var @this = "88888";
Console.WriteLine(nameof(@dataTest)); // output: dataTest
Console.WriteLine(dataTest);
Console.WriteLine(@dataTest);//https://learn.microsoft.com/zh-cn/dotnet/csharp/language-reference/operators/nameof#code-try-0
Console.WriteLine(@this);
// Console.WriteLine(this);
var n1 = typeof(Student).Name;
var n2 = typeof(int).Name;
// Fixed code
var n11 = nameof(Student);
var n22 = nameof(Int32);
Console.WriteLine(n1);//Student
Console.WriteLine(n11);//Student
Console.WriteLine(n2);//Int32
Console.WriteLine(n22);//Int32
五 运行结果图
六 结论
基础还是要打牢,学习要持续,不然跟不上变化。