可以通过 反射类验证
父类和子类:
public abstract class Parent
{
private void PrivSaySomeWord()
{
Console.WriteLine("我是一个私有方法!");
}
private string PrivProp { get; set; }
}
public class Child: Parent
{
public void CommonSay()
{
Console.WriteLine("子级的普通方法");
}
}
反射代码:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("********************打印父类方法和属性***********************");
var type = typeof(Parent);
var res = string.Join(",", type.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance).Select(p => $"{p.Name}"));
Console.WriteLine("***属性***");
Console.WriteLine(res);
res = string.Join(",", type.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance).Select(p => $"{p.Name}"));
Console.WriteLine("***方法***");
Console.WriteLine(res);
Console.WriteLine("********************打印子类方法和属性***********************88");
type = typeof(Child);
res = string.Join(",", type.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance).Select(p => $"{p.Name}"));
Console.WriteLine("***属性***");
Console.WriteLine(res);
res = string.Join(",", type.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance).Select(p => $"{p.Name}"));
Console.WriteLine("***方法***");
Console.WriteLine(res);
//parent.CommonSay();
//parent.VirtualSay();
//parent.AbstractSay();
Console.ReadLine();
}
}
结果:事实证明 子类不会继承父类的私有成员