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;
}