首页 > 解决方案 > 为什么我的继承不起作用?子对象删除了我的 ToString() 方法。C#

问题描述

我有 3 个类,第一个是一个名为 Equipos 的抽象类,具有 ToString() 抽象方法,第二个称为 Camiones,它覆盖 ToString() 并显示属性“marca”和“dominio”,第三个是称为 Gruas 并且是第二个 (Camiones) 的子级,它还具有 ToString() 方法并使用 override 向其添加更多数据,包括来自第二个类的数据。

这是Camiones类:

public class Camiones : Equipos
{
    #region atributos
    protected string marca;
    protected string dominio;
    #endregion

    #region constructores
    public string Marca
    {
        get { return marca; }
        set
        {
            marca = value;
        }
    }
    public string Dominio
    {
        get { return dominio; }
        set
        {
            dominio = value;
        }
    }
    #endregion

    #region metodos 
    public override string ToString()
    {
        string cadena = "Dominio Camion: " + this.dominio + "Marca Camion: " + this.marca;
        return cadena;
    }
}

这是 Equipos 的孩子,叫做 Gruas:

public class Grua : Camiones
{
    #region atributos
    protected int altura;
    protected int peso;
    protected double precioHora;
    #endregion

    #region constructores
    public int Altura
    {
        get { return altura; }
        set
        {
            altura = value;
        }
    }
    public int Peso
    {
        get { return peso; }
        set
        {
            peso = value;
        }
    }
    public double PrecioHora
    {
        get { return precioHora; }
        set
        {
            precioHora = value;
        }
    }
    public override string ToString()
    {
        string cadena = base.ToString();
        cadena += " - Peso Grua: " + peso + " Altura Grua: " + altura + " Precio Hora: " + precioHora + " Marca Grua: " + this.marca;
        return cadena;
    }

我已经做了一个 MessageBox 来显示 Camiones 类是否使用 ToString() 正常,并且它确实有效。但是当我在我的第三类 Gruas 上调用它时,来自 Camiones 的 ToString() 只显示“Dominio Camion: -不显示属性 dominio - Marca Camion:” -显示我为 Gruas 设置的属性 marca,但它应该显示我为 Camiones 设置的属性 marca!-

似乎我的第三堂课正在删除我为其父级 Camiones 设置的属性......我不知道为什么会这样

这就是我在主窗体上设置属性的方式:

Camiones nuevoCamion; nuevoCamion = 新 Camiones();

Grua nuevaGrua;
nuevaGrua = new Grua();

nuevoCamion.Marca = Convert.ToString(cbCamionMarca.SelectedItem);
nuevoCamion.Dominio = Convert.ToString(tCamionDominio.Text);

MessageBox.Show(nuevoCamion.Marca + " " + nuevoCamion.Dominio); //it shows everything it should show
MessageBox.Show(nuevoCamion.ToString()); //it also shows everything it should show

nuevaGrua.Marca = Convert.ToString(cbGruaTipo.SelectedItem);
nuevaGrua.Peso = Convert.ToInt32(mtPesoMaximo.Text.Trim());
nuevaGrua.Altura = Convert.ToInt32(mtAlturaIzaje.Text.Trim());
nuevaGrua.PrecioHora = Convert.ToDouble(mtPrecioGrua.Text);

parListaGruas.Add(nuevaGrua);
MessageBox.Show(parListaGruas[0].ToString()); //only shows Dominio Camion: with no data, and Camion Marca: with marca from Gruas. And it shows the rest of the Gruas.ToString() ok- This is where im having issues
MessageBox.Show(nuevoCamion.ToString()); //i called my nuevoCamion again to check if the data was still there, and its still there.

标签: c#formsclassobjecttostring

解决方案


推荐阅读