gpt4 book ai didi

c# - 从父类(super class)中获取字段

转载 作者:太空宇宙 更新时间:2023-11-03 23:08:43 25 4
gpt4 key购买 nike

我有以下抽象类:

 abstract class Customer
{
private string address { get; set; }
private int phone { get; set; }

public Customer(string address, int phone)
{
this.address = address;
this.phone = phone;
}
}

然后我有以下继承自客户类的类:

   class Private : Customer
{

private string name { get; set; }
private int age { get; set; }
private string sex { get; set; }

public Private(string name, int age, string sex, string address, int phone) : base(address, phone)
{
this.name = name;
this.age = age;
this.sex = sex;
}

我的问题是:如何访问私有(private)类(class)中的电话和地址字段?

最佳答案

对继承类可以访问的所有内容使用 protected 修饰符而不是 private

来自文档:

A protected member is accessible within its class and by derived class instances.

https://msdn.microsoft.com/en-us/library/bcd5672a.aspx

关于c# - 从父类(super class)中获取字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40228598/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com