Chapter 4
1. Interface
public interface ITty { void Put(); }
A. Generally, an interface can only contain methods, attributes, indexers, and event declarations. It is not allowed to provide implementation methods for any member of the interface. It only declares that no implementation is implemented.
B. There cannot be constructor, and members in the interface cannot have access modifiers, because the interface is originally public ),
C. As long as a class inherits the interface, it must provide implementation for all methods in the interface.
2. Hide Methods
If the method with the same signature is declared in both the base class and the derived class, but this method is not declared as virtual and override, the derived class method will hide the base class method. In most cases, the method should be rewritten, rather than hidden, because hiding the method will pose a risk of calling an error method for the instance of the given class.
Public class HisBaseC1ass {public String mypolicymeth () {return ("Base Class") ;}} public class MyDerivedC1ass: HisBaseC1ass {public String mypolicymeth () {return ("My Derived C1ass") ;}} MyDerivedC1ass derived = new MyDerivedC1ass (); Console. writeLine (derived. mypolicymeth (); // The output is "My Derived C1ass" // calls the subclass's own, not inherited from the parent class, that is, the subclass overwrites the methods in the parent class. The correct and clear method is to use the new keyword, // the above method. The compiler will think that it is implicit to overwrite the public class MyDerivedC1ass: hisBaseC1ass {public new String mypolicymeth () {return ("My Derived C1ass ");}}
3. abstract class
Public abstract class pretty tty {public void Get () {} public abstract void Push (); [No method body ]}
A. this class cannot be instantiated. In addition to declaring related methods and attributes like other common classes, this class can also declare abstract methods. There is no method body for abstract methods, the derived class must use override to override the inherited abstract method.
B. Differences between abstract and Virtual Methods
1) The parent class can only declare virtual methods, but does not provide implementation. It can also provide implementation. [Note: All methods have a method body]
Public class Animal {public virtual void Sleep () {console. writeline (...);} // provides public virtual void Eat () {}// implementation not provided}
2) The subclass must use override to override all the inherited abstract methods. For virtual methods, override can be used to override them, or you do not need to override them.
4. define and implement interfaces
Public interface IBankAccount {void PayIn (); void Withdraw ();} public class A: IBankAccount {void PayIn (){...} void Withdraw (){...}} public class B: IBankAccount {void PayIn (){...} void Withdraw (){...}}
IBankAcoount a = neW ();
IBankAccount B = neW B ();
// The Declaration Form of the and B interface variables indicates that they can point to any instance of any class implementing this interface. In this case, a and B can only inherit Class A and all the methods inherited from the IBankAcoount interface in Class B, when Class B includes other Members defined by itself, a and B cannot be accessed.
5. derived Interfaces
Interfaces can inherit from each other, while interfaces in C # can inherit from each other.
Public interfaCe ITransferBankACcount: IBankAccount {bool TransferTo (IBankACcount destinatio, decima amount );}
// Because ITransferBankACcount is derived from IBankAccount, it owns all the members of IBankAccount and its own members. This means that any class derived from ITransferBankACcount must implement all IBankAccount methods and the new method TransferTo defined in ITransferBankAccomt.