110,570
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
a<Program>.Add();//T类型不适合此方法
a<int>.Add();//jijubhuibuib
a<string>.Add();
Console.ReadKey();
}
}
class a<T>
{
private static T t;
public static T Add()
{
//①子类对象可以直接赋值给父类变量;
//③如果父类对象中装的是子类对象,可以将这个父类对象强转为子类对象;
//is:如果转换成功,返回 true,失败返回 false;
if (t is int || (t as string == null))//如果返回为true则证明T类型是int和string
{
Console.WriteLine("复制并连接或相加");
t = t + t;
return t;
}
else
{
Console.WriteLine("T类型不适合此方法");
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _787878
{
class Program
{
static void Main(string[] args)
{
}
}
class a<T>
{
public static int operator +(int a,int b)
{
return a + b;
}
public static float operator +(float a, float b)
{
return a + b;
}
public static double operator +(double a, double b)
{
return a + b;
}
public static int operator-(int a,int b)
{
return a - b;
}
T ff<T>(T a,T b)
{
return a + b;
}
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
var a = new A();
A.t = 1;
var ret = a.Add();
A.t = "123";
var ret1 = a.Add();
}
}
class A
{
public static dynamic t;
public dynamic Add()
{
return t + t;
}
}