(Fields are private variables, and common attributes are attributes)
An object is a self-contained entity, which is identified by a set of identifiable characteristics and behaviors.
Class: an abstract set of objects with the same attributes and functions
(The first letter of a class name must be capitalized. If multiple words are used, the first letter of each word must be capitalized)
The constructor or constructor initializes the class. The constructor has the same name as the class and has no return value,
Void is not required. It is called in new mode.
(All classes have constructor methods. If not encoded, the system generates an empty constructor by default,
If you have a defined constructor, the default constructor will be invalid)
Method overloading provides the ability to create multiple methods of the same people, but these methods need to use different parameter types.
(The method name is the same, but the parameter type or number is different)
When protected is inherited, The subclass can have full access to the base class. If the subclass inherits the parent class, the subclass has
Attributes and functions except private. (Public to subclass, but not to other classes)
Protected is only used by the inherited subclass. When a member of the parent class is called, the base keyword A7. inheritance has the advantage that inheritance puts the common parts of all child classes in the parent class, so that Code Shared to avoid duplication. In addition, inheritance makes it easier to modify or extend the inherited implementations.
If you want to modify the function without inheritance, you must modify the function in all repeated methods. The more code, the more likely the error will be.
Inheritance has disadvantages. If the parent class is changed, the subclass has to be changed. Inheritance increases the coupling between two classes.
(Is-A: inheritance can be considered when a class is a special type of another class. Has-A is not suitable for inheritance when a role has a certain responsibility, if an airport has an airplane, the plane cannot inherit the airport.) A8. polymorphism indicates that different objects can perform the same action, but they must execute the same action through their own implementation code.
1. Child classes appear as parent classes. 2. Subclass is implemented in its own way. 3. When a subclass appears as a parent class, its unique attributes and methods cannot be used.
In order for the subclass instance to completely replace the class member from the parent class, the parent class must declare the member as virtual. Only by adding the virtual keyword before the returned type of the member. Subclass can use the override keyword to replace the parent class implementation with its own implementation. This is the method to override or override the method.
Different objects can execute the same action, but they must be executed through their own implementation code.
The principle of polymorphism is that when a method is called, no matter whether the object is converted to its parent class or not, only the method implementation at the end of the Object Inheritance chain will be called. Virtual methods are dynamically bound and called Based on the runtime type rather than the compile-time type.
Polymorphism: the virtual method body in the parent class is defined, and the Child class is implemented through override.
(Different subclasses can be implemented in different ways)
A9. both the restructured class name and the constructor name inherit the parent class (where the constructor inherits the method name: Base (), and then rewrite the implemented method in the subclass). The called methods are the same, however, the implementation effect can reach different A10. the abstract class changes the parent class (such as animal) that has no significance in instantiation to the abstract class. Classes and methods can be declared as abstract, namely abstract classes and abstract methods.
The abstract method does not have a method body. A semicolon is directly added after the brackets;
1) abstract classes cannot be instantiated. 2) abstract methods must be overwritten by the quilt class. 3) if the class contains abstract methods, the class must be defined as an abstract class, whether or not the common method a11. interface interfaces combine implicit public methods and attributes to encapsulate a set of specific functions. Once an interface is implemented, the class can support all attributes and members specified by the interface. The Declaration interface syntax is the same as the Declaration abstract class, but it cannot provide the execution methods of any member of the interface. (Apis cannot be instantiated, constructor methods and fields, modifiers such as public and private, and virtual or static declarations ). The class that implements the interface must implement all methods and attributes in the interface.
Abstract class abstract members can be partially implemented by the quilt class. Interface members must fully implement the class. A class can only inherit one abstract class, but can implement multiple interfaces.
First, the class is the abstraction of objects, the abstract class is the abstraction of classes, and the interface is the abstraction of behavior;
Second, if the behavior spans objects of different classes, you can use interfaces. For some similar class objects, use the inherited abstract class;
Third, from the design point of view, abstract classes discover common things from sub-classes, generalize the parent class, and then inherit the parent class from the sub-classes. Interfaces do not know the existence of the sub-classes, the implementation of the method is unknown and pre-defined.
(Abstract classes are abstracted from the bottom, and interfaces are designed from the top down.) A12. the size of the Set array is fixed, the arraylist capacity can be automatically expanded as needed.
(Arraylist is system. part of collections, using an array that can be dynamically increased as needed to implement the ilist Interface) a13. generic declaration of generic variables, such as: ilist <animal> arrayanimal; (Both ilist and list can be used)
Arrayanimal = new list <animal> ();
Arrayanimal. Add (new CAT ("Xiaohua");... A7. inherit
Class animal
{
Protected string name = "";
Public animal (string name)
{This. Name = Name ;}
Public animal ()
{This. Name = "anonymous ";}
Protected int shoutnum = 3;
Public int shoutnum
{
Get {return shoutnum ;}
Set {shoutnum = value ;}
}
}
Class Cat: Animal
{
Public CAT (): Base ()
{}
Public CAT (string name)
{}
Public String shout ()
{
String result = "";
For (INT I = 0; I <shoutnum; I ++)
{Result + = "meow ,";}
Return "my name is" + name + "" + result;
}
}
Class dog: Animal
{
Public dog (): Base ()
{}
Public dog (string name)
{}
Public String shout ()
{
String result = "";
For (INT I = 0; I <shoutnum; I ++)
{Result + = "Wang ,";}
Return "my name is" + name + "" + result;
}
} A8. Polymorphism
Class animal
{
Public Virtual string shout () // virtual method, which can be overwritten by the quilt class
{Return "";}
}
Class Cat: Animal
{
Public CAT (): Base ()
{}
Public CAT (string name): Base (name)
{}
Public override string shout ()
{
String result = "";
For (INT I = 0; I <shoutnum; I ++)
{
Result + = "meow ,";
}
Return "my name is" + name + "" + result;
}
}
Class dog: Animal
{
Public dog (): Base ()
{}
Public dog (string name): Base (name)
{}
Public override string shout ()
{
String result = "";
For (INT I = 0; I <shoutnum; I ++)
{
Result + = "Wang ,";
}
Return "my name is" + name + "" + result;
}
} Private animal [] arrayanimal;
Private void button#click (Object sender, eventargs E)
{
Arrayanimal = new animal [5];
Arrayanimal [0] = new CAT ("Xiaohua ");
Arrayanimal [1] = new dog ("Tom ");
Arrayanimal [2] = new dog (" ");
Arrayanimal [3] = new CAT ("Jiaojiao ");
Arrayanimal [4] = new CAT ("Mimi ");
}
Private void button2_click (Object sender, eventargs E)
{
// The call may be different due to polymorphism; Program The system automatically finds the object of item, such as cat and dog.
Foreach (animal item in arrayanimal)
{MessageBox. Show (item. Shout ());}
} A10. abstract class
Abstract class animal
{
Protected abstract string getshoutsound (); // No method body, add points directly
}
// The class must be an abstract class as long as there are abstract methods in the class;
// To inherit an abstract class, you must implement the abstract class method (that is, rewrite)
A11. Interface
Interface ichange
{
String changething (string thing); // returns the sting type.
}
Class machinecat: CAT, ichange // inherits cat and implements the ichange Interface
{
Public machinecat (): Base ()
{}
Public machinecat (string name): Base (name)
{}
Public String changething (string thing) // method of implementing the interface
{Return base. Shout () + "I have a universal pocket, And I Can variable it out:" + thing ;}
}
Private void button#click (Object sender, eventargs E)
{
Machinecat MCAT = new machinecat (" ");
Stonemonkey Wukong = new stonemonkey ("Sun Wukong"); ichange [] array = new ichange [2];
Array [0] = MCAT;
Array [1] = Wukong; // use polymorphism to achieve different changething
MessageBox. Show (array [0]. changething ("various things! "));
MessageBox. Show (array [1]. changething ("various things! "));
} Design Pattern 380 delegate and event eye protection color value:
Tone 85
Saturation 90
Brightness 205