简体   繁体   中英

How to add more code to the child class constructor while passing arguments to parent class constructor?

Child class:

using System;
using System.Collections;
namespace Game
{
    public class Warrior:Inhabitant
    {
        public Warrior(string name,int age):base(name,age)
        {
            private bool mobility;
            private bool immortality;
        }
    }
}

Now the parent class:

public class Inhabitant {

     private string name;
     private int age;

    public Inhabitant(string name,int age){
        this.name = name;
        this.age = age;
    }

This code worked in Java. I can't understand why it's not working here.

You need to declare private fields and initialize them in constructor

using System;
using System.Collections;
namespace Game
{
    public class Warrior:Inhabitant
    {
        // declare private fields
        private bool mobility;
        private bool immortality;

        public Warrior(string name,int age, bool mobility, bool immortality):base(name,age)
        {
            // initialize
            this.mobility=mobility;
            this.immortality=immortality;
        }
    }
}
  1. Declare your members outside the constructor:

     public class Warrior:Inhabitant { private bool mobility; private bool immortality; // ... } 
  2. Pass parameters to your new constructor:

     public class Warrior:Inhabitant { private bool mobility; private bool immortality; public Warrior(string name,int age, bool mobility, bool immortablity) : base(name,age) // base call as usual { // set properties this.mobility = mobility; this.immortability = immortability; } } 

First of all you can not declare any variable as private inside constructor or method of a class.

Change Warrior class as following and it should resolve the issue.

public class Warrior:Inhabitant
{
    private bool mobility;
    private bool immortality;
    public Warrior(string name,int age):base(name,age)
    {

    }
}

if they are the variables local to the constructor then they should be declared without access modifiers.

public class Warrior:Inhabitant
{
    public Warrior(string name,int age):base(name,age)
    {
        bool mobility;
        bool immortality;
    }
}

you cant use access modifiers inside constructor. Try:

public Warrior(string name,int age):base(name,age)
{
   bool mobility;
   bool immortality;
}

The above is syntactically correct , but does not do anything semantically, since mobility and immortality live only inside the constructor. Probably you want sth like:

public class Warrior:Inhabitant
{
       private bool mobility;
       private bool immortality;
       public Warrior(string name,int age):base(name,age)
       {
            this.mobility=false; //example case
            this.immortality=true; //example case
       }
}

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