zoukankan      html  css  js  c++  java
  • C#语言——对象和类型

    目录

    1. 类和结构的区别
    2. 类成员
    3. 按值和按引用传送参数
    4. 方法重载
    5. 构造函数和静态构造函数
    6. 只读字段
    7. 部分类
    8. 静态类

    类和结构的区别

    类和结构实际上都是创建对象的模板,每个对象都包含数据,并提供了处理和访问数据的方法。

    结构与类的区别是他们在内存的储存方式、访问方式(类上存储在堆上的引用类型,而结构是存储在栈上的值类型)。较小的数据类型使用结构可提高性能。两者在语法上非常相似。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace _3_ObjectAndType
    {
        /// <summary>
        ////// </summary>
        class PhoneCustomer
        {
            public const string DayOfSendingBill = "Monday";
            public int CustomerId;
            public string FirstName;
            public string LastName;
        }
    
        /// <summary>
        /// 结构
        /// </summary>
        struct PhoneCustomerStruct
        {
            public const string DayOfSendingBill = "Monday";
            public int CustomerId;
            public string FirstName;
            public string LastName;
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                PhoneCustomer myPhoneCustomer = new PhoneCustomer();
                PhoneCustomerStruct myPhoneCustomerStruct = new PhoneCustomerStruct();
            }
        }
    }

    类成员

    类成员包含类的数据——字段、常量和事件的成员。

    方法的声明’

    方法的调用

    using System;
    
    namespace Wrox
    {
        class MainEntryPoint
        {
            static void Main()
            {
                // 调用静态方法
                Console.WriteLine("Pi is " + MathTest.GetPi());
                int x = MathTest.GetSquareOf(5);
                Console.WriteLine("Square of 5 is " + x);
    
                // 实例化对象
                MathTest math = new MathTest();  
    
    
                // 调用非静态方法
                math.Value = 30;
                Console.WriteLine(
                   "Value field of math variable contains " + math.Value);
                Console.WriteLine("Square of 30 is " + math.GetSquare());
            }
        }
    
        
        class MathTest
        {
            public int Value;
    
            public int GetSquare()
            {
                return Value * Value;
            }
            //静态方法
            public static int GetSquareOf(int x)
            {
                return x * x;
            }
            //静态方法
            public static double GetPi()
            {
                return 3.14159;
            }
        }
    }

    给方法传递参数

    参数可以通过引用或通过值传递给方法。传递引用变量,变量在方法内部的改变将会保持。而传递值变量,被调用的方法将得到的是变量的一个相同副本。因此,在传递复杂数据类型时,按引用类型传递更高效。

    ref参数

    C#值传递变量是默认的,也可以加上ref关键字来强制使值参数通过引用传送给方法。

    out参数

    输出参数可以不初始化。

    命名参数

    命名参数允许按任意顺序传递。

    可选参数

    参数是可选的,必须为可选参数提供默认值,并且是方法定义中最后的参数。

    方法的重载

    C#方法的重载有几种方式(即,方法名相同,但参数的个数和,或类型不同)。

    方法重载的限制:

    两个方法不能仅在返回类型上有区别。

    两个方法不能仅根据参数是声明为ref或out来区分。

    using System;
    
    namespace Wrox
    {
        class ParameterTest
        {
            //重载方法一
            static void SomeFunction(int[] ints)
            {
                ints[0] = 200;
            }
    
            //重载方法二
            static void SomeFunction(int[] ints, int i)
            {
                ints[0] = 100;
                i = 100;
            }
    
            static void SomeFunction(int[] ints, ref int i)
            {
                ints[0] = 100;
                i = 100;
            }
    
            static void SomeFunction(int[] ints, out int i)
            {
                ints[0] = 100;
                i = 100;
            }
    
            static string FullName(string firstName, string lastName)
            {
                return firstName + " " + lastName;
            }
    
            static string FullName2(string firstName, string lastName = "Hello")
            {
                return firstName + " " + lastName;
            }
    
    
    
    
            public static int Main()
            {
                int i = 0;
                int[] ints = { 0, 1, 2, 4, 8 };
                // 初始值
                Console.WriteLine("i = " + i);
                Console.WriteLine("ints[0] = " + ints[0]);
                Console.WriteLine("Calling SomeFunction...");
    
                //重载方法一
                SomeFunction(ints, i);
                Console.WriteLine("i = " + i);
                Console.WriteLine("ints[0] = " + ints[0]);
                //重载方法二
                SomeFunction(ints);
                Console.WriteLine("i = " + i);
                Console.WriteLine("ints[0] = " + ints[0]);
    
                //引用参数传递
                SomeFunction(ints, ref i);
                Console.WriteLine("i = " + i);
                Console.WriteLine("ints[0] = " + ints[0]);
    
                //输出参数传输
                SomeFunction(ints, out i);
                Console.WriteLine("i = " + i);
                Console.WriteLine("ints[0] = " + ints[0]);
    
                //命名参数
                FullName("John", "Doe");
                FullName(lastName: "Doe", firstName: "John");
    
                //可选参数
                FullName2("John");
                FullName2("Jonh", "jim");
    
                return 0;
            }
        }
    }

    属性

    属性的概念是:它是一个方法或一对方法。

    只读和只写属性

    属性的访问修饰符

    自动实现的属性

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace _3_ObjectAndType
    {
        class PropertyDemo
        {
            private int age;
            public int Age
            {
                get
                {
                    return age;
                }
                set
                {
                    age = value;
                }
            }
    
            //只读和只写属性
            private string name;
            public string Name {
                get
                {
                    return name;
                }
            }
    
            //属性的访问修饰符
            private string _firstname;
            public string FirstName
            {
                get
                {
                    return _firstname;
                }
                private set
                {
                    _firstname = value;
                }
            }
    
            //自动实现的属性
            public int LastName { get; set; }
        }
    }

    内联

    通过属性访问字段,而不是直接访问字段并不会降低性能。

    构造函数

    声明基本构造函数的语法就是声明一个与包含的类同名的方法,但该方法没有返回类型。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace _3_ObjectAndType
    {
        class StructMothedDemo
        {
            public StructMothedDemo()
            {
    
            }
    
            private int number;
            public StructMothedDemo(int number)
            {
                this.number = number;
            }
    
            private StructMothedDemo(int number, int number2)
            {
                this.number = number;
            }
        }
    }

    只读字段

    readonly比const更灵活,它允许把一个字段设置为常量,并在构造函数中给只读字段赋值。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace _3_ObjectAndType
    {
        class ReadOnlyDemo
        {
            public static readonly uint MaxDocuments;
            static ReadOnlyDemo()
            {
                MaxDocuments = 10;
            }
        }
    }

    匿名类型

    var与new关键字一起使用时,可以创建匿名类型。匿名类型只是一个继承Object且没有名称的类。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace _3_ObjectAndType
    {
        class AnonymousClass
        {
            public AnonymousClass()
            {
                var caption = new {FirstName = "James", LastName="Tom" };
                //caption.FirstName = "AA";  //只读字段
                //caption.LastName = "ZZ";
            }
        }
    }

    结构

    结构是值类型,在把结构作为参数传递给方法时,应把它作为ref参数传递,以避免性能损失,但要注意被调用的方法可以改变结构的值。

    结构的构造函数,不允许定义无参数的构造函数。

    部分类

    partial关键字允许把类、结构或接口放在多个文件中。

    静态类

    如果类只包含静态的方法和属性,该类就是静态的。静态类在功能上与使用私有静态构造函数创建的类相同。不能创建静态类的实例。在调用时,无需创建静态类的实例。

    using System;
    
    namespace Wrox
    {
        class MainEntryPoint
        {
            static void Main()
            {
                // 调用静态方法
                Console.WriteLine("Pi is " + MathTest.GetPi());
                int x = MathTest.GetSquareOf(5);
                Console.WriteLine("Square of 5 is " + x);
    
                // 实例化对象
                MathTest math = new MathTest();  
    
    
                // 调用非静态方法
                math.Value = 30;
                Console.WriteLine(
                   "Value field of math variable contains " + math.Value);
                Console.WriteLine("Square of 30 is " + math.GetSquare());
            }
        }
    
        
        class MathTest
        {
            public int Value;
    
            public int GetSquare()
            {
                return Value * Value;
            }
            //静态方法
            public static int GetSquareOf(int x)
            {
                return x * x;
            }
            //静态方法
            public static double GetPi()
            {
                return 3.14159;
            }
        }
    }

    Object类

    所有的.NET类都派生自System.Object。实际上,如果在定义类时没有指定基类,编译器就会自动假定这个类派生自Object。

  • 相关阅读:
    spring mvc注入配置文件里的属性
    spring mvc注入配置文件里的属性
    spring mvc注入配置文件里的属性
    ajaxFileUpload进行文件上传时,总是进入error
    ajaxFileUpload进行文件上传时,总是进入error
    ajaxFileUpload进行文件上传时,总是进入error
    配置quartz启动时就执行一次
    配置quartz启动时就执行一次
    JAVA遍历机制的性能的比较
    MyBatis中jdbcType与javaType对应表
  • 原文地址:https://www.cnblogs.com/cmhunter/p/4148328.html
Copyright © 2011-2022 走看看