关于基类与派生类的学习
在一些面试中,基类与派生类的调用是少不了的题目,也是作为面向对象必需掌握的内容。今天就来复习一下所学的。
![](/Images/OutliningIndicators/ContractedBlock.gif)
Demo15
using System;
![](/Images/OutliningIndicators/None.gif)
namespace Demo15
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
public class BaseClass
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private string baseClassPrivateVar="BaseClassPrivateVar";
protected string baseClassProtectedVar="BaseClassProtectedVar";
public string baseClassPublicVar="BaseClassPublicVar";
private void BaseClassPrivateMethod()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("BaseClass BaseClassPrivateMethod Called");
}
protected void BaseClassProtectedMethod()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("BaseClass BaseClassProtectedMethod Called");
}
public void BaseClassPublicMethod()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("BaseClass BaseClassPublicMethod Called");
}
public virtual void VirtualMethod()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("BaseClass VirtualMethod Called");
}
public override string ToString()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return string.Format("baseClassPrivateVar={0},baseClassProtectedVar={1},baseClassPublicVar={2}",baseClassPrivateVar,baseClassProtectedVar,baseClassPublicVar);
}
}
public class DriveClass:BaseClass
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private string driveClassPrivateVar="DriveClassPrivateVar";
protected string driveClassProtectedVar="DriveClassProtectedVar";
public string driveClassPublicVar="DriveClassPublicVar";
private void DriveClassPrivateMethod()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("DriveClass DriveClassPrivateMethod Called");
}
protected void DriveClassProtectedMethod()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("DriveClass DriveClassProtectedMethod Called");
}
public void DriveClassPublicMethod()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("DriveClass DriveClassPublicMethod Called");
}
public override void VirtualMethod()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("DriveClass VirtualMethod Called");
}
public override string ToString()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return string.Format("driveClassPrivateVar={0},driveClassProtectedVar={1},driveClassPublicVar={2}",driveClassPrivateVar,driveClassProtectedVar,driveClassPublicVar);
}
public static void Main()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("==================内部调用开始==========================");
Console.WriteLine();
Console.WriteLine("在派生类中,基类调用自身的方法,只能调用pulic方法");
BaseClass bc=new BaseClass();
Console.WriteLine("基类实例调用bc.BaseClassPrivateMethod();出现访问权限越级错误");
//bc.BaseClassPrivateMethod();
Console.WriteLine("基类实例调用bc.BaseClassProtectedMethod();出现访问权限越级错误");
//bc.BaseClassProtectedMethod();
Console.WriteLine("基类实例调用bc.BaseClassPublicMethod();");
bc.BaseClassPublicMethod();
Console.WriteLine("基类实例调用bc.VirtualMethod();");
bc.VirtualMethod();
//Console.WriteLine(bc.ToString());
Console.WriteLine();
Console.WriteLine("在派生类内部,自己可以调用自己的所有方法,没有任何悬念!");
DriveClass dc=new DriveClass();
dc.DriveClassPrivateMethod();
dc.DriveClassProtectedMethod();
dc.DriveClassPublicMethod();
dc.VirtualMethod();
Console.WriteLine();
Console.WriteLine("在派生类中,派生类调用基类的方法,只能调用protected和public方法");
dc.BaseClassProtectedMethod();
dc.BaseClassPublicMethod();
//Console.WriteLine(dc.ToString());
Console.WriteLine();
Console.WriteLine("用BaseClass baseClass=new DriveClass();创建的实例对象,只能调用基类的公有方法,和被重写了的virtual方法");
BaseClass baseClass=new DriveClass();
Console.WriteLine("如果用这样创建的实例对象,访问基类private和protected方法,如下几句则显示错误:访问权限超过级别");
Console.WriteLine("baseClass.BaseClassPrivateMethod();");
Console.WriteLine("baseClass.BaseClassProtectedMethod();");
//baseClass.BaseClassPrivateMethod();
//baseClass.BaseClassProtectedMethod();
Console.WriteLine("但是这样创建的实例对象,可以调用基类的公有方法,和被重写了的virtual方法");
baseClass.BaseClassPublicMethod();
baseClass.VirtualMethod();
Console.WriteLine("如果用这样创建的实例对象,访问派生类private,protected和public方法,如下几句则显示错误:基类没有该方法");
Console.WriteLine("baseClass.DriveClassPrivateMethod();");
Console.WriteLine("baseClass.DriveClassProtectedMethod();");
Console.WriteLine("baseClass.DriveClassPublicMethod();");
//baseClass.DriveClassPrivateMethod();
//baseClass.DriveClassProtectedMethod();
//baseClass.DriveClassPublicMethod();
Console.WriteLine("==================内部调用结束==========================");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("==================外部调用开始==========================");
Test.TestMain();
Console.WriteLine("==================外部调用结束==========================");
}
}
public class Test
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public static void TestMain()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
BaseClass bc=new BaseClass();
//bc.BaseClassPrivateMethod();
//bc.BaseClassProtectedMethod();
bc.BaseClassPublicMethod();
bc.VirtualMethod();
DriveClass dc=new DriveClass();
//dc.DriveClassPrivateMethod();
//dc.DriveClassProtectedMethod();
dc.DriveClassPublicMethod();
dc.VirtualMethod();
BaseClass baseClass=new DriveClass();
//baseClass.BaseClassPrivateMethod();
//baseClass.BaseClassProtectedMethod();
baseClass.BaseClassPublicMethod();
baseClass.VirtualMethod();
//baseClass.DriveClassPrivateMethod();
//baseClass.DriveClassProtectedMethod();
//baseClass.DriveClassPublicMethod();
}
}
}
仔细看看代码里注释的部分,相信大部分人都能明白了。
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
作者:Jackhuclan
出处:http://jackhuclan.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://jackhuclan.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。