110,577
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
A X;
X = CreateClass(typeof(A));
X = CreateClass(typeof(B));
Console.Read();
}
class A
{
public A()
{
Console.WriteLine("A");
}
}
class B : A
{
public B()
{
Console.WriteLine("B");
}
}
static A CreateClass(Type T)
{
if (T==typeof(B))
return new B();
else
return new A();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
A X;
X = new A();
Print(X);
X = new B();
Print(X);
Console.Read();
}
class A
{
public A()
{
Console.WriteLine("A");
}
}
class B:A
{
public B()
{
Console.WriteLine("B");
}
}
static void Print(A X)
{
if (X is B)
Console.WriteLine("Print B");
else
Console.WriteLine("Print A");
}
}
}
A a = new B();
//相当于
A a;
B b = new B();
a = b;
A a = new B();
func(a);
void func(A a){}
B b = new B();
func(b);
void func(A a){}