开发者

Why does this work? Inheritance

开发者 https://www.devze.com 2023-04-06 05:02 出处:网络
No problems here, just need explanation how does that work. I was doing homework for my C# class and I managed to do it by myself by following code examples provided by our professor. The problem is

No problems here, just need explanation how does that work.

I was doing homework for my C# class and I managed to do it by myself by following code examples provided by our professor. The problem is I don't get how it works. Here are things that boggle me:

First of all, how come I need to use xmlBook.Title = "XML Primer Plus"; instead of Book clrBook = new Book("CLR via C#", ...") and vice-versa as constructors.

Second, why I don't have to have any parameters when using : base()?

Third, how does overwrite by using new public void display() only adds output, instead of completely modifying original protected void display()? I guess because the original diplay() is protected?

Please clarify

Regards.

Main.cs

using System;

namespace Lab_4
{
    class Program
    {
        static void Main(string[] args)
        {
            Book xmlBook = new Book();
            xmlBook.Title = "XML Primer Plus";
            xmlBook.AuthorFirstName = "Nicolas";
            xmlBook.AuthorLastName = "Chase";
            xmlBook.Price = 44.99F;
            xmlBook.PublisherName = "Sams Publishing";

            Book clrBook = new Book("CLR via C#", 
                                    "Jeffrey", 
                                    "Richter", 
                                    59.99f, 
                                    "Microsoft Press");

            Console.WriteLine("=== xmlBook ===");
            xmlBook.display();
            Console.WriteLine();
            Console.WriteLine("=== clrBook ===");
            clrBook.display();
        }
    }
}

Publication.cs

using System;

namespace Lab_4
{
    public class Publication
    {
        string publisherName, title;
        float price;

        public Publication()
        {
        }

        public Publication(string title, 
                           string publisherName, 
                           float price)
        {
            Title = title;
            PublisherName = publisherName;
            Price = price;
        }

        public float Price
        {
            set
            {
                price = value;
            }
        }

        public string PublisherName
        {
            set
            {
                publisherName = value;
            }
        }

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

        protected void display()
        {
            Console.Write("{0}\n{1}\n{2}\n", title, publisherName, price);
        }
    }
}

Book.cs

using System;

namespace Lab_4
{
   public class Book : Publication
    {
        string authorFirstName, authorLastName;

        public Book()
        {
        }

        public Book(string bookTitle, 
                    string firstName, 
                    string lastName, 
                    float bookPrice, 
                    string publisherName)
           : base()
        {
            Title = bookTitle;
            AuthorFirstName = firstName;
            AuthorLastName = lastName;
            Price = bookPrice;
            PublisherName = publisherName;
        }

        public string AuthorFirstName
        {
            get
            {
                return authorFirstName;
            }
            set
            {
                authorFirstName = value;
            }
        }

        public string AuthorLastName
        {
            get
            {
                return authorLastName;
            }
            set
            {
                authorLastName = value;
            }
        }

        new public void display()
   开发者_如何学C     {
            base.display();
        Console.WriteLine("{0}", getAuthorName());
        }

        string getAuthorName()
        {
            return AuthorFirstName + " " + AuthorLastName;
        }
    }
}


First of all, how come I need to use xmlBook.Title = "XML Primer Plus"; instead of Book clrBook = new Book("CLR via C#", ...") and vice-versa as constructors.

You are free to swap them if you like. Either option works, which is what that code is trying to demonstrate.

There are multiple constructors for the Book class. From code outside the class (like in your Main method), you are allowed to call any constructor of the class that is marked public.

The reason you need to set the properties when you call new Book(); is because that constructor doesn't set the properties of the class. When you call new Book("CLR via C#", "Jeffrey", "Richter", etc);, you are calling the constructor that does set the properties.

Second, why I don't have to have any parameters when using : base()?

The base class (Publication) also has two constructors. When calling base(), you are calling the constructor of Publication that takes no parameters.

When you call the constructor for a derived class, some constructor for the base class will always get called. If you don't explicitly specify which of the base class's constructors you want to call (by calling base() or base("some title", "some publisher name", 0.0f /* some price */)), then the parameterless constructor (i.e. public Publication()) will be called by default.

Third, how does overwrite by using new public void display() only adds output, instead of completely modifying original protected void display()? I guess because the original diplay() is protected?

It doesn't have anything to do with it being protected. Try changing it to public, and you'll see the same behavior.

The way it "adds" behavior is by actually calling that function in the base class. The line base.display(); calls that function. If that line weren't there, the base class's function would effectively be "replaced".

There is only one thing that protected means. It means you can't call it from outside code - you can only call it from within the same class (from within Publication) and from inside a derived class (Book). protected will not "protect" it from being overridden in derived classes (either using override or new).


1.) It's using the constructor of Book that takes no parameters hence

Book xmlBook = new Book();

The rest is just assigning public properties.

2.) base() is calling the constructor of the base class - since the Publication constructor does not have any parameters, you don't have to (and can't) pass any. Note that in this case calling base() is optional because the base class provides an empty constructor. Also see "Using Constructors" as reference.

3.) The display() method in Book is calling the display() method of the base class as part of its implementation, that's why there is additional output. Note that because the method signature is using new public void the method in the Book class is only called if you use via Book reference, usually you want to use override instead.


First Question

Publication has public Title property. Book is a Publication and so you can call

Book xmlBook = new Book();
xmlBook.Title = "XML Primer Plus";

Second Question

Publication has a constructor that doesn't have any parameters. So you can call it as base() from the Book class.

   public Publication()
    {
    }

Third Question

Display method in the Book class calls the Display method in the Publication class. Note that if you use the new modifier instead of override, the method in the derived class doesn't override the method in the base class, it merely hides it. Look at this thread for more information.


1) With xmlBook, you call the constructor without params,public Book(), and then set the individual fields.

2) In the constructor for Books with params, you could call base with some of the params, namely title, publisherName and price. An alternative (which is what you have), is to set all the fields in the Books constructor itself.

3) Books.display() adds output to Publication.display(), instead of replacing it, because it calls base.display().

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号