C # object-oriented technology-interfaces, abstractions, seals, iterators, segments, and generics

Source: Internet
Author: User
Tags class manager

Interfaces, abstractions, and iterators are similar to java. Therefore, they are only explained in a simple way. They are used in simple examples.

Interface: You can use it to implement multiple inheritance functions. An interface can contain attributes, methods, indexers, and events, but can only be defined without assigning values.

Simple declaration interface:

interface IPeople{    string Name{get;set;}    void show();}

Use the Program class to implement the interface:

class Program : IPeople{    string name="";    public string Name{get{return name;} set{name=value;}}    public show(){Console.Write("Name:"+Name);}}

The main Program that uses the instantiated Program object:

Program pro = new Program (); // instantiate the Program object IPeople ip = pro; // use the ip address of the instantiated interface of the derived class object. name = "hello"; // assign an ip to the Name attribute in the derived class. show (); // call the method in the derived class to display the property value

In the preceding example, attributes and methods in the derived class are accessed through the instantiated interface object. In addition, interfaces can be inherited in multiple ways, which are separated:

Interface IPeople {string Name {get; set ;}} interface IMan: IPeople {void sex () ;}class Program: IPeople, IMan {public void sex () {Console. write ("... ") ;}} // call time: IMan man = pro; man. sex ();

Explicitly call the interface member class to implement two interfaces. When the two interfaces have the same members, they are used separately ):

Interface ICal1 {int Add ();} interface ical {int Add ();} class Calculate: ICal1, iCal {int ICal1.Add () {// explicit interface member implementation int x = 10; int y = 20; return x + y;} int ICal2.Add () {int x = 10; int y = 20; int z = 30; return x + y + z ;}// Calculate cal = new Calculate (); ICal1 cal1 = cal; Console. writeLine (cal. add (); // Similarly, you can use an interface to inherit the Object Instantiation interface of ical.


*********

Abstract classes and abstract methods:

As long as a method is declared as an abstract method, this class must also be declared as an abstract class. Abstract classes cannot be sealed.

Abstract class declaration:

Public abstract class Test {public int I; public void method (); public abstract void abdMethod (); // abstract method}

Use of abstract classes and abstract methods:

Public abstract class People {private string name = ""; public string Name {get {return name;} set {name = value ;}} public abstract void show (); // abstract method} public class One: People {// Method for rewriting the output information in the abstract class public override void show () {Console. write ("... ") ;}/// implement One one = new One (); // instantiate the derived class People p = one; // use the object of the derived class to instantiate the abstract class p. name = "hello"; // use an abstract class object to access the Name p. show (); // use an abstract class object to call a method in a derived class


**********

Sealing class and sealing method: proposed to avoid abuse of inheritance)

The sealing class can restrict scalability. If a class is sealed, other classes cannot inherit from the class, so the abstract class cannot be sealed.) Likewise, the sealing members are also like this.

Seal class declaration:

public sealed class SealedTest{}

Sealing Method:

The sealed method can only be used to implement the virtual method of the base class and provide specific implementation. Therefore, when declaring the sealed method, the sealed modifier and the override modifier are used at the same time.

Public class Base {public virtual void show () {// defines a virtual method Console. writeLine ("this is the virtual method in the base class");} public sealed class Derive: base {// derive a sealed subclass from the base class // sealed and override the virtual method show () public sealed override void show () {Base. show (); // call the virtual method Console of the base class. writeLine ("this is the method used to repeat the sealing class ");}}

Use of sealing class and sealing method:

Public class People {public virtual void show () {}// virtual method} public sealed class Student {private string name = ""; public string Name {get {return name ;} set {name = value ;}/// sealed and override show () public sealed override void show () {Console in the base class. writeLine ("NAME:" + Name) ;}// call Student stu = new Student (); // instantiate the stu. name = "hello"; stu. show ();

Iterator:

Code that returns an ordered sequence of values of the same type. The iterator Code uses the yield return statement to return each element in sequence. The yield break statement terminates the iteration. The return type must be IEnumerable or IEnumerator.

Create iterator:

Public class Banks: IEnumerable {string [] arr = {"item1", "item2", "item3", "item4"}; public IEnumerator GetEnumerator () {for (int I = 0; I <arr. length; I ++) {yield return arr [I] ;}}// call Banks banks = new Banks (); foreach (string str in banks) {richTextBox1.Text + = str + "\ n ";}


**********

Branch class: it is easier to understand the direct examples)

Partial class Part {// the first Part of the Division class public int add (int a, int B) {return a + B ;}} partial class Part {// the second Part of the Division class public int sub (int a, int B) {return a-B ;}// when calling. We can know that each Part of the Division class is actually composed of a class part = new Part (); part. add (1, 2); part. sub (5, 3 );

********

Generics are similar to java, so they are just examples ):

Public class Manager {public static int Mana <T> (T [] items, T item) {// create a generic method} // call a generic method int I = Manager. mana <int> (new int [] {1, 2}, 2 );

Instance -- use the interface to select different content

interface ISelect{    void Speak(string str);}class Chinese :ISelect{    public void Speak(".....");}class Foreign :ISelect{    public void Speak(".....");}

Instance -- Implement polymorphism through rewriting Abstract METHODS

Class Animal {public string Run () {return "can run! ";}Public abstract string Sound (); // declare the abstract method} class Dog: Animal {public override string Sound () {return" Wang! ";}}// This class outputs relevant behavior information of animals through polymorphism public static class AnimalClass {public static string AnimalSound (Animal an) {return. sound () ;}/// when called in the program, Dog = new dog (); AnimalClass. animalSound (dog );


This article from "tired also happy D" blog, please be sure to keep this source http://zhangzhang.blog.51cto.com/6250085/1263897

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.