C # encapsulation, polymorphism, abstraction, interfaces, anonymous methods, etc,

Source: Internet
Author: User

C # encapsulation, polymorphism, abstraction, interfaces, anonymous methods, etc,

1: Encapsulation

Encapsulating an object does not completely package the entire object. Instead, you can set certain access permissions based on actual needs. Users can call the functions provided by the object based on different permissions, in C #, you can use the modifiers public, internal, protected, and private to modify the fields, attributes, and methods of the class.

 

2: inheritance. Pay attention to the inheritance format.

 

[Csharp]View plaincopy
  1. Public class ParentClass // parent class
  2. {
  3. Public ParentClass ();
  4. }
  5. Public class ChildClass: ParentClass: subclass
  6. {
  7. Public ChildClass ();
  8. }

 

 

3: Polymorphism

Polymorphism is one of the core concepts of object-oriented programming. polymorphism means that when some subclasses are derived from the parent class, each subclass is implemented by different codes. Therefore, the same call can produce different effects, mainly determined by the subclass. When the program runs, the object-oriented language automatically determines the derived type of the object and calls the corresponding method.

Polymorphism in C # can be divided into two types: Polymorphism during compilation and Runtime polymorphism. polymorphism during compilation is implemented through overloading, the running polymorphism is the operation that the system determines based on the actual situation.

Instance code:

 

[Html]View plaincopy
  1. Public class MyIntertest
  2. {
  3. Public virtual void interest ()
  4. {
  5. Console. WriteLine ("My hobbies ");
  6. Console. ReadLine ();
  7. }
  8. }
  9. Public class Writer: MyIntertest
  10. {
  11. Public override void Interest ()
  12. {
  13. Console. WriterLine ("writing ");
  14. }
  15. }
  16. Public class Program: MyInterest
  17. {
  18. Public override void interest ()
  19. {
  20. Console. WriteLine ("programming ");
  21. }
  22. }
  23. Public class Sing: MyInterest
  24. {
  25. Public override void interest ()
  26. {
  27. Console. WriteLine ("singing ");
  28. }
  29. }
  30. Public class Test
  31. {
  32. Public static int main (String [] args)
  33. {
  34. MyInterest [] interests = new MyInterest [4];
  35. Interests [0] = new Write ();
  36. Interests [1] = new Program ();
  37. Interests [2] = new Sing ();
  38. Interests [3] = new MyInterest ()
  39. Foreach (MyInterest interest in interests)
  40. {
  41. Interest. interest ();
  42. }
  43. Return 0;
  44. }
  45. }

 

4:

Interface: the interface is the junction of two bounded things. It can interact with the other party through the interface. The interaction method is produced by the Convention, which is the rule of the specific interaction action, you only need to follow the rules to interact with things with rules. In C #, an interface is a special class that declares methods and members of a class and cannot be instantiated and can only be inherited, it only describes the features of things, but the tasks for implementing features are the responsibility of other classes. The interface cannot contain a series of methods defined by the field interface to be implemented by the class, but it cannot implement these methods itself, but it is described in a logical structure. An interface member can only be a method, attribute, event, or indexer. Its members cannot contain constants, fields, operators, instance constructors, destructor, or types, it cannot contain any type of static members.

[Csharp]View plaincopy
  1. Interface MyInterface
  2. {
  3. String STR // attribute STR
  4. {
  5. Get; // read attributes
  6. Set; // attribute writeable
  7. }
  8. Void outMethod (); // member method outMethod
  9. }
  10. Class InheritInterface: MyInterface
  11. {
  12. String str = "21-day C #"; // define and initialize the string
  13. Public string STR
  14. {
  15. Get
  16. {
  17. Return str;
  18. }
  19. Set
  20. {
  21. Str = value;
  22. }
  23. }
  24. Public void outMethod ()
  25. {
  26. Console. WriteLine (this. STR );
  27. }
  28. }



 

5: The domain and attribute domain are also called member variables. They can be used to save various information of the class. The domain can be divided into static domain and instance domain. Among them, static domain belongs to the class, the instance domain is an object. Attribute is a special member used to access objects. Field Declaration: it is the same as a common variable. new, public, protected, static, and readonly can be used.

[Csharp]View plaincopy
  1. Public class Date
  2. {
  3. Private string name = "hoan ";
  4. Public string nameAttr
  5. {
  6. Get // read value using get accessors
  7. {
  8. Return name;
  9. }
  10. Set // use the set accessors to write values
  11. {
  12. }
  13. }
  14. }


6: anonymous method:
The function of the anonymous method is to use the code block as a parameter, so that the code is very direct to the delegated instance, which can reduce the overhead of the instantiated delegate on system resources. The anonymous method is also shared.
The access permission of the function Member contained in the local statement.

Use of anonymous methods:

[Csharp]View plaincopy
  1. Namespace codemo
  2. {
  3. Delegate void Writer (string s); // name method called by the delegate
  4. Class Program
  5. {
  6. Static void NamedMethod (string k) // name method called by the delegate
  7. {
  8. System. Console. WriteLine (k );
  9. }
  10. Static void Main (string [] args)
  11. {
  12. Writer w = delegate (string j) // The delegate class calls the anonymous method.
  13. {
  14. System. Console. WriteLine (j );
  15. }
  16. W ("anonymous method called ");
  17. Console. WriteLine ();
  18. W = new Writer (NamedMethod); // create an object
  19. W ("the naming method is called"); // call the naming method
  20. Console. ReadLine ();
  21. }
  22. }
  23. }

& In C Language

& Can be used as the bitwise AND or address fetch Operator
The following describes two usage methods:
1. bitwise and operation bitwise AND operator "&" are binary operators. Its function is the binary phase corresponding to the two numbers involved in the operation. The result bit is 1 only when the two binary numbers are 1. Otherwise, the result bit is 0. The number of involved operations is supplemented.
For example, 9 & 5 can be written as follows: 00001001 (Binary complement of 9) & 00000101 (Binary complement of 5) 00000001 (Binary complement of 1) Visible 9 & 5 = 1.
Bitwise AND operations are usually used to clear some bits or retain some bits. For example, if a clears the high eight bits of 0 and retains the low eight bits, it can be used as a & 255 operation (255 of the binary number is 0000000011111111 ).
2. Get the address
& As The unary operator, the result is the address of the right operation object.
For example, & x returns the address of x.
The address itself is an abstract concept used to indicate the logical location of an object in the memory.

& In C Language

& Can be used as the bitwise AND or address fetch Operator
The following describes two usage methods:
1. bitwise and operation bitwise AND operator "&" are binary operators. Its function is the binary phase corresponding to the two numbers involved in the operation. The result bit is 1 only when the two binary numbers are 1. Otherwise, the result bit is 0. The number of involved operations is supplemented.
For example, 9 & 5 can be written as follows: 00001001 (Binary complement of 9) & 00000101 (Binary complement of 5) 00000001 (Binary complement of 1) Visible 9 & 5 = 1.
Bitwise AND operations are usually used to clear some bits or retain some bits. For example, if a clears the high eight bits of 0 and retains the low eight bits, it can be used as a & 255 operation (255 of the binary number is 0000000011111111 ).
2. Get the address
& As The unary operator, the result is the address of the right operation object.
For example, & x returns the address of x.
The address itself is an abstract concept used to indicate the logical location of an object in the memory.

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.