简体   繁体   中英

C# Homework. I am looking for an explanation of what is going on

Forgive me for asking what some might think are stupid questions. I am trying to read the code below and make sense out of it.

Why is novel.title used in the main. I understand where I get the title from but why does novel.title used. The other one is what is holding the title information that was entered by the user. Same questions with author, and level.

 using System;
 using System.Collections.Generic;
 using System.Text;

 namespace assignment6
{
public class Book
{
    protected string title;
    public string Title
    {
        get
        {
            return title;
        }
        set
        {
            title = value;
        }
    }
    protected string author;
    public string Author
    {
        get
        {
            return author;
        }
        set
        {
            author = value;
        }
    }
    public void showBook()
    {
        Console.WriteLine("The Book you entered is " + title + " by " + author);
    }
    public Book(string booktyp1, string booktyp2)
    {
        Console.WriteLine("You will enter data for a " + booktyp1 + " book indicating the " + booktyp2 + " book.");
        Console.WriteLine();
    }
}

public class Fiction : Book
{
    private int level;
    public int Level
    {
        get
        {
            return level;
        }
        set
        {
            level = value;
        }
    }
    public void showFiction()
    {
        showBook();
        Console.WriteLine("Reading level is " + level);
    }
    public Fiction()
        : base("Fiction", "reading level")
    {
    }
}

public class NonFiction : Book
{
    private int pages;
    public int Pages
    {
        get
        {
            return pages;
        }
        set
        {
            pages = value;
        }
    }
    public void showNonFiction()
    {
        showBook();
        Console.WriteLine(pages + " pages");
    }
    public NonFiction()
        : base("NonFiction", "Number of pages")
    {
    }
}

public class assignment6
{
    public static void Main(string[] args)
    {
        char choice;

        Console.Write("Do you want Fiction (F) or Non-Fiction (N)? -->");
        choice = Convert.ToChar(Console.ReadLine().ToUpper());
        switch (choice)
        {
            case 'F':
                {
                    Fiction novel = new Fiction();
                    Console.Write("Enter the book title-->");
                    novel.Title = Console.ReadLine();
                    Console.Write("Enter the book author-->");
                    novel.Author = Console.ReadLine();
                    Console.Write("Enter the reading level-->");
                    novel.Level = Convert.ToInt32(Console.ReadLine());
                    novel.showFiction();
                    break;
                }
            case 'N':
                {
                    NonFiction manual = new NonFiction();
                    Console.Write("Enter the book title-->");
                    manual.Title = Console.ReadLine();
                    Console.Write("Enter the book author-->");
                    manual.Author = Console.ReadLine();
                    Console.Write("Enter the number of pages-->");
                    manual.Pages = Convert.ToInt32(Console.ReadLine());
                    manual.showNonFiction();
                    break;
                }
            default:
                {
                    Console.WriteLine("You made an inappropriate entry -- closing program");
                    break;
                }
        }
    }

}

}

I am sure everyone here know how new I am to programming. I can say this I love it when it works, I hate it when it doesnt and makes me want to quiet.

Title is a property. It actually manages field called title. The value of title is used in the Show... metod.

novel is a Fiction object. Since Fiction inherits from Book, Fiction has a Title and Author property. So what

novel.Title = Console.ReadLine();

is doing is reading input from the console and storing it in the title attribute of your novel object through the Title property.

You should take a look at this Properties Tutorial .

Title , Author , and Level are attributes of the novel object created at the beginning of the block. The attributes of an object cannot be referenced without a reference to the object itself.

novel is an object, you access properties of objects such as Title, Author, and level.

Put it into persepect:

You have a novel and you are asking the user what is the Title of the novel (novel.Title). Then you ask the user: who is the Author of the novel (novel.Author). Finally, you ask for the Level (novel.Level), (which I'm guessing is like novel difficulty to read) of the novel.

The novel.Title is specifically referring to the Title in the novel object (the one created by Fiction novel = new Fiction(); . Say that more Fiction objects were created:

Fiction novel1 = new Fiction();
Fiction novel2 = new Fiction();
Fiction novel3 = new Fiction();

You'd have three Title fields, but how would you indicate which one you're referring to? That's what the prefix is for. In the above example, for instance, you could refer to the Title in novel3 by using novel3.Title .

Your question is a little unclear, but I think you're asking about the difference between novel.title and novel.Title and why one is used over the other.

novel.title is a field that can store some data (in this case, a string of text). novel.Title is a property that provides access to that field. The property itself cannot store any data, only the field can. Fields are considered to be part of the implementation details of the class and it's considered to use a bad idea to directly expose that to the user code. The property is to give the programmer the freedom to later change the implementation without breaking compatibility - it could be as simple as renaming the field or it might be changed to do something else entirely.

The

 protected string title;

cant be accessed directly, but can access through :

public string Title
{
    get
    {
        return title;
    }
    set
    {
        title = value;
    }
}

Notice title different Title here, You can novel.Title but no novel.title

Book is the base/super class where derived class are Fiction and NonFiction

Properties of Book are inherited by the two derived class

Two Derived Class declare their own properties.

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