In development, interface programming requires that attributes be stored in Interfaces. But how can I inherit the attributes of interfaces in classes?
At the beginning, I thought that attributes can be used as long as the interface is inherited. The Code is as follows:
public interface IA { int count{get;set;} void test(); } public class A:IA { public void test() { //TODO... } } public interface IA{ int count{get;set;} void test();}public class A:IA{ public void test() { //TODO... }}
At this time, how to compile will prompt an error, see.
Check the prompt and you will know that the attribute count in IA is not implemented. Because the attribute is essentially a method, that is, the get and set methods, but the attribute looks like a field. The Code is as follows:
public class A : IA { #region count normal Property private int count_; public int count { get { return count_; } set { count_ = value; } } #endregion public void test() { //TODO... } } public class A : IA { #region count normal Property private int count_; public int count { get { return count_; } set { count_ = value; } } #endregion public void test() { //TODO... }
Here is a tip: When inheriting an interface class, you can press CTRL +. Next to the interface, and then you can quickly implement all items in the interface (including methods and attributes ). It is worth noting that there will be such code "throw new NotImplementedException ();" for methods and attributes, which requires implementation.
See Visual Studio 2010 tips for improving development efficiency
Can I add a public field to the interface so that the inherited field can also be accessed? For example:
ublic interface IA { public int sum; int count { get; set; } void test(); } public interface IA { public int sum; int count { get; set; } void test();
} An error is reported during compilation. See
The interface has the following attributes:
Interface and abstract base class. Any option class or structure that implements the interface must implement the link of all its members.
The interface cannot be directly instantiated. Its members are implemented by implementing any option class or structure of the interface.
Interfaces can contain events, indexers, methods, and attributes.
The interface does not contain the implementation of methods.
The option class or structure can implement multiple interfaces. The option class can inherit the base class and implement one or more interfaces.