主题:对象传值的问题
/**程序说明:使用is运算符
* 创建日期:2006年11月12日*/
using System;
namespace IS
{
internal class ClassA
{
}
internal class ClassB:ClassA
{
}
internal class Checked
{
public void Check(object obj) //传入一个对象
{
if(obj is ClassA)
{
/*问题是如何输出对象名,比如说传入的是ClassA类的try1实例,在输出时应该输
*出"try1 Variable is ClassA",而不是单独输出 "Variable is ClassA",这该怎
*么实现*/
Console.WriteLine("Variable is ClassA");
}
else
{
Console.WriteLine("Variable isn't ClassA");
}
if(obj is ClassB)
{
Console.WriteLine("Variable is ClassB");
}
else
{
Console.WriteLine("Variable isn't ClassB");
}
}
}
class Class1
{
static void Main(string[] args)
{
Checked chek = new Checked();
ClassA try1 = new ClassA();
ClassA try2 = new ClassA();
ClassB try3 = new ClassB();
chek.Check(try1);
chek.Check(try2);
chek.Check(try3);
Console.Read();
}
}
}
* 创建日期:2006年11月12日*/
using System;
namespace IS
{
internal class ClassA
{
}
internal class ClassB:ClassA
{
}
internal class Checked
{
public void Check(object obj) //传入一个对象
{
if(obj is ClassA)
{
/*问题是如何输出对象名,比如说传入的是ClassA类的try1实例,在输出时应该输
*出"try1 Variable is ClassA",而不是单独输出 "Variable is ClassA",这该怎
*么实现*/
Console.WriteLine("Variable is ClassA");
}
else
{
Console.WriteLine("Variable isn't ClassA");
}
if(obj is ClassB)
{
Console.WriteLine("Variable is ClassB");
}
else
{
Console.WriteLine("Variable isn't ClassB");
}
}
}
class Class1
{
static void Main(string[] args)
{
Checked chek = new Checked();
ClassA try1 = new ClassA();
ClassA try2 = new ClassA();
ClassB try3 = new ClassB();
chek.Check(try1);
chek.Check(try2);
chek.Check(try3);
Console.Read();
}
}
}