Review of grammar based on examples

example

① Library interface (Library)
Member method:
borrow a Book
revert(), return the book
② Books (Book)
Member variables:
Book name (name) string type
publisher string type
Construction method:
Initialize book name and publisher through formal parameters
General method:
(1) Set the get and set methods to get and set the value of the name variable in the class;
(2) Override the Equals method. If and only if the book name and publisher are equal, it is the same book.
(3) Override the ToString method to return the information of the book name and publisher. The style is as follows:
"Title: Java programming, press: Tsinghua University Press"
③ The CollectionBook class inherits from the Book class and implements the Library interface
Member variables:
Book number (bNo) string type
Stack string type
Whether to borrow (isBorrow) bool type books. When the status is borrowed, the value is true
Construction method:
Call the parent class constructor to initialize the book name (name) and publisher information, and then initialize the book number (bNo) and line book stacks.
General method:
(1) Implement the borrow method in the interface
If the book status is borrowed, output "sorry, the book has been borrowed". Otherwise, modify the book status to borrowed and output "borrowed successfully".
(2) Implement the revert method in the interface
If the book status is available for borrowing, output "the book has been returned". Otherwise, modify the book borrowing status to not borrowed, and output "returned successfully".
④ Pass the main function test
(1) Create two CollectionBook objects book1 and book2, output book1 and book2 respectively, and call its equals method to judge whether the two objects are equal.
(2) Enter an integer through the keyboard, enter 0 to borrow book1, and enter 1 to return book1.

P1

① Library interface (Library)
Member method:
borrow a Book
revert(), return the book*/

```csharp
interface ILibrary		//Interfaces generally start with I
{
    bool Borrow();		//The interface has no method body and no constructor
    bool revert();
     }
    

P2

② Books (Book)
Member variables:
Book name (name) string type
publisher string type
Construction method:
Initialize book name and publisher through formal parameters
General method:
(1) Set the get and set methods to get and set the value of the name variable in the class;
(2) Override the Equals method. If and only if the book name and publisher are equal, it is the same book.
(3) Override the ToString method to return the information of the book name and publisher. The style is as follows:
"Title: Java programming, press: Tsinghua University Press"*/

 class Book
{
    private string name;		//Fields declared in a class are properties
    private string publisher;
   public Book(string name,string publisher) 		//The method with the same method name and class name can be declared as formal parameter (formal parameter) in constructor ()
    {
        this.name = name;		//this keyword indicates that the second name of the attribute is a declared parameter
        this.publisher = publisher;//
    }
    public string Name		//Generally, it is convenient to identify the function of the method in case
    { 
        get{ return name; }		//The get set accessor gets the value of the variable name
        set{ name = value; }
    }

     public override string ToString()		//Override ToString method
    {
        return "title:" + name + publisher + "tsinghua university press ";
    }
    public override int GetHashCode()
    {
        return base.GetHashCode();
    }
    public override bool Equals(object obj)		//When overriding Equals, it should be overridden together with HashCode
    {
        Book a = obj as Book;
        return name == a.name && publisher == a.name;       
    }
}
     

P3

③ The CollectionBook class inherits from the Book class and implements the Library interface
Member variables:
Book number (bNo) string type
Stack string type
Whether to borrow (isBorrow) bool type books. When the status is borrowed, the value is true
Construction method:
Call the parent class constructor to initialize the book name (name) and publisher information, and then initialize the book number (bNo) and line book stacks.
General method:
(1) Implement the borrow method in the interface
If the book status is borrowed, output "sorry, the book has been borrowed". Otherwise, modify the book status to borrowed and output "borrowed successfully".
(2) Implement the revert method in the interface
If the book status is available for borrowing, output "the book has been returned". Otherwise, modify the book borrowing status to not borrowed, and output "returned successfully".

 class CollectionBook : Book, ILibrary		//Inherit the parent class and implement the interface
{
    string bNo;
    string stacks;
    bool isBorrow;
    public CollectionBook(string bNo, string stacks, string name, string publisher) : base(name, publisher)		//The base keyword is the calling parent class parameter
    {
        this.bNo = bNo;
        this.stacks=stacks;
        }
    public bool Borrow()
    {
        if(isBorrow)
        {
            Console.WriteLine("Yes, No,The book has been returned");
        }
        else
        {
            isBorrow = true;
            Console.WriteLine("Borrowing succeeded");
        }
        return isBorrow;    
    }
    public bool revert()
    {
        if (! isBorrow)
        {
            Console.WriteLine("The book has been returned");
        }
        else
        {
            isBorrow = true;
            Console.WriteLine("Return the book successfully");
        }
        return isBorrow;
    }

P4

④ Pass the main function test
(1) Create two CollectionBook objects book1 and book2, output book1 and book2 respectively, and call its equals method to judge whether the two objects are equal.
(2) Enter an integer through the keyboard, enter 0 to borrow book1, and enter 1 to return book1.

   static void Main(string[] args)
        {
        CollectionBook book1 = new CollectionBook("","","","");   //Just don't forget
        CollectionBook book2 = new CollectionBook("","","","");
        book1.Equals(book2);
        switch (int.Parse(Console.ReadLine()))
        {
            case 0:
                book1.Borrow();
                break;
            case 1:
                book1.revert();
                break;
            default:
                break;
        }
    }
}

summary

  clarify the logical relationship, clarify the function of each code, and understand the meaning of the definition of grammatical structure

Tags: C# Back-end

Posted on Sat, 27 Nov 2021 23:31:37 -0500 by crinkle