parent child - Basics of Inheritance -
Consider a parent class with some variables a, b, c if I get a child's class from this basic class If I do, will the kid's classes A, B, C be detected? If yes, then the value of A, B and C will also be in this child category? OOP languages have different levels of access, which determine the visibility of fields ("variables") from the outside. Most OOP-languages inside the classroom have at least three of the following: Of course, the methods in the base class can reach the private variable always in the base class - even if the newly added methods in the derived category can not see them. Here is an example in C ++ (there is a similar syntax in other OOP languages). private ,
protected and
public . If your base is a class variable
private , the derivative classes can not see them, if they are
protected (but not non-derived classes) and if they are
public Everyone can see them - both derivative and non-related classes.
class base {private: int a; Protected: int b; Public: int f () {a = 1; Return A + B; }} Category derivative: Public base {Public: intg () {// You can not access it, so it is illegal: a = 2; // You can reach B here, so it's legal: b = 2; // base :: F, or parent :: F () in Java, one can use, then it is 1 + 2 = 3. Return base :: F (); }} Class non-related {void h () {derived d; // Create a derivative item. These two are illegal because neither neither nor the public is: d.a = 3; Db = 4; // You can call both * f * and g (), because they both are public // this will be back at 3. (Side note: In real C ++, calling DF () will not be a good idea, because no one is initial). D.g (); }}
Comments
Post a Comment