zoukankan      html  css  js  c++  java
  • C#基础 基本语法4

    1、除了基本的OOP技术,还要熟悉一些比较高级的技术。集合、比较、转换。
    System.Collections名称空间中的几个接口提供了基本的集合功能。
    IEnumberable;ICollection;IList;IDictionnary
    2、
    ArrayList animalArrayList=new ArrayList();

    ------------------------------------------------------------------------------------------

    第十章
    1、
    public:
    private:成员只能由类中的代码访问。
    internal:只能由定义它的项目内部代码访问。
    protected:只能由类或派生类中的代码访问。//只能由项目中派生类的代码来访问。
    2、.NET Framework中的公共字段以PascalCasing来命名,而不是camelCasing
    定义字段
    class MyClass
    {
    public int MyInt;
    }

    class MyClass
    {
    public readonly int MyInt=17;//只在执行构造函数的过程中赋值,或由初始化语句赋值
    }

    class MyClass
    {
    public static int MyInt;//可以用static关键字声明为静态;静态字段必须通过定义它们的类来访问,而不是通通过这个类的对象实例来访问。const成员是静态的,所以不需要用static修饰符。
    }
    3、定义方法
    class MyClass
    {
    public string GetString()
    {
    return "Here is a string.";
    }
    如果使用了static关键字,这个方法就只能通过类来访问。不能通过对象实例来访问。
    也可以在方法定义中使用
    virtual:方法可以重写
    abstract:方法必须在非抽象的派生类中重写(只用于抽象类中)
    override:方法重写了一个基类方法(如果方法重写就必须使用该关键字)
    extern:方法定义放在其他地方。
    }
    重写::::::::
    public class MyBaseClass
    {
    public virtual void DoSomething()
    {
    //Base implementation.
    }

    public class MyDerivedCalss:MyBaseClass
    {
    public override void DoSomething(){
    //Derived class implementation,override base implementation.
    }
    //如果使用了override 也可以使用sealed指定在派生类中不能对这儿方法进一步修改
    //public override sealed void DoSomething(){
    // .......
    //}
    }

    4、属性拥有两个类似于函数的快,一个快获取属性的值,另一个用于设置属性的值。也称为访问器
    分别用get、set关键字来定义。可以忽略其中一个块来创建只读或只写属性(忽略get块创建只写属性,忽略set块创建只读属性)
    private int myInt;
    public int MyIntProp//可以是public或private
    {
    get{return myInt;}//get字段必须有一个属性类型的返回值,简单的属性一般与私有字段相关联,以控制对这个字段的访问,此时get块可以直接返回该字段的值。
    set{myInt =value;}//set以类似的方式把一个值赋值给字段。Value等于类型与属性相同的类型,就不必担心数据类型转换了。
    }
    //这个简单的属性只能直接访问myInt字段。在对操作进行更多的控制时,属性的真正作用才发挥出来。
    set
    {
    if(value>=0&&value<=10)//只有赋值属性的值在0-10之间,才会改为myInt
    myInt=value;
    }
    //如果使用了无效值1、什么也不错2、给字段赋值默认值3、继续执行4、抛出异常
    set
    {
    if(value>=0&&value<=10)//只有赋值属性的值在0-10之间,才会改为myInt
    myInt=value;
    else
    throw(new ArgumentOutOfRangeException("MyIntProp",value,"MyIntProp must be assigned a value between 0 and 10."))
    }
    -------------------------------------------
    private int MyInt;
    public int MyIntProp{
    get{return myInt;}
    protected set{myInt =value;}//只有在类或派生类的代码才能使用set访问器。
    }
    5、自动属性
    public int MyIntProp{get;set;}//我们按照通常的方式定义属性的可访问性、类型和名称,但没有给get,set块提供实现的代码(和底层的字段)都由编译器提供。自动属性的唯一限制是它们必须包含get set存取器,无法使用这种方式定义只读只写属性。
    6、public class MyBaseClass
    {
    public void DoSomething()
    {
    //Base implementation;
    }
    }

    public class MyDerivedClass:MyBaseClass
    {
    public void DoSomething()
    {
    //Derived class implementation,hides base implementation.
    }
    }
    //这段代码可以正常运行,但是会产生警告,说明隐藏了一个基类成员。如果无意间隐藏此时可以改正错误,如果确实要隐藏,要用new关键字显示的表明意图。
    public class MybaseClass
    {
    public void DoSomething()
    {
    //Base implementation;
    }
    }

    public class MyDerivedClass:MyBaseClass
    {
    new public void DoSomething()
    {
    //Derived class implementation,hides base implementation.
    }
    }
    ---------------------------注意隐藏基类成员和重写他们的区别
    public class MybaseClass
    {
    public virtual void DoSomething()
    {
    Console.WriteLine("Base imp");
    }
    }

    public class MyDerivedClass:MyBaseClass
    {
    public override void DoSomething()
    {
    Console.WriteLine("Derived imp");
    }
    }//重写方法将替换基类中的实现代码,这样代码将使用新版本,即使通过基类类型进行的,情况也是这样的(使用多态性)
    MyDerivedClass myObj=new MyDerivedClass();
    MyBaseClass myBaseObj;
    myBaseObj=myObj;
    myBaseObj=DoSomething();//结果也是Deried imp
    -----------------------------------------------使用下面代码隐藏基类方法
    public class MybaseClass
    {
    public virtual void DoSomething()
    {
    Console.WriteLine("Base imp");
    }
    }

    public class MyDerivedClass:MyBaseClass
    {
    new public void DoSomething()
    {
    Console.WriteLine("Derived imp");//基类方法不一定是虚拟的,结果为Base imp
    }
    }
    7、调用重写或隐藏的基类方法
    无论是重写成员还是隐藏成员,都可以在派生类的内部访问基类成员。在许多情况下都很有用的。
    1、要对 派生类的用户隐藏继承的公共成员,但仍能在类总访问其功能
    2、要给继承的虚拟成员添加实现代码,而不是简单地用重写的新执行的代码替换它。
    public class MybaseClass
    {
    public virtual void DoSomething()
    {
    Console.WriteLine("Base imp");
    }
    }

    public class MyDerivedClass:MyBaseClass
    {
    public override void DoSomething()
    {
    base.DoSomething();
    }
    }
    //MyBaseClass是MyDrerivedClass的基类,而DoSomething()版本包含在MyDrivedClass中,因为base使用的是对象实例,所以静态成员中使用它会产生错误
    8、this关键字最常用的事把当前对象实例的引用传递给一个方法
    public void DoSomething()
    {
    MyTargetClass myObj=new MyTargetClass();
    myobj=DoSomethingWith(this);
    }
    还有一个方法是限定本地类型成员。
    public class MyClass
    {
    private int someData;
    public int SomeData
    {
    get
    {
    return this.someData;//一眼就看出引用的是成员
    }
    }
    }
    9、接口-成员-的定义:
    不允许使用访问修饰符public,private,protected,internal所有的接口成员都是公共的。
    接口成员不能包含代码体
    接口不能定义字段成员
    接口成员不能用关键字static,virtual,abstract,sealed来定义
    类型定义成员是禁止的。
    -----------------------------------------------------------------------------------------
    interface IMyBaseInterface
    {
    void DoSomething();
    }
    interface IMyDerivedInterface:IMyBaseInterface
    {
    new void DoSomething();
    }//执行方式与隐藏继承的类成员的方式一样,接口中定义的属性可以定义访问块get,set中的哪一个用于该属性
    10、interface IMyInterface
    {
    int MyInt{get;set;}//int属性MyInt有get,set存取器对于访问级别有更严格的限制来说,可以省略他们任何一个//类似自动属性,但自动属性是为类定义的,自动属性必须包含get,set存取器
    }
    //接口没有指定如何存储属性数据接口不能指定字段。接口与类一样,可以定义为类的成员但不能定义为其他接口的成员,因为接口不包含类型定义。

    11、类中实现接口
    类中实现接口必须包含该接口所有成员的实现代码,且必须匹配指定的签名,包括匹配指定的get,set块,并且必须是公共的。
    public interface IMyInterface
    {
    void DoSomething();
    void DoSomethingElse();
    }
    public class MyClass:IMInterface
    {
    public void DoSomething()
    {
    }
    public void DoSomething()
    {
    }
    }
    ------------------------------------------------------------------------------------
    可以使用关键字virtual或abstract实现接口成员,但不能使用static或const。
    //可以在基类上实现接口
    public interface IMyInterface
    {
    void DoSomething();
    void DoSomethingElse();
    }
    public class MyBaseClass()
    {
    public void DoSomething()
    {
    }
    }
    public class MyDerivedClass:MyBaseClass,IMyInterface
    {
    public void DoSomethingElse()
    {
    }
    }
    //P223
    12、隐式的实现接口成员
    MyClass myObj=new MyClass();
    myObj.DoSomething();
    或者
    MyClass myObj=new MyClass();
    IMyInterface myInt=myObj;
    myInt.DoSomething();
    显示的实现接口成员
    public class MyClass:IMyInterface
    {
    void IMyInerface.DoSomething()//DoSomething是显示的实现
    {
    }
    public void DoSomethingElse()//是隐式的实现,只有这个可以通过MyClass的对象实例来访问
    {
    }
    }
    13、用非公共的可访问性添加属性存储器
    public interface IMyInterface
    {
    int MyIntProperty
    {
    get;
    }
    }
    public class MyBaseClass:IMyInterface
    {
    public int MyIntProperty{get;protected set;}
    }
    14、部分类的定义//如果使用部分类定义,partial关键字就必须出现在包含在定义部分的每个文件的于此相同的位置。
    public partial clss MyClass
    {
    ............
    }
    ------------------------------------------------------------------------
    public partial class MyClass:IMyInterface1
    {
    ......
    }
    public partial class MyClass:IMyInterface2
    {
    ......
    }
    和public class Myclass:IMyInerface1,IMyInterface2
    {
    ......
    }//等价的
    15、部分方法在部分类中定义,但没有方法体,但在另一个部分类中包含实现代码,这两个类中都要使用pratial关键字。
    public partial class Myclass
    {
    prrtial void MyPartialMethod();
    }
    public partial class MyClass
    {
    partial void MyPartialMethod()
    {
    //Method implementation
    }
    }
    //部分方法也可以是静态的,但他们总是私有的,且不能有返回值,他们使用的任何参数都不能是out参数,但可以是ref参数。部分方法也不能使用virtual,abstract,override,new,sealed,extern修饰符
    部分方法编译时重要,用法不重要。
    public partial classMyClass
    {
    public void DoSomethingElse();
    public void DoSomething()
    {
    Console.WriteLine("DoSomething() execution started.");
    DoSomethingElse();
    Console.WriteLine("DoSomething() exexution finished.");
    }
    }

    public partical class MyClass
    {
    partial void DoSomethingElse()
    {
    Console.WriteLine("DoSomethingElse() calles.");
    }
    }//第一个部分类定义和调用部分方法DoSomethingElse,在第二个部分类中实现它,结果为
    DoSomething() execution started.
    DoSomething() called.
    Dosomething() execution finished.

    ------------------------------------------------------------------------------------------

  • 相关阅读:
    oracle 导入数据时提示只有 DBA 才能导入由其他 DBA 导出的文件
    oracle 常用语句
    android udp 无法收到数据 (模拟器中)
    android DatagramSocket send 发送数据出错
    AtCoder ABC 128E Roadwork
    AtCoder ABC 128D equeue
    AtCoder ABC 127F Absolute Minima
    AtCoder ABC 127E Cell Distance
    CodeForces 1166E The LCMs Must be Large
    CodeForces 1166D Cute Sequences
  • 原文地址:https://www.cnblogs.com/d685600/p/3650365.html
Copyright © 2011-2022 走看看