当前位置: 移动技术网 > IT编程>开发语言>c# > C#基础概念二十五问 16-20

C#基础概念二十五问 16-20

2019年07月18日  | 移动技术网IT编程  | 我要评论
16.类和结构的区别? 答: 类: 类是引用类型在堆上分配,类的实例进行赋值只是复制了引用,都指向同一段实际对象分配的内存 类有构造和析构函数 类可以继承和被继承 结构:
16.类和结构的区别?

答:
类:

类是引用类型在堆上分配,类的实例进行赋值只是复制了引用,都指向同一段实际对象分配的内存

类有构造和析构函数

类可以继承和被继承

结构:

结构是值类型在栈上分配(虽然栈的访问速度比较堆要快,但栈的资源有限放),结构的赋值将分配产生一个新的对象。

结构没有构造函数,但可以添加。结构没有析构函数

结构不可以继承自另一个结构或被继承,但和类一样可以继承自接口

 

示例:

根据以上比较,我们可以得出一些轻量级的对象最好使用结构,但数据量大或有复杂处理逻辑对象最好使用类。

如:geoemtry(gis 里的一个概论,在 ogc 标准里有定义) 最好使用类,而 geometry 中点的成员最好使用结构

using system;
using system.collections.generic;
using system.text;

namespace example16
{
    interface ipoint
    {
        double x
        {
            get;
            set;
        }
        double y
        {
            get;
            set;
        }
        double z
        {
            get;
            set;
        }
    }
    //结构也可以从接口继承
    struct point: ipoint
    {
        private double x, y, z;
        //结构也可以增加构造函数
        public point(double x, double y, double z)
        {
            this.x = x;
            this.y = y;
            this.z = z;
        }
        public double x
        {
            get { return x; }
            set { x = value; }
        }
        public double y
        {
            get { return x; }
            set { x = value; }
        }
        public double z
        {
            get { return x; }
            set { x = value; }
        }
    }
    //在此简化了点状geometry的设计,实际产品中还包含project(坐标变换)等复杂操作
    class pointgeometry
    {
        private point value;

        public pointgeometry(double x, double y, double z)
        {
            value = new point(x, y, z);
        }
        public pointgeometry(point value)
        {
            //结构的赋值将分配新的内存
            this.value = value;
        }
        public double x
        {
            get { return value.x; }
            set { this.value.x = value; }
        }
        public double y
        {
            get { return value.y; }
            set { this.value.y = value; }
        }
        public double z
       {
            get { return value.z; }
            set { this.value.z = value; }
        }
        public static pointgeometry operator +(pointgeometry left, pointgeometry rigth)
        {
            return new pointgeometry(left.x + rigth.x, left.y + rigth.y, left.z + rigth.z);
        }
        public override string tostring()
        {
            return string.format("x: {0}, y: {1}, z: {2}", value.x, value.y, value.z);
        }
    }
    class program
    {
        static void main(string[] args)
        {
            point tmppoint = new point(1, 2, 3);

            pointgeometry tmppg1 = new pointgeometry(tmppoint);
            pointgeometry tmppg2 = new pointgeometry(tmppoint);
            tmppg2.x = 4;
            tmppg2.y = 5;
            tmppg2.z = 6;

            //由于结构是值类型,tmppg1 和 tmppg2 的坐标并不一样
            console.writeline(tmppg1);
            console.writeline(tmppg2);

            //由于类是引用类型,对tmppg1坐标修改后影响到了tmppg3
            pointgeometry tmppg3 = tmppg1;
            tmppg1.x = 7;
            tmppg1.y = 8;
            tmppg1.z = 9;
            console.writeline(tmppg1);
            console.writeline(tmppg3);

            console.readline();
        }
    }
}
结果:
x: 1, y: 2, z: 3
x: 4, y: 5, z: 6
x: 7, y: 8, z: 9
x: 7, y: 8, z: 9 


17.接口的多继承会带来哪些问题?

答:

c# 中的接口与类不同,可以使用多继承,即一个子接口可以有多个父接口。但如果两个父成员具有同名的成员,就产生了二义性(这也正是 c# 中类取消了多继承的原因之一),这时在实现时最好使用显式的声明

示例:

using system;
using system.collections.generic;
using system.text;

namespace example17
{
    class program
    {
        //一个完整的接口声明示例
        interface iexample
        {
            //属性
            string p
            {
                get;
                set;
            }
            //方法
            string f(int value);
            //事件
            event eventhandler e;
            //索引指示器
            string this[int index]
            {
                get;
                set;
            }
        }
        interface ia
        {
            int count { get; set;}
        }
        interface ib
        {
            int count();
        }
        //ic接口从ia和ib多重继承
        interface ic : ia, ib
        {
        }
        class c : ic
        {
            private int count = 100;
            //显式声明实现ia接口中的count属性
            int ia.count
            {
                get { return 100; }
                set { count = value; }
            }
            //显式声明实现ib接口中的count方法
            int ib.count()
            {
                return count * count;
            }
        }
        static void main(string[] args)
        {
            c tmpobj = new c();

            //调用时也要显式转换
            console.writeline("count property: {0}", ((ia)tmpobj).count);
            console.writeline("count function: {0}", ((ib)tmpobj).count());

            console.readline();
        }
    }
}
结果:
count property: 100
count function: 10000 


18.抽象类和接口的区别?

答:

抽象类(abstract class)可以包含功能定义和实现,接口(interface)只能包含功能定义

抽象类是从一系列相关对象中抽象出来的概念, 因此反映的是事物的内部共性;接口是为了满足外部调用而定义的一个功能约定, 因此反映的是事物的外部特性

分析对象,提炼内部共性形成抽象类,用以表示对象本质,即“是什么”

为外部提供调用或功能需要扩充时优先使用接口


19.别名指示符是什么?

答:

通过别名指示符我们可以为某个类型起一个别名

主要用于解决两个命名空间内有同名类型的冲突或避免使用冗余的命名空间

别名指示符在所有命名空间最外层定义,作用域为整个单元文件。如果定义在某个命名空间内,那么它只在直接隶属的命名空间内起作用

示例:

class1.cs: 


using system;
using system.collections.generic;
using system.text;

namespace com.nblogs.reonlyrun.csharp25qexample.example19.lib01
{
    class class1
    {
        public override string tostring()
        {
            return "com.nblogs.reonlyrun.csharp25qexample.example19.lib01's class1";
        }
    }
}
class2.cs: 


using system;
using system.collections.generic;
using system.text;

namespace com.nblogs.reonlyrun.csharp25qexample.example19.lib02
{
    class class1
    {
        public override string tostring()
        {
            return "com.nblogs.reonlyrun.csharp25qexample.example19.lib02's class1";
        }
    }
}
主单元(program.cs):

using system;
using system.collections.generic;
using system.text;

//使用别名指示符解决同名类型的冲突
//在所有命名空间最外层定义,作用域为整个单元文件
using lib01class1 = com.nblogs.reonlyrun.csharp25qexample.example19.lib01.class1;
using lib02class2 = com.nblogs.reonlyrun.csharp25qexample.example19.lib02.class1;

namespace example19
{
    namespace test1
    {
        //test1class1在test1命名空间内定义,作用域仅在test1之内
        using test1class1 = com.nblogs.reonlyrun.csharp25qexample.example19.lib01.class1;

        class class1
        {
            //lib01class1和lib02class2在这可以正常使用
            lib01class1 tmpobj1 = new lib01class1();
            lib02class2 tmpobj2 = new lib02class2();
            //testclass1在这可以正常使用
            test1class1 tmpobj3 = new test1class1();
        }
    }
    namespace test2
    {
        using test1class2 = com.nblogs.reonlyrun.csharp25qexample.example19.lib01.class1;

        class program
        {
            static void main(string[] args)
            {
                //lib01class1和lib02class2在这可以正常使用
                lib01class1 tmpobj1 = new lib01class1();
                lib02class2 tmpobj2 = new lib02class2();

                //注意这里,testclass1在这不可以正常使用。
                //因为,在test2命名空间内不能使用test1命名空间定义的别名
                //test1class1 tmpobj3 = new test1class1();

                //testclass2在这可以正常使用
                test1class2 tmpobj3 = new test1class2();

                console.writeline(tmpobj1);
                console.writeline(tmpobj2);
                console.writeline(tmpobj3);

                console.readline();
            }
        }
    }
}

结果:
com.nblogs.reonlyrun.csharp25qexample.example19.lib01's class1
com.nblogs.reonlyrun.csharp25qexample.example19.lib02's class1
com.nblogs.reonlyrun.csharp25qexample.example19.lib01's class1


20.如何手工释放资源?

答:

 .net 平台在内存管理方面提供了gc(garbage collection),负责自动释放托管资源和内存回收的工作。但在以下两种情况需要我们手工进行资源释放:一、由于它无法对非托管资源进行释放,所以我们必须自己提供方法来释放对象内分配的非托管资源,比如你在对象的实现代码中使用了一个com对象;二、你的类在运行是会产生大量实例(象 gis 中的geometry),必须自己手工释放这些资源以提高程序的运行效率

最理想的办法是通过实现一个接口显式的提供给客户调用端手工释放对象,system 命名空间内有一个 idisposable 接口,拿来做这事非常合适,省得我们自己再声明一个接口了 
示例:

using system;
using system.collections.generic;
using system.text;

namespace example20
{
    class program
    {
        class class1 : idisposable
        {
            //析构函数,编译后变成 protected void finalize(),gc会在回收对象前会调用调用该方法
            ~class1()
            {
                dispose(false);
            }

            //通过实现该接口,客户可以显式地释放对象,而不需要等待gc来释放资源,据说那样会降低效率
            void idisposable.dispose()
            {
                dispose(true);
            }

            //将释放非托管资源设计成一个虚函数,提供在继承类中释放基类的资源的能力
            protected virtual void releaseunmanageresources()
            {
                //do something...
            }

            //私有函数用以释放非托管资源
            private void dispose(bool disposing)
            {
                releaseunmanageresources();

                //为true时表示是客户显式调用了释放函数,需通知gc不要再调用对象的finalize方法
                //为false时肯定是gc调用了对象的finalize方法,所以没有必要再告诉gc你不要调用我的finalize方法啦
                if (disposing)
                {
                    gc.suppressfinalize(this);
                }
            } 
        }
        static void main(string[] args)
        {
            //tmpobj1没有手工释放资源,就等着gc来慢慢的释放它吧
            class1 tmpobj1 = new class1();

            //tmpobj2调用了dispose方法,传说比等着gc来释放它效率要调一些
            //个人认为是因为要逐个对象的查看其元数据,以确认是否实现了dispose方法吧
            //当然最重要的是我们可以自己确定释放的时间以节省内存,优化程序运行效率
            class1 tmpobj2 = new class1();
            ((idisposable)tmpobj2).dispose();
        }
    }
}

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网