How to pass an attribute of the child class through the constructor to the mother class

Asked

Viewed 220 times

1

I have 3 classes

Mother class: Animal

Child Classes: Dog and Cat

I want to save the name attribute only when one of the objects is instantiated Gato or Cachorro.

In the mother class I have the attribute Name which can be read-only (get)

How to pass the value (attribute nome) from class daughters to mother through the builders of the daughter classes?

  • If the classes Cachorro and Gato are inherited from Animal and Animal has the property Nome just do Nome = "nome";. For more details you need to enter your code!

  • You will have access to your mother class through the reserved word base. https://msdn.microsoft.com/pt-br/library/hfw7t1ce%28v=vs.120%29.aspx? f=255&Mspperror=-2147217396

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.

2 answers

3

There are several wrong premises in the question. The first is the misuse of the term attribute. I know, everyone teaches wrong because of the plague that is UML that uses this term. Languages do not use, does not make sense their use.

The other is that he is thinking that a camp that is in the mother class is not in the daughter or that the daughter’s camp needs to be passed on to the mother class as if the mother were something else isolated.

It’s not clear to you either what is a class and what is an object. In fact it seems that classes are isolated because it is visible in code, but they are only models. When has the object is one thing only, the models are not present in isolation, so do not have to pass anything from one to another, it is all present in the object and just use normally.

Maybe I don’t understand better What good is a builder?.

Has a example using chained constructors. Otherwise the question would need to be more specific.

using static System.Console;

public static class Program {
    public static void Main(string[] args) {
        var gato = new Gato("Dener");
        WriteLine(gato.Nome);            
    }
}

public abstract class Animal {
    public string Nome { get; private set; }
    public Animal(string nome) => Nome = nome;
}

public class Gato : Animal {
    public Gato(string nome) : base(nome) {}
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

3

From what I understand of your question, you wish to own a property Nome in your class Animal with the private Setter and want to assign its value through the child class constructor. You can use the keyword base to call the constructor of the class "mother".

public abstract class Animal

{
    public string Nome { get; private set; }

    public Animal(string nome)
    {
        Nome = nome;
    }
}

public class Gato : Animal
{
    public Gato(string nome) : base(nome)
    {

    }
}

Example:

class Program
{
    static void Main(string[] args)
    {

        var gato = new Gato("Miau");

        Console.WriteLine(gato.Nome);            
        Console.Read();
    }

}

Reference

Browser other questions tagged

You are not signed in. Login or sign up in order to post.