zoukankan      html  css  js  c++  java
  • 求解:关于C#中结构中的构造函数的问题?

        构造函数用实例化吗?
       Str1 s = new Str1();编译通过;但结构中确不能声明无参的构造函数。
       new Str1()和 Str1 s 这样的声明有什么区别吗?
     
    public struct Str1
        {
            public string name;
            public int age;
            public void Say()
            {
                Console.WriteLine(name + "-->" + age);
            }
    
            public override string ToString()
            {
                return "我的名字是:" + name + ";我的年龄是:" + age;
            }
    
            public Str1(string n, int e)//注意这里,“结构中不支持无参的构造函数”
            {
                name = "asdf";
                age = 1;
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                // Str1 s = new Str1();既然struct不支持无参的构造函数,为什么这里可以用无参的构造函数来创建
                Str1 s = new Str1("xx", 0);
                // Str1 s;
                //s.age = 27;
                //s.name = "potoo";
                //s.Say();
                Console.WriteLine(s.ToString());
    
                Console.ReadKey();
            }
        }
  • 相关阅读:
    webapi之fiddler头设置
    ios---setContentOffset
    webapi参数处理get过个参数
    socket网络编程
    logging模块
    configparser模块(拷贝)
    hashlib模块--摘要算法
    异常处理
    面向对象拓展
    反射
  • 原文地址:https://www.cnblogs.com/potoofly/p/2965238.html
Copyright © 2011-2022 走看看