• 再说类和结构


    之所以分开说,是因为这次是由变量说起的,变量是存储在内存中的,内存的的数据是由0和1组成的,那么操作系统怎么才能区分这些数据呢,所以有了数据的类型。

    我们定义变量的时候,一定会给变量定义类型的,否则系统就会报错。

    如果是一个简单是数据类型的话,我们可以定义为数值型,字符型,布尔型等。这些简单数据是直接存储在一级缓存中的,读取很快的,而且由系统自动分配.

    对于一些较复杂的变量,我们可以使用枚举,结构,和数组,当然这些变量你会发现他们是有规律的变量---都有共同的特性,比如枚举,他是有一定顺序的,所以他可以称为简单变量的集合------

    所以结构就是有共同特性变量的集合(或者说是一个特性的集合,一个类型的集合),一个类型的结合,对!你又自己创造了一个类型,你就可以直接使用这个类型来定义新的变量,直接使用就可以。

    (注意我们在红色标注字体中说的是较复杂的变量,这些变量有共同的一个特性,我们可以把它组合到一块)

    当然变量多的话就不好操作,这时候你可以用结构函数来管理

    上个例子

    struct CustomerName
    {
    public string firstName, lastName;
    }
    
    
    
    CustomerName myCustomer;
    myCustomer.firstName = "John";
    myCustomer.lastName = "Franklin";
    Console.WriteLine("{0} {1}", myCustomer.firstName, myCustomer.lastName);

    使用函数

    struct CustomerName
    {
    public string firstName, lastName;
    public string Name()
    {
    return firstName + " " + lastName;
    }
    }
    
    
    CustomerName myCustomer;
    myCustomer.firstName = "John";
    myCustomer.lastName = "Franklin";
    Console.WriteLine(myCustomer.Name());

    现在我们继续深入了解变量,开头我说过变量需要在内存中存储的,那么这个较复杂的变量是怎么存储的呢,我们用到了栈,是操作系统中的栈,他是一个区域的内存我们给他叫做栈,这个栈存储的是一些共同特性的一些数据,我们对这些数据的操作是读取要求快,且不进行一些复杂的反复的操作。

    上个例子

    namespace Ch05Ex03
    {
    enum orientation: byte
    {
    north = 1,
    south = 2,
    east = 3,
    west = 4
    }
    struct route
    {
    public orientation direction;
    public double distance;
    }
    class Program
    {
    static void Main(string[] args)
    {
    route myRoute;
    int myDirection = -1;
    double myDistance;
    Console.WriteLine("1) North
    2) South
    3) East
    4) West");
    do
    {
    Console.WriteLine("Select a direction:");
    myDirection = Convert.ToInt32(Console.ReadLine());
    }
    while ((myDirection < 1) || (myDirection > 4));
    Console.WriteLine("Input a distance:");
    myDistance = Convert.ToDouble(Console.ReadLine());
    myRoute.direction = (orientation)myDirection;
    myRoute.distance = myDistance;
    Console.WriteLine("myRoute specifies a direction of {0} and a " +
    "distance of {1}", myRoute.direction, myRoute.distance);
    Console.ReadKey();
    }
    }
    }

    ——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

    以上说的是简单的,或者有一些共同特性或者不经行复杂操作的一些变量,但肯定有一些变量我们要反复使用经行一些复杂操作(我见过一个把所有变量都设成类)我们引入操作更方便的类,对于一些复杂的变量MS也都使用的类,他把这些变量用类封装起来,供我们来反复使用(很方sha便gua的),这个是在二级缓存中的(有点疑问),存放的是地址,我们所使用的变量是对地址的操作,所以我实例化,用到NEW哦,所有的类都继承于System.Object,这个是操作系统的堆。

    好吧接口也是这个定义的,因为他也要反复的使用,且比较复杂,(注意接口并没有继承任何东西)不再扩展了。。。。。。

  • 相关阅读:
    navicat连接mysql报错1251解决方案
    ubuntu 安装nodejs/npm
    sync-settings(vscode)
    ubuntu远程桌面连接windows系统
    three.js中点生成矩阵方法
    threeJs中旋转位移等操作坐标系
    ubuntu查看进程端口号及运行的程序
    Ubuntu终端远程连接linux服务器
    THREE.OrbitControls参数控制
    canvas设置长宽
  • 原文地址:https://www.cnblogs.com/mamiyiya777/p/5937726.html
Copyright © 2020-2023  润新知