代码之家  ›  专栏  ›  技术社区  ›  Stradigos

简单继承问题

  •  0
  • Stradigos  · 技术社区  · 14 年前

    我有几个错误说:

    “名称'title'在其当前上下文中不存在” “当前上下文中不存在'author'名称” “当前上下文中不存在'genre'名称” “名称'pages'在其当前上下文中不存在”

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace ReadingMaterials
    {
        class Program
        {
            static void Main(string[] args)
            {
    
            }
    
            public class Basic
            {
                protected string Title;
                protected string Author;
                protected string Genre;
                protected int Pages;
    
                public Basic(string title, string author, string genre, int pages)
                {
                    Title = title;
                    Author = author;
                    Pages = pages;
                    Genre = genre;
                }
    
                public int PageCount
                {
                    get { return Pages; }
                    set { Pages = value; }
                }
    
                public string GenreType
                {
                    get { return Genre; }
                    set { Genre = value; }
                }
    
                public string AuthorType
                {
                    get { return Author; }
                    set { Author = value; }
                }
    
                public string TitleName
                {
                    get { return Title; }
                    set { Title = value; }
                }
            }
    
            public class Book : Basic
            {
                protected bool Hardcover;
    
                public Book(bool hardcover) 
                    : base(title, author, genre, pages)
                {
                    Hardcover = hardcover;
                }
    
                public bool IsHardcover
                {
                    get { return Hardcover; }
                    set { Hardcover = value; }
                }
            }
    
    
        }
    }
    

    我错过了什么?提前谢谢。

    2 回复  |  直到 14 年前
        1
  •  13
  •   Dean Harding    14 年前

    在你的构造器中 Book ,您希望标题、作者、流派和页面使用什么值?您希望将它们传递给构造函数吗?如果是,则需要修改book构造函数,使其看起来像这样:

    public Book(string title, string author, string genre, int pages, bool hardcover)
        : base(title, author, genre, pages)
    {
        Hardcover = hardcover;
    }
    
        2
  •  0
  •   Brian R. Bondy    14 年前

    还必须将成员变量传递给派生类,然后使用这些变量初始化基类。