简体   繁体   中英

Invalid Arguments (Interface C#)

in the current code i receive a error in line 174 to 188 because the same error (the best overloaded method match for 'Relatorio.AdicionarItem' has some invalid arguments).

Why a created class is invalid in this situation and how i can fix it?

In that case the program is rejecting the classes a1 , p1 and t1 .

Thanks!

using System;
using System.Collections.Generic;

public interface IImprimivel
{
  void Imprimir();
}

public abstract class Pessoa
{
  protected string _nome;

  public string Nome
  {
    get
    {
      return this._nome;
    }
  }
}

public class Aluno : Pessoa
{
  private string _matricula;

  public Aluno(string nome, string matricula)
  {
    this._nome = nome;
    this._matricula = matricula;
  }

  public string Matricula
  {
    get
    {
      return this._matricula; 
    }
    set
    {
      _matricula = value;
    }
  }
}

public class Professor : Pessoa
{
  private string _email;

  public string Email
  {
    get
    {
      return this._email;
    }
  }

  public Professor(string nome, string email)
  {
    this._nome = nome;
    this._email = email;
  }
}

public class Turma
{
  private string _nome;
  private string _serie;
  private List<Aluno> Estudantes = new List<Aluno>();

  public string Nome
  {
    get
    {
      return this._nome;
    }
  }

  public string Serie
  {
    get
    {
      return this._serie;
    }
  }

  public Turma(string nome, string serie, Professor x, Professor y)
  {
    this._nome = nome;
    this._serie = serie;
  }

  public void AdicionarAluno(Aluno x)
  {
    Estudantes.Add(x);
  }

}

public class Relatorio
{
    private string _titulo;
    private string _descricao;
    private List<IImprimivel> _itens;

    public string Titulo
    {
        get
        {
            return this._titulo;
        }
    }

    public string Desricao
    {
        get
        {
            return this._descricao;
        }
    }

    public Relatorio(string titulo, string descricao)
    {
        this._descricao = descricao;
        this._titulo = titulo;
        this._itens = new List<IImprimivel>();
    }

    public void AdicionarItem(IImprimivel item)
    {
        this._itens.Add(item);
    }

    public void ImprimirRelatorio()
    {
        
        Console.WriteLine("\n======== RELATÓRIO ========");
        Console.WriteLine(this._titulo);
        Console.WriteLine("===========================");
        Console.WriteLine(this._descricao);
        Console.WriteLine("========== ITENS ==========");
        foreach (var item in _itens)
        {
            item.Imprimir(); 
            Console.WriteLine("---------------------------");
        }
        Console.WriteLine("===========================");
    }

    class MainClass {
  public static void Main (string[] args) {

    Aluno a1 = new Aluno("Jo", "A78265139");
    Aluno a2 = new Aluno("Lala", "A27346133");
    Aluno a3 = new Aluno("Lis", "A21312312");
    Aluno a4 = new Aluno("Lô", "A457238423");
    Aluno a5 = new Aluno("Titi", "A382735922");
    Aluno a6 = new Aluno("Juca", "A018367541");

    Professor p1 = new Professor("Anaxarmandra", "anax@email.com");
    Professor p2 = new Professor("Hermenegildo", "herm@email.com");
    Professor p3 = new Professor("Benevides", "bene@email.com");

    Turma t1 = new Turma("101", "1° série", p1, p2);
    t1.AdicionarAluno(a1);
    t1.AdicionarAluno(a2);
    t1.AdicionarAluno(a3);

    Turma t2 = new Turma("201", "2° série", p2, p3);
    t2.AdicionarAluno(a4);
    t2.AdicionarAluno(a5);
    t2.AdicionarAluno(a6);

    Relatorio rAlunos = new Relatorio("Alunos", "Relatório de Alunos");
    rAlunos.AdicionarItem(a1);
    rAlunos.AdicionarItem(a2);
    rAlunos.AdicionarItem(a3);
    rAlunos.AdicionarItem(a4);
    rAlunos.AdicionarItem(a5);
    rAlunos.AdicionarItem(a6);

    Relatorio rProfessores = new Relatorio("Professores", "Relatório de Professores");
    rProfessores.AdicionarItem(p1);
    rProfessores.AdicionarItem(p2);
    rProfessores.AdicionarItem(p3);

    Relatorio rTurmas = new Relatorio("Turmas", "Relatório de Turmas");
    rTurmas.AdicionarItem(t1);
    rTurmas.AdicionarItem(t2);

    rAlunos.ImprimirRelatorio();
    rProfessores.ImprimirRelatorio();
    rTurmas.ImprimirRelatorio();


  }
}

}

AlunoProfessor都不是从接口IImprimivel继承的,因此这些类的对象不能传递给需要IImprimivel类型参数的IImprimivel

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