001_结构struct

作者: HMY轩园 | 来源:发表于2017-04-26 10:10 被阅读2次
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication1
    {   
            public struct Name
            {
                //字段
                private string fName, mName, lName;
    
                public Name(string fname, string mname, string lname)
                {
                    fName = fname;
                    mName = mname;
                    lName = lname;
                }
    
                //属性
                public string firstName
                {
                    get { return fName; }
                    set { fName = value; }
                }
                public string middleName
                {
                    get { return mName; }
                    set { mName = value; }
                }
                public string lastName
                {
                    get { return lName; }
                    set { lName = value; }
                }
                //重写ToString()方法
                public override string ToString()
                {
                    return  String.Format("{0} {1} {2}", fName, mName, lName);
                }
    
                public string Initials()
                {
                    return (String.Format("{0}{1}{2}", fName.Substring(0, 1), mName.Substring(0, 1), lName.Substring(0, 1)));
    
                }
            }
            class NameTest
            {
            static void Main()
            {
            Name myName = new Name("Michael", "Mason", "McMillan");
            string fullName, initsName;
            fullName = myName.ToString();
                initsName = myName.Initials();
                Console.WriteLine(fullName);
                Console.WriteLine(initsName);
                Console.Read();
                }
            }
        }
    
    

    运行结果:

    Michael Mason McMillan
    MMM

    相关文章

      网友评论

        本文标题:001_结构struct

        本文链接:https://www.haomeiwen.com/subject/dfcizttx.html