我正在尝试了解C#中的OOP概念.
在以下示例代码中:
ins1更喜欢通用方法ins2,ins3更喜欢非泛型方法注意:当我注释掉任何一个"MyTestMethod"方法时,程序仍然会继续成功运行.这段代码不是来自制作的东西.这只是我的训练样本.所以,请不要介意命名约定和标准.
using System;
namespace ConsoleApplication1
{
class Program
{
public static void MyTestMethod(J input)
{
Console.WriteLine($"Program.MyTestMethod: {input.Val}");
}
public static void MyTestMethod<T>(T input) where T : J
{
Console.WriteLine($"Program.MyTestMethod<T>: {input.Val}");
}
static void Main(string[] args)
{
J2 ins1 = new J2(1);
MyTestMethod(ins1);
J ins2 = new J(2);
MyTestMethod(ins2);
J ins3 = new J2(3);
MyTestMethod(ins3);
Console.ReadKey();
}
}
internal class J
{
public int Val { get; set; }
public J(int …Run Code Online (Sandbox Code Playgroud)