本文实例分析了C#引用访问权限问题。分享给大家供大家参考。具体分析如下:
同样代码表现的不同行为:
创建基类(Super)和派生类(Sub)每个类有一个字段field和一个公共方法getField,并且使用内联的方式初始化为1,方法getField返回字段field。C#和Java代码及运行结果如下
class Super { public int field = 0; public int getField() { return field; } }class Sub : Super { public int field = 1; public int getField() { return field; } }
class Program { private static void Main(string[] args) { Super sup = new Sub(); Console.WriteLine("sup.getField() result:" + sup.getField()); Console.ReadKey(); } }
class Super { public int field = 0; public int getField() { return field; } } class Sub extends Super { public int field = 1; public int getField() { return field; } } public class polymorphicDefects { public static void main(String[] args) { Super sup = new Sub(); System.out.println("sup.getField() result:" + sup.getField()); } }
class Super { public int field = 0; public virtual int getField() { return field; } }class Sub : Super { public int field = 1; public override int getField() { return field; } }
class Program { private static void Main(string[] args) { Super sup = new Sub(); Console.WriteLine("sup.getField() result:" + sup.getField()); Console.ReadKey(); } }
希望本文所述对大家的C#程序设计有所帮助。