简体   繁体   中英

Can I have a 'protected' member in a parent class and inherit it in a child class?

I have 3 kinds of users like admin, customer, and supplier, which are all planned to be inherited from the User class. Can I use protected for username and password in the User class and inherit them in child classes? All tutorials seem to use private , is there any special reason for that?

Can I use protected for username and password in user class

Yes

Whether you should is another question. If they are fields, they personally I'd say: keep the field private and add a protected property - or just simply:

protected string UserName {get;set;}

or maybe:

protected string UserName {get;private set;}

if only the base class should be able to set it, or

protected string UserName {get;}

if only the base class should be able to set it and only in the constructor

Side note: any discussion of storing a password - even in memory - makes me nervous.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM