asp.net(6)

来源:互联网 发布:上海敬游 知乎 编辑:程序博客网 时间:2024/04/29 14:26
c#的接口在类中具有显示和隐示的写法,接口显示用法虽然比较隐蔽但是是很非常关键:
具体利用显示接口的好处就不介绍了,现在写个例子
using System;
using System.Collections.Generic;
using System.Text;

namespace WindowsApplication1
{
    
interface Interface1
    
{
        
string add();
    }

}



using System;
using System.Collections.Generic;
using System.Text;

namespace WindowsApplication1
{
    
class Class1:Interface1
    
{
        
public Interface1 ddd
        
{
            
get {

                
return (Interface1)this;    //将该类强制转换为该接口
            }

        }


        
string Interface1.add()
        
{
            
return "aa";
        }


        
public string ccc()
        
{
            
return this.ddd.add();  //可以调用显示接口的定义的方法
        }

    }


    
class Program
    
{
        
static void Main(string[] args)
        
{

          Class1 eee
=new Class1();
          Console.Write(eee.ccc());
          Console.Read();
        }

    }

}

我们所需要注意的是:
大家一定想知道显示声明接口的方法如何在类内部使用,其中我定义的属性ddd即将该类(通过this)
强行转化为该接口类型,接下来就可以利用ddd.add来使用该方法了
这样就可以在内部使用

2.内部类的使用
using System;
using System.Collections.Generic;
using System.Text;

namespace WindowsApplication1
{
    
class Class1
    
{
        
private int aa;
        
internal Class1(int aa)
        
{
            
this.aa = aa;
        }


        
public class class11  //定义了一个内部类,必须是public或者internal,外部才能访问
        {

            
public string bb="bb";
 
            
public class class111 //再内部类11里面又定义了一个内部类,该类必须通过11才能引出
            
            
            }


            
public class11.class111 dd()
            
{
                
return  new  class11.class111();  //在类的内部只能通过class11来得到class111,不能直接由class1得到
            }

        }


        
public class11 bbb()
        
{
            
return new class11();
        }

    }

    
class class4
    
{
        
public static void Main()
        
{
            Class1 cls1 
= new Class1(5555);
            Class1.class11 cls11 
= cls1.bbb();  //通过类名直接可以引出内部类,前提内部类为public或者internal
            Console.Write(cls11.bb);
               Class1.class11 cls12 = new Class1.class11();
               Console.Write(cls12.bb);
            Console.Read();
        }

    }

}


3.接口的继承
接口的继承有这个地方需要注意:

using System;
using System.Collections.Generic;
using System.Text;


namespace aaa
{
    
interface itest
    
{
        
void foo();
    }



    
class aaa:itest
{
        
public void foo()
        
{
            Console.Write(
"aaa.itest");
        }

}


    
class bbb:aaa
    
{
        
public new void foo()
        
{
            Console.Write(
"bbb itest");
        
        }

    
    }


    
public class inter
    
{
        
public static void Main()
        
{
            bbb mybbb 
= new bbb();
            mybbb.foo();

            itest test
=(itest)mybbb;
            test.foo();
            Console.Read();
        }

    
    }

}


//结果是bbb itest  aaa itest
如何修改呢?让它实例化的接口也同样显示bbb itest:

namespace aaa
{
    
interface itest
    
{
        
void foo();
    }



    
class aaa:itest
{
        
public void foo()
        
{
            Console.Write(
"aaa.itest");
        }

}


    
class bbb:aaa,itest
    
{
        
public new void foo()
        
{
            Console.Write(
"bbb itest");
        
        }

    
    }


    
public class inter
    
{
        
public static void Main()
        
{
            bbb mybbb 
= new bbb();
            mybbb.foo();

            itest test
=(itest)mybbb;
            test.foo();
            Console.Read();
        }

    
    }

}
或者也可以这样
using System;
using System.Collections.Generic;
using System.Text;


namespace aaa
{
    
interface itest
    
{
        
void foo();
    }



    
class aaa:itest
{
        
public virtual void foo()
        
{
            Console.Write(
"aaa.itest");
        }

}


    
class bbb:aaa
    
{
        
public override void foo()
        
{
            Console.Write(
"bbb itest");
        
        }

    
    }


    
public class inter
    
{
        
public static void Main()
        
{
            bbb mybbb 
= new bbb();
            mybbb.foo();

            itest test
=(itest)mybbb;
            test.foo();
            Console.Read();
        }

    
    }

}



通常我们还会遇到遮掩的问题:
using System.Collections.Generic;
using System.Text;


namespace aaa
{
    
public class control
    
{
        
public void ser()
        

           Console.Write(
"control ser");
        }

    }


    
public interface idata
    
{
        
void ser();
    }


    
public class editbox : control, idata
    
{

    }


    
public class application
    
{
        
public static void Main()
        
{
            editbox edit 
= new editbox();
            idata bound 
= edit as idata;
            
if (bound != null)
            
{
                bound.ser();
            }

            
else
            
{
                Console.Write(
"idata is false");
            }

            Console.Read();
        }

    }

}
大家可以看到,editbox明明没有实现接口的ser方法,却仍然通过执行,就是因为基类有一个相同
命名的ser,导致错误,那我们只好在editbox中重新实现这个方法,
using System;
using System.Collections.Generic;
using System.Text;


namespace aaa
{
    
public class control
    
{
        
public  void ser()
        

           Console.Write(
"control ser");
        }

    }


    
public interface idata
    
{
        
void ser();
    }


    
public class editbox : control, idata
    
{
        
public  new void ser()
        
{
            Console.Write(
"editbox  ser");
        }

    
    }


    
public class application
    
{
        
public static void Main()
        
{
            editbox edit 
= new editbox();
            idata bound 
= edit as idata;
            
if (bound != null)
            
{
                bound.ser();
            }

            
else
            
{
                Console.Write(
"idata is false");
            }

            Console.Read();
        }

    }

}





顺便再介绍一下c#当中的IEnumerable接口,这个接口的功能就是为类提供了迭代算法
即foreach遍历就是通过这个接口来实现的,不多说看代码,一个非常简单但是典型的该接口用法:
  using   System;  
  
using   System.Collections;  
   
  
public   class   Tokens   :   IEnumerable  
  
{  
        
private   string[]   elements;  
   
        Tokens(
string   source,   char[]   delimiters)  
        
{  
              elements   
=   source.Split(delimiters);  
        }
  
   
        
public   IEnumerator   GetEnumerator()  
        
{  
              
return   new   TokenEnumerator(this);    //这是最关键的地方
        }
  
   
        
private   class   TokenEnumerator   :   IEnumerator   //该类内部定义了一个类
        
{  
              
private   int   position   =   -1;  
              
private   Tokens   t;  
   
              
public   TokenEnumerator(Tokens   t)  
              
{  
                    
this.t   =   t;  
              }
  
   
              
public   bool   MoveNext()  
              
{  
                    
if   (position   <   t.elements.Length   -   1)  
                    
{  
                          position
++;  
                          
return   true;  
                    }
  
                    
else  
                    
{  
                          
return   false;  
                    }
  
              }
  
   
              
public   void   Reset()  
              
{  
                    position   
=   -1;  
              }
  
   
              
public   object   Current  
              
{  
                    
get  
                    
{  
                          
return   t.elements[position];  
                    }
  
              }
  
        }
  
   
        
static   void   Main()  
        
{  
              Tokens   f   
=   new   Tokens("This is a sample sentence.",new   char[]   {' '});  
              
foreach   (string   item   in   f)  
              
{  
                 Console.WriteLine(item);  
              }


              IEnumerator fff 
= f.GetEnumerator();

              
while (fff.MoveNext())
              
{
                  Console.Write(fff.Current.ToString() 
+ "----");
              }


              Console.Read();
        }
  
  }
看明白这个你也就明白了Arraylist的实现过程,基本上是一样的,所以说非常典型


using System;
using System.Collections.Generic;
using System.Text;

namespace fanxing
{
    
public class A
    
{
        
public  static int _AInt;
        
public  int _instanceInt;
        
private static void AMethod()
        
{
            Console.WriteLine(_AInt);
        }

        
public void SayIt()
        
{
            NestedA.Method(
this);
        }

        
/**/
        
/*嵌套类 定义*/
        
private class NestedA
        
{
            
public static void Method(A a)
            
{
                
//内部类可以使用外层类的静态成员
                _AInt = 100;
                AMethod();
                
//实例成员,非静态成员需要实例化
                a._instanceInt = 10;
                
//a.SayIt();
            }

        }

    }


   
class Application123
{

    
static void Main(string[] args)
    
{
        A bb 
= new A();
        bb.SayIt();
        Console.Write(A._AInt);
        Console.Write(bb._instanceInt);
        Console.Read();
    }

}

}

原创粉丝点击