码迷,mamicode.com
首页 > Windows程序 > 详细

C#函数拓展(EduCoder实训题目)

时间:2020-06-15 16:01:52      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:委托   cti   ice   open   str   重载   div   reading   space   

第1关:结构函数

技术图片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace H1
{

    class Program
    {
        /********** Begin *********/
        struct book
        {
            public string author;
            public string bookName;
            public double price;
            public int yearOfPublication;
            public void setAuthor(string s)
            {
                author = s;
            }
 
            public void setBookName(string s)
            {
                bookName = s;
            }
 
            public void setPrice(double s)
            {
                price = s;
            }
 
            public void setYearOfPublication(int s)
            {
                yearOfPublication = s;
            }
 
            public string getInformation()
            {
                string info = "author:" + author + ",price:" + price + ",yearOfPublication:" + yearOfPublication
                    + ",bookeName:" + bookName;
                return info;
            }
        }
        /********** End *********/

        static void Main(string[] args)
        {
            book orderBook = new book();

            orderBook.setAuthor("Cixin Liu");
            orderBook.setBookName("The three body problem");
            orderBook.setPrice(40);
            orderBook.setYearOfPublication(2006);

            Console.WriteLine(orderBook.getInformation());

        }
    }
}
View Code

第2关:函数的重载

技术图片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace H2
{
    class Program
    {
        struct book
        {
            string author;
            int price;
            int yearOfPublication;
            string bookName;

            public string getAuthor()
            {
                return author;
            }
            public void setAuthor(string _author)
            {
                author = _author;
            }
            public int getPrice()
            {
                return price;
            }
            public void setPrice(int _price)
            {
                price = _price;
            }
            public int getYearOfPublication()
            {
                return yearOfPublication;
            }
            public void setYearOfPublication(int _yearOfPublication)
            {
                yearOfPublication = _yearOfPublication;
            }
            public string getBookName()
            {
                return bookName;
            }
            public void setBookName(string _bookName)
            {
                bookName = _bookName;
            }

            public void showInformation()
            {
                Console.WriteLine("author:" + author);
                Console.WriteLine("book name:" + bookName);
                Console.WriteLine("price:" + price);
                Console.WriteLine("year of publication:" + yearOfPublication);
            }

            public void setParam(book _bk)
            {
                author = _bk.getAuthor();
                price = _bk.price;
                yearOfPublication = _bk.getYearOfPublication();
                bookName = _bk.getBookName();
            }

            /********** Begin *********/
            //your function
            public void setParam(string _author, int _price, int _yearOfPublication, string _bookName)
            {
            author = _author;
            price = _price;
            yearOfPublication = _yearOfPublication;
            bookName = _bookName;
            }
            /********** End *********/
        }

        static void Main(string[] args)
        {

            book yourBook = new book();

            //book myBook = new book();
            //myBook.setAuthor("Hemingway");
            //myBook.setBookName("The Old Man and the Sea");
            //myBook.setPrice(30);
            //myBook.setYearOfPublication(1952);
            //yourBook.setParam(myBook);

            /********** Begin *********/
            //Use your functions instead of comments section
            yourBook.setParam("Hemingway", 30, 1952, "The Old Man and the Sea");
            /********** End *********/
            yourBook.showInformation();

        }
    }
}
View Code

第3关:委托

技术图片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace H3
{
    class Program
    {
        /********** Begin *********/
        //delegate
        delegate string myRead();
        static void Apply(myRead r)
        {
            string str = r();
            Console.WriteLine("my words:" + str);
        }
        //Apply function

        /********** End *********/
        static void Main(string[] args)
        {

            Apply(Console.ReadLine);

        }
    }
}
View Code

C#函数拓展(EduCoder实训题目)

标签:委托   cti   ice   open   str   重载   div   reading   space   

原文地址:https://www.cnblogs.com/QQ1723545340/p/13131153.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!