When do you need to show up the constructor that calls the parent class?

Updated on technology 2024-04-02
10 answers
  1. Anonymous users2024-02-07

    When you write your own subclass constructor, you will be shown to call the parent constructor (if you need it, of course, but you can also leave it untuned) in the default constructor, and the parent constructor will be automatically called.

  2. Anonymous users2024-02-06

    When you are not using the default constructor of the parent class, you need to show the constructor defined by the parent class in the constructor of the child class.

    For example: Parent class:

    class animal{

    private string name;

    If you define a new constructor.

    public animal(string name) {= name;

    Subclass: public dog extends animal{ This is the constructor you want to show up calling the parent class, because the child class calls the parent class by default.

    Argument-free construct animal().

    public dog(){

    super("Puppies");Displays the argumentative constructor that calls the parent class.

    .Subclass constructor processing.

    Of course, if you write out the argumentless constructor in the parent class, for example:

    Parent: class animal{

    private string name;

    Parameter-free construction method.

    public animal()

    .Dispose.

    If you define a new constructor.

    public animal(string name) {= name;

    Then in the constructor of the subclass, you can call the constructor of the parent class without the display, because the subclass has a parameterless constructor, and the subclass will automatically call the parameterless constructor that the parent class has defined in the constructor.

    However, in general, if you use the overload of the constructor in the parent class, you can call the corresponding parent constructor in the child class as needed.

  3. Anonymous users2024-02-05

    Summary. When you call a class's constructor, the parent class's non-static chunk is always executed first, then the parent class's constructor, and finally the current class.

    Non-static blocks and constructors. There is a sequence in the execution process.

    If you want to explicitly call the constructor of the parent class, you can use super(), but both the superkeyword and the this keyword must be placed on the first line of the construct, and only the .

    With one, why put it in the first row? Because if it is not placed in the first line, the initialization of the child class is called first, and then the initialization of the parent class is called, then the initialized value in the parent class.

    Overrides the initialized value in the subclass.

    How do I call the parent class's instance variables, methods, and constructors? What are the conditions for instance variables, methods, and constructors to access modifiers that can be called?

    Hello, I am sorting out your question and will reply to you immediately.

    When calling the constructor of a class, Qingjin will always execute the non-static block of the parent class first, and then execute the constructor of the parent class, and then execute the non-static block and constructor of the current class. There is a sequence in the execution process. If you want to explicitly call the constructor of the parent class, you can use super(), to call it, but both the super keyword and the this keyword must be placed on the first line of the construct, and only one can be used, so why put it on the first line?

    Because if you don't put it on the first line, you call the initialization of the child class first, and then call the initialization of the parent class, then the initialized value in the parent class will overwrite the initialized value in the child class.

    Short-answer questions. Freshman exam questions.

  4. Anonymous users2024-02-04

    There are probably two situations:

    1) The parent class is an abstract class (with pure virtual functions), and the child class is no longer used to derive a subordinate class, in which case the child class must implement the methods in the parent class.

    2) For a method in the parent class, the function of the subclass needs to be changed, and the subclass must re-implement the method in the parent class.

  5. Anonymous users2024-02-03

    1. All constructors of the subclassDefault callParameterless constructor of the parent class.

    2. If the parent class does not define a constructor, the system will define a no-argument no-return value by default.

    If the parent class defines a parameter constructor, the first line of the child class's constructor must explicitly call a parameterized constructor defined by the parent class. That is, the number of corresponding parameters, the corresponding parameter type, and this super( [arg0][,arg1]....

    The parent class constructor.

    3. If a constructor of a subclass wants to call other constructors with parameters of the parent class, artificially add super(val1,val2[,val3...) to the first line of the constructorThe number of variables in parentheses in super() is determined by the number of variables in the constructor of the parent class you want to call. For example, line 2 in ** calls the constructor of the two parameters in the parent class constructor, then super(20, "hello") is two variables.

    4. Add super(val1,val2,... by yourselfYou can specify the constructor that calls the parent class with the same type and number of arguments. In the subclass constructor, the system will no longer call the parent parameterless constructor by default;

    5. If each constructor of the subclass adds super([val1,]...Unless the parent class parametric constructor is artificially tuned, the parent class's parameterless constructor can be omitted. If there is a parent constructor called by the super specified, it is sufficient.

    6. super refers to the parent class object, and super. can be used in the subclassparent method name source manuscript (); The method in the parent class can be called (whether it is a class method or an instance method), and the invocation of the instance method can also be instantiated inside the method and then called.

  6. Anonymous users2024-02-02

    It's all wrong, 1. The parent class is a subset of the child class.

    2. The subclass inherits from the parent class, and the constructor of the parent class must be called in the construction and destruction function of the subclass, if the subclass has the constructor, there is no displayed call, then the parameter-free constructor of the parent class Lu Yuyuan will be automatically called, and if the parent class does not have a parameter-free constructor, then the constructor of the parent class must be displayed in the subclass.

    3. The definition of the private keyword is "no other class can access this member except the class that contains it." In other words, only you can directly access the private member, and no other class, including subclasses, can access this member.

    4. 'Static methods cannot be overridden, but the same static methods can be defined in the child class to hide the methods of the parent class.

  7. Anonymous users2024-02-01

    Certainly, and it must be written on the first line of the subclass construction method, however, there are special cases:

    1. There are both parametric and parametric constructors in the parent class, so if the subclass needs to call the parametric constructor, you can not write it, and the system will automatically add it, and if you want to call the constructor of the parent class with parameters, you must write it.

    Second, there is no parameterless constructor in the parent class, only the one with parameters, which must be explicitly called in the subclass.

    3. If the subclass has multiple constructors, such as two ab constructors, and the parent class's constructor has been called inside the A constructor (or the parent class's non-argumented constructor is implicitly called), and in the B constructor, the A constructor will be called, then the b constructor must call the A constructor in the form of this() on the first line, and there is no need to write super().

    It seems to be very tongue-twisting.,Take your time to look at it.,,Can you ask for an adoption?

  8. Anonymous users2024-01-31

    The constructor of the child class does not need to call the constructor of the parent class, but the parameterless constructor of the parent class is called before the constructor of the subclass is executed.

  9. Anonymous users2024-01-30

    Points 2 and 3, there is a bit of a deviation in understanding! ~

    Second, if there is no default constructor in the parent class, there can be multiple constructors in the child class, but the parent class's constructor must be called.

    For example, the parent class test1

    Public test1 (string a) subclass public class test2 extends test1......

    public test2()

    public test2(string a)public test2(int a, string b)point 3, when the parent class has a default constructor and a parameter constructor, if you don't use super to call the parametric constructor, then the default tune is the parent class's parametric constructor! ~

    Can you understand that? good luckļ¼~

  10. Anonymous users2024-01-29

    No, the parent constructor is only used to instance the part of the parent class in the inherited subclass, and has nothing to do with the subclass, only the abstract parent requires the subclass to override the parent method.

Related questions
13 answers2024-04-02

After the purchase of ** before 15 o'clock on the day of T day, after the confirmation of T+1, the company will announce the earnings after 9 o'clock in the evening of each trading day. Currency**Earnings during weekends or holidays are combined on the evening of the first trading day after the holiday, and some currency**Earnings will not be added to the position until the earnings are carried forward. For investment in overseas markets**, T+2 confirmation is required before you can view the returns. >>>More

7 answers2024-04-02

Gardenia fertilization time.

Gardenias need to be reasonably supplemented with fertilizer during the growth period. Apply rotten human manure or cake fertilizer every 10 days or so, stop watering 1 day before fertilization, and water once at the same time on the day of fertilization. Fertilization will be stopped from mid-September. >>>More

7 answers2024-04-02

[Calculate how much it will cost you to renovate your home]. >>>More

5 answers2024-04-02

The MBA exam is divided into two stages: written test and face-to-face (double-)test. 1. MBA Preliminary Examination: The preliminary examination is a national joint examination organized by the Ministry of Education of the People's Republic of China. >>>More

8 answers2024-04-02

6-year exemption means that it takes 6 years to go to the inspection line, but it takes 2 years to go to the vehicle management office or the traffic police team to receive the bid.