zoukankan      html  css  js  c++  java
  • (原创)c#学习笔记06--函数04--结构函数

    6.4  结构函数

      第5章介绍了结构类型,它可在一个地方存储多个数据元素,结构可以做的工作远不止此。一个重要的功能就是结构可以包含函数和数据。这初看起来很奇怪,但实际上是非常有用的。

      例如:

    struct CustomerName 
    { 
        public string firstName, lastName; 
        public string Name() 
        { 
            return firstName + " " + lastName; 
        } 
    }

      看起来这与本章前面的其他函数很类似,但没有使用static修饰符。本书将在后面阐明其原因,现在知道该关键字不是结构函数所必须的即可。这个函数的用法如下所示:

    CustomerName myCustomer; 
    myCustomer.firstName = "John"; 
    myCustomer.lastName = "Franklin"; 
    Console.WriteLine(myCustomer.Name());

      注意,Name()函数可以直接访问firstName和lastName机构成员,在CustomerName结构中,它们可以被看作是全局成员。

  • 相关阅读:
    join函数——Gevent源码分析
    代理上网(ssh 动态端口转发)
    内核热patch
    技术债
    mysql 隔离级别与间隙锁等
    python type
    django : related_name and related_query_name
    ssh 卡主
    logistics regression
    __new__ 和 __init__
  • 原文地址:https://www.cnblogs.com/wodehao0808/p/4912351.html
Copyright © 2011-2022 走看看