本文介绍了嗨,盖兹(Guyz),您能给我一个容易理解的typeof()含义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨盖兹,
我对使用Typeof()感到困惑,因为它的工作方式和用途
Google无法给我一个明确的答案
您能否给出一个易于理解的typeof();含义?
对于此示例:
hi guyz,
i''m confused of using Typeof(), on how it works and what''s for
google can''t give me a clear answer
can you give an easy-to-understand meaning of typeof();
for this example:
//where Customer in a class
typeof(Customer)
//Customer class
public class Customer
{
public string SayHello(string name)
{
return "Hello" + name + "!...";
}
}
谢谢Guyz,我将感谢您的回答!
thanks guyz i''ll appreciate ur answer!
推荐答案
class Customer {/*...*/}
class ValuedCustomer : Customer {/*...*/}
//...
//usually, using such methods means abuse of OOP but can be useful in some special cases:
static bool IsDerivedCustomer(Customer customer) {
return typeof(Customer) == customer.GetType();
}
//...
Customer customer = new Customer();
Customer valuedCustomer = new ValuedCustomer();
if (IsDerivedCustomer(customer)) //will return false
System.Console.WriteLine("This is a base type. Learn OOP");
else
System.Console.WriteLine("This is a derived type. Learn OOP anyway");
if (valuedCustomer is Customer) //will return true
System.Console.WriteLine(
"This is a Customer;" +
"this method helps to check if an instance belongs to the class hierarchy");
这篇关于嗨,盖兹(Guyz),您能给我一个容易理解的typeof()含义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!