C#基础知识整理:C#类和结构(1)

黄舟
Release: 2017-02-10 15:13:53
Original
1240 people have browsed it

1、结构功能特性? 实现代码?
结构用struct关键字定义的,与类类似,但有本质区别。结构实质是一个值类型,它不需要对分配的。
结构的特性:
(1)、结构作为参数传递时,是值传递。
(2)、结构的构造函数必须带参数的。
(3)、结构实例化可以不用new的。
(4)、结构不能继承,但是可以实现接口。
(5)、结构中不能初始化实例字段。
例:

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

namespace TestStruct
{
    class Program
    {
        public struct Circle     //定义一个圆
        {
            private const double pi = 3.1415926;

            public double radius;  //半径

            /// 
            /// 构造函数
            /// 
            public Circle(double r)
            {
                radius = r;
            }
            /// 
            /// 面积
            /// 
            public double CArea()
            {
                return 3.14 * radius * radius;
            }

        }

        static void Main(string[] args)
        {
            Circle circle1;    //不用new实例化

            circle1.radius = 5;

            Console.WriteLine("圆面积为:" + circle1.CArea());

            Circle circle2 = new Circle(1);  //用new实例化

            Console.WriteLine("圆面积为:" + circle2.CArea());

            Console.ReadLine();
        }
    }
}
Copy after login

2、什么是委托? 特点? 何时使用委托而不使用接口? 如何声明、实例化和使用委托?

(1)、委托是一个类,它定义了方法的类型,可以讲方法当做另一个方法的参数。避免在程序中使用分支,

程序扩展性更好。
例子:

  class Program
    {
        public delegate void PrinteDelegate(string name);

        private static void PrinteEnglish(string name)
        {
            Console.WriteLine("Your Name: " + name);
        }

        private static void PrinteChinese(string name)
        {
            Console.WriteLine("你的大名: " + name);
        }

        private static void Printe(string name, PrinteDelegate MakeGreeting)
        {
            MakeGreeting(name);
        }

        static void Main(string[] args)
        {
            Printe("Sam Young", PrinteEnglish);

            Printe("白杨树", PrinteChinese);

            Console.ReadLine();
        }
    }
Copy after login

(2)、委托类似于 C++ 函数指针,但它是类型安全的。

委托允许将方法作为参数进行传递。

委托可用于定义回调方法。

委托可以链接在一起;例如,可以对一个事件调用多个方法。

方法不需要与委托签名精确匹配。有关更多信息,请参见协变和逆变。

C# 2.0 版引入了匿名方法的概念,此类方法允许将代码块作为参数传递,以代替单独定义的方法。

使用委托,我觉得应该在需要分支调用不同方法时使用。但是例如工厂模式中,根据分支实例化不同的类

,这时就用接口。


委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递,这种将方法动

态地赋给参数的做法,可以避免在程序中大量使用If-Else(Switch)语句,同时使得程序具有更好的可扩

展性。

3、什么是部分类/分部类? 哪些功能特性? 实现代码? 适用场合? 需遵循几个规则?
分部类就是将一个类用partial关键字,分成好几个独立文件,但实质还是一个类。一般当一个类行数太多或者某些功能比较独立可以用分部类,最常见就是窗体Form.cs和Form.designer.cs。
一般需遵循如下规则:
(1)、必须使用partial 关键字

(2)、虽然有不同的部分,但是各个部分必须具有相同的可访问性,如public、private 等

(3)、如果将任意部分声明为抽象的、密封的,则整个类型都被视为抽象的、密封的

(4)、如果任意部分声明继承基类时,则整个类型都将继承该类

(5)、 各个部分可以指定不同的基接口,最终类型将实现所有分部声明所列出的全部接口

(6)、在某一分部定义中声明的任何类、结构或接口成员可供所有其他部分使用

(7)、嵌套类型可以是分部的,即使它们所嵌套于的类型本身并不是分部的也如此。

 以上就是C#基础知识整理:C#类和结构(1)的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!