class Fruit
{ }
class Apple : Fruit
{
public int i=1;
}
public class Mod
{
static void Main()
{
Fruit f = new Apple();
Type t = f.GetType();
Apple a=f;
Console.WriteLine(a.i);
}
}想问的是 f 属于Fruit类型,但是用Apple来new,而且GetType 显示也是Apple
为什么Apple a=f;这样,却访问不了i呢? 还要强制转换为什么呢?
f到底是Apple 还是Fruit呢?如果是Fruit,怎么GetType又是Apple呢?
这个问题没搞懂.
完毕.
{
public abstract int i { get;}
}
class Apple : Fruit
{
public override int i
{
get { return 1; }
}
}
public class Mod
{
static void Main()
{
Fruit f = new Apple();
Type t = f.GetType();
Apple a = f;
Console.WriteLine(a.i);
}
}
告诉外星人,人类都有个脑袋。见谁都不会闹笑话。
丫死人不算...完毕。
{ }
class Apple : Fruit
{
public int i = 1;
}
public class Mod
{
static void Main()
{
Fruit f = new Apple();
Type t = f.GetType();
Apple a = (Apple)f;
Console.WriteLine(a.i);
}
}
强制类型转化一下就好了,C#它不知道你是那个儿子来代替父亲,所以他不会通过编译的