Demo15
using System;
namespace Demo15
{
public class BaseClass
{
private string baseClassPrivateVar="BaseClassPrivateVar";
protected string baseClassProtectedVar="BaseClassProtectedVar";
public string baseClassPublicVar="BaseClassPublicVar";
private void BaseClassPrivateMethod()
{
Console.WriteLine("BaseClass BaseClassPrivateMethod Called");
}
protected void BaseClassProtectedMethod()
{
Console.WriteLine("BaseClass BaseClassProtectedMethod Called");
}
public void BaseClassPublicMethod()
{
Console.WriteLine("BaseClass BaseClassPublicMethod Called");
}
public virtual void VirtualMethod()
{
Console.WriteLine("BaseClass VirtualMethod Called");
}
public override string ToString()
{
return string.Format("baseClassPrivateVar={0},baseClassProtectedVar={1},baseClassPublicVar={2}",baseClassPrivateVar,baseClassProtectedVar,baseClassPublicVar);
}
}
public class DriveClass:BaseClass
{
private string driveClassPrivateVar="DriveClassPrivateVar";
protected string driveClassProtectedVar="DriveClassProtectedVar";
public string driveClassPublicVar="DriveClassPublicVar";
private void DriveClassPrivateMethod()
{
Console.WriteLine("DriveClass DriveClassPrivateMethod Called");
}
protected void DriveClassProtectedMethod()
{
Console.WriteLine("DriveClass DriveClassProtectedMethod Called");
}
public void DriveClassPublicMethod()
{
Console.WriteLine("DriveClass DriveClassPublicMethod Called");
}
public override void VirtualMethod()
{
Console.WriteLine("DriveClass VirtualMethod Called");
}
public override string ToString()
{
return string.Format("driveClassPrivateVar={0},driveClassProtectedVar={1},driveClassPublicVar={2}",driveClassPrivateVar,driveClassProtectedVar,driveClassPublicVar);
}
public static void Main()
{
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
{
public static void TestMain()
{
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();
}
}
}
仔细看看代码里注释的部分,相信大部分人都能明白了。
using System;
namespace Demo15
{
public class BaseClass
{
private string baseClassPrivateVar="BaseClassPrivateVar";
protected string baseClassProtectedVar="BaseClassProtectedVar";
public string baseClassPublicVar="BaseClassPublicVar";
private void BaseClassPrivateMethod()
{
Console.WriteLine("BaseClass BaseClassPrivateMethod Called");
}
protected void BaseClassProtectedMethod()
{
Console.WriteLine("BaseClass BaseClassProtectedMethod Called");
}
public void BaseClassPublicMethod()
{
Console.WriteLine("BaseClass BaseClassPublicMethod Called");
}
public virtual void VirtualMethod()
{
Console.WriteLine("BaseClass VirtualMethod Called");
}
public override string ToString()
{
return string.Format("baseClassPrivateVar={0},baseClassProtectedVar={1},baseClassPublicVar={2}",baseClassPrivateVar,baseClassProtectedVar,baseClassPublicVar);
}
}
public class DriveClass:BaseClass
{
private string driveClassPrivateVar="DriveClassPrivateVar";
protected string driveClassProtectedVar="DriveClassProtectedVar";
public string driveClassPublicVar="DriveClassPublicVar";
private void DriveClassPrivateMethod()
{
Console.WriteLine("DriveClass DriveClassPrivateMethod Called");
}
protected void DriveClassProtectedMethod()
{
Console.WriteLine("DriveClass DriveClassProtectedMethod Called");
}
public void DriveClassPublicMethod()
{
Console.WriteLine("DriveClass DriveClassPublicMethod Called");
}
public override void VirtualMethod()
{
Console.WriteLine("DriveClass VirtualMethod Called");
}
public override string ToString()
{
return string.Format("driveClassPrivateVar={0},driveClassProtectedVar={1},driveClassPublicVar={2}",driveClassPrivateVar,driveClassProtectedVar,driveClassPublicVar);
}
public static void Main()
{
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
{
public static void TestMain()
{
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();
}
}
}