C#构造函数及其函数的重载

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

namespace BookApplication
{
    class TestBook
    {
        static void Main(string[] args)    //入口函数
        {
            Book bookA = new Book("0001111"); 
            bookA.m_Name="asp.net";
            bookA.m_Price = "$50.21";

            Console.WriteLine("编号为: " + bookA.m_Id + " 价格为: " + bookA.m_Price);

            Book bookB = new Book("010101","C#","张三","$20.00");
            Console.WriteLine("编号为: " + bookB.m_Id + " 书名为: "  + bookB.m_Name + " 价格为: " + bookB.m_Price+" 作者: "+bookB.m_Author);

 

            string id = "0000-0000-122";
            bookB.UpdateId(id);
            bookB.m_Author = "李四";
            bookB.m_Name = "C#";
            bookB.m_Price = "$50.00";
            Console.WriteLine("编号为: " + bookB.m_Id + " 书名为: " + bookB.m_Name + " 价格为: " + bookB.m_Price+" 作者: "+bookB.m_Author);

            Console.Read();
        }
    }

    class Book
    {
        private string id;
        private string name;
        private string author;
        private string price;

        public Book()  //默认构造器
        {
 
        }

        public Book(string id)
        {
            this.id=id;
        }

        public Book(string id, string name, string author, string price) //构造器重载
        {
            this.id = id;
            this.name = name;
            this.author = author;
            this.price = price;
        }

        public string m_Id   //只读属性
        {
           
            get { return id; }
        }

        public string m_Name
        {
            set { this.name=value; }
            get { return name; }
        }

        public string m_Author
        {
            set { this.author = value; }
            get { return author; }
        }
        public string m_Price
        {
            set { this.price = value; }
            get { return price; }
        }

        public void Updatebook(Book book)   // 修改图书Book的函数

        {
            this.m_Name = book.m_Name;
            this.m_Price = book.m_Price;
            this.m_Author = book.m_Author;
        }


        public void UpdateId(string id)   // 调用getBook(string id)方法获取到一个Book对象

        {
            Book book = Getbook(id);
            this.m_Author = book.m_Author;
            this.m_Name = book.m_Name;
            this.m_Price = book.m_Price;
        }


        public Book Getbook(string id)    // 根据图书Book的id,即编号,获取一个Book对象的方法

        {
            return new Book(id, "000000", "121212", "45646");
        }

 


    }

}

posted on 2009-06-13 13:56  aspneter  阅读(1037)  评论(0)    收藏  举报