The interface cannot contain fields, constructors, destructor, static members, or constants. Otherwise, errors may occur.

Source: Internet
Author: User

The interface cannot contain fields, constructors, destructor, static members, or constants. Otherwise, errors may occur.

Define an excuse, the interface encapsulates the length and width of the rectangle, and also contains a custom method to calculate the area of the rectangle. Define a class, inherit from this interface, and implement custom methods in this class.

1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 6 namespace Test06 7 {8 interface ImyInterface 9 {10 /// <summary> 11 /// 12 // </summary> 13 int Width14 {15 get; 16 set; 17} 18 /// <summary> 19 /// width 20 /// </summary> 21 int Height22 {23 get; 24 set; 25} 26 /// <summary> 27 // calculate the rectangle Area 28 /// </summary> 29 int Area (); /// // int Area (int Width, int Height); 30} 31 class Program: ImyInterface // inherited from interface 32 {33 private int width = 0; 34 private int height = 0; 35 // <summary> 36 // length 37 /// </summary> 38 public int Width39 {40 get41 {42 return width; 43} 44 set45 {46 width = value; 47} 48} 49 // <summary> 50 // width 51 // </summary> 52 public int Height53 {54 get55 {56 return height; 57} 58 set59 {60 height = value; 61} 62} 63 // <summary> 64 // calculate the rectangle Area 65 // </summary> 66 public int Area (int Width, int Height) 67 {68 return Width * Height; 69} 70 static void Main (string [] args) 71 {72 Program program = new Program (); // instantiate the Program Class Object 73 ImyInterface imyinterface = program; // use the derived class Object Instantiation interface ImyInterface74 imyinterface. width = 5; // assign 75 imyinterface to the Width attribute in the derived class. height = 3; // assign a value to the Height attribute in the derived class 76 Console. writeLine ("the area of the rectangle is:" + imyinterface. area (3, 5); 77} 78} 79}

The following is correct:
1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 6 namespace Test06 7 {8 interface ImyInterface 9 {10 /// <summary> 11 /// 12 // </summary> 13 int Width14 {15 get; 16 set; 17} 18 /// <summary> 19 /// width 20 /// </summary> 21 int Height22 {23 get; 24 set; 25} 26 // <summary> 27 // calculate the rectangle Area 28 // </summary> 29 int Area (); 30} 31 class Program: imyInterface // inherited from interface 32 {33 int width = 0; 34 int height = 0; 35 /// <summary> 36 /// length 37 /// </summary> 38 public int Width39 {40 get41 {42 return width; 43} 44 set45 {46 width = value; 47} 48} 49 // <summary> 50 // width 51 // </summary> 52 public int Height53 {54 get55 {56 return height; 57} 58 set59 {60 height = value; 61} 62} 63 // <summary> 64 // calculate the rectangle Area 65 // </summary> 66 public int Area () 67 {68 return Width * Height; 69} 70 static void Main (string [] args) 71 {72 Program program = new Program (); // instantiate the Program Class Object 73 ImyInterface imyinterface = program; // use the Object Instantiation interface of the derived class ImyInterface74 imyinterface. width = 5; // assign 75 imyinterface to the Width attribute in the derived class. height = 3; // assign a value to the Height attribute in the derived class 76 Console. writeLine ("the area of the rectangle is:" + imyinterface. area (); 77} 78} 79}

The key error point of this question is: The method declaration form in the interface should be consistent with the method definition form in the interface implementation; otherwise, an error will occur:"

Error 1 the "Area" method does not have any overload using "2" Parameters"

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.