This class is the fourth lesson of C #, focusing on the three major features of object-oriented. The last lesson has finished the package, this lesson is about inheritance and polymorphism.
content :
1. Inheritance: When writing a program, some information is public, you can write these public information in the parent class, and enhance the reusability of the code.
(1) class A:b, the inherited class is called the base class or parent class or superclass, and the resulting class is called a subclass or derived class. Subclasses inherit the state and Behavior (fields and methods) of the superclass, and can also have their own characteristics. The parent class is a private member, and the subclass is inherited, just without access, just like a safe that doesn't know the password .
(2) A:b,b:c, then a:c.
(3) Any type of base class is of type object.
(4) When a class is a sealed class (sealed), it cannot be inherited.
(5) A:b,b inside the Sayhi (), with an instantiation of the object call Sayhi (), the first call is the construction method in the B, then a construction method, the last is Sayhi (), if there is an inherited class B above, then first call the class's construction method. Such as:
Animal class:
1 class Animal 2 {3public Animal ()4 { 5 Console.WriteLine (" I am an animal, I was born "); 6 }7 }
Person class:
1 classPerson:animal2 {3 Public intAge ;4 Public stringname;5 6 PublicPerson ()7 {8Console.WriteLine ("I'm a human being, I was born.");9 }Ten Public voidSayhi () One { AConsole.WriteLine ("Hello, I am a human being ."); - } -}
Chinese class:
1 class Chinese:person 2 {3public Chinese ()4 {5 Console.WriteLine (" I am a Chinese, I was born "); 6 }7 }
Among them: Chinese:person,person:animal.
1 class Program2 {3 Static voidMain (string[] args)4 {5Chinese C =NewChinese ();6 C.sayhi ();7 Console.readkey ();8 }9}
The result of the operation is:
(6) If a method with the same name is in the subclass and parent class, if the child class object calls the method, the method in the parent class will be hidden. You can use new to modify the method with the same name to hide the method with the same name as the parent class.
(7) A method of a parent class if you want the quilt class to overwrite, the parent class needs to be declared as the virtual type. After the subclass inherits, the method is decorated with override. As in the parent class: public virtual void Sayhi (), in the subclass: public override void Sayhi ().
(8) Richter replacement principle: Subclass objects can be assigned to the parent class object: Person p = new Chinese (), and vice versa.
(9) If a:b,a, B has a method called Sayhi (), b b = new A (), when B calls Sayhi (), by default (that is, not replicated), then the nearest principle, that is, call B's Sayhi (). If Sayhi () is overwritten, the subclass's Sayhi () is called.
2. Polymorphism: The same method name, but there are different implementation forms. Polymorphism is divided into overloads (overload) and overwrite (override).
(1) Overloading: The method name is the same, the method inside the number of parameters or different types.
(2) Only the virtual method (virtual) of the parent class can overwrite the quilt class, and the subclass override with the keyword. If you do not override the method of the parent class is hidden (add keyword new).
(3) Static methods cannot be overwritten, but can be hidden.
(4) Static class can only have static members.
(5) Static members cannot take override,virtual,abstract.
3. Abstract class, abstract method: Abstract + class Name, abstract + method name
(1) Abstract classes cannot be instantiated.
(2) The function of the abstract method is to provide a uniform interface for the subclass, the abstract method is only declared, not implemented.
(3) Once a class inherits an abstract class, it must implement the implementation of all the abstract methods within that abstract class.
(4) An abstract class can have non-abstract methods, but once a class has an abstract method, the class is an abstract class.
(5) Abstract method with one after declaration, end, do not need to use {}.
(6) The abstract method does not need to be declared as a virtual method at the time of overwrite.
1 Public Abstract class Program2 {3 Public Abstract voidA ();4 Public Abstract voidB ();5 }6 Public classC:Program7 {8 Public Override voidA ()9 {TenConsole.WriteLine (" China"); One } A Public Override voidB () - { -Console.WriteLine ("Hello"); the } - Public Static voidMain () - { -C me =NewC (); + me. A (); - } +}
4. Interface: Interface + class name
(1) The interface is used to implement multiple inheritance, a class can have only one parent class, but can inherit multiple interfaces.
(2) The interface is a special kind of abstract class, it is more strict in the rules, the method can only be abstract method.
(3) When defining an interface, it is generally the default, that is, the Interface+ class name, and the front automatically adds the public abstract. When you define a method in an interface, you only need to write the return type + method name, and the default is public abstract.
(4) All members of the interface are public, and the members of the abstract class can be private, public, and protected.
(5) When a class inherits from an interface, it must complete the implementation of all the methods in the interface.
(6) An interface cannot contain fields, constructors, static variables, or constants.
5. Generics: In order to avoid the unboxing process, the generic type is introduced. It declares the data type that is mounted, allowing only this type of data to be joined. In the System.Collections.Generic namespace.
Non-generic collection classes are compared to generic collection classes: ArrayList---list<t>;hashtable---dictionary<t>;stack---stack<t>.
PostScript : study under the class.
Asp. NET Dynamic Web site production (+)--C # (4)