zoukankan      html  css  js  c++  java
  • asp.net中C#对象与方法 属性详解

    C#对象与方法

    一、相关概念:

      1、对象:现实世界中的实体

     2、 类:具有相似属性和方法的对象的集合

     3、面向对象程序设计的特点:封装  继承 多态

    二、类的定义与语法

    1、定义类: 修饰符 类名称 类成员

    a)定义类语法:

    修饰符 class 类名
    {
       类成员
    }

    2、类的访问修饰符:public internal

    a) public:可访问域是所在的程序 和任何引用的程序 访问不受限制

    定义语法:
     public class 类名
     {
       类成员
     }


    b) internal:可访问域定义范围内 (默认访问修饰符)

    语法:

     (internal) class 类名
     {
       类成员
     }

    3、类成员:数据成员和字段 

     a)  数据成员:字段和常量

      字段:变量

      声明:类型 字段名

    例:

     代码如下 复制代码


    public class Persion
    {
        public string name;
    }

    class Test
    {
       static void Main(string[] args)
       {
           Persion persion=new Persion();
           persion.name="kaka";
           Console.WriteLine("{0}",persion.name);
       }
    }

    b) 方法成员
      声明:
     修饰符 返回值类型 方法名(参数列表)
     {
          方法体
     }
       修饰符:如:public、private、protected、internal
       返回值类型:若方法无返回值,则使用 void
    例:

     代码如下 复制代码

    public void Method()
    {
      Console.riteLine("方法声明 www.111Cn.net");
    }

    4、成员的访问修饰符:public、private、protected、internal

      a)  public:公有成员

      b) private:私有成员

      c) protected:保护成员

      d) internal:内部成员

    例:

     代码如下 复制代码


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace ConsoleApplication2
    {
        public class Employee
        {
            private float sum;
            public int day;
            public float wage;
            //定义方法输出工资信息
            public void Show()
            {
                sum = day * wage;
                Console.WriteLine("工作时间:{0},每天工资:{1},总工资:{2}",day,wage,sum);
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                Employee employee = new Employee();
                employee.day = 20;
                employee.wage = 50;
                //employee.sum:无法访问  因为它为私有成员
                //调用方法现实工资
                employee.Show();
            }
        }
    }

    三、实例化对象:关键字:new

    例:

     代码如下 复制代码

    ?using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
     
    namespace ConsoleApplication1
    {
        public class car
        {
            private string carName;
            private string carType;
            private int price;
     
            public string CarName
            {
                get { return carName; }
                set { carName = value; }
            }
            public string CarType
            {
                get { return carType; }
                set { carType = value; }
            }
           
            public int Price
            {
                get { return price; }
                set { price = value; }
            }
     
            public void  action()
            {
                Console.WriteLine("一辆名叫{0}车,型号是{1},价钱是:{2}",carName,carType,price);
            }
             
        }
     
        //创建实例并访问字段和方法
        class Program
        {
            static void Main(string[] args)
            {
                //创建car类的实例
                car vehicle = new car();
                //给实例赋值
                vehicle.CarName = "奔驰";
                vehicle.CarType = "XZ001";
                vehicle.Price = 1000000;
     
                //调用方法
                vehicle.action();
     
     
     
            }
        }
    }


     

    C#对象数组排序方法

    排序是编程中常用的法算之一,排序的方法有很多种,下面介绍一种简单有效的排序方法,代码如下:

     代码如下 复制代码
    private bool isReverse = false;
    private void Sort(PersonalNotificationEntity [] list,string key)
    {
    if ( isReverse )
    {
    Array.Reverse(list);
    isReverse = false;
    }
    else
    {
    int len = list.Length;
    Type type = typeof(PersonalNotificationEntity);
    object [] keys = new object[len];
    for(int i = 0 ; i < len ; i++)
    {
    keys[i] = type.InvokeMember(key,BindingFlags.GetField ,null,list[i],null);
    }
    Array.Sort(keys,list);
    isReverse = true;
    }
    }

    这里使用了Array.Sort()和Array.Reverse()方法对数据进行正/反排序,变量isReverse做为反排序的标志位
    方法传入了2个参数,一个是要排序的对象数组list,一个是排序关键字key,即要对象的根据哪个属性或字段来进行排序(这个值是等于对象的属性/字段名)
    type.InvokeMember()方法可以得到对象实例的属性/字段值,这里使用的是字段
    在得到数组中的每一个要排序的字段值后,把这个字段值数组做为Array.Sort()方法的参数传入,Sort方法就会将对象数按这个字段的值进行排序

    四、属性

    1、
     a) 概念:用于访问类的字段的成员
     b) 属性用途:保证数据安全   作数据的验证

    2、声明:
    访问修饰符 数据类型 属性名
    {
      get{}
      set{}
    }

    3、get 访问器
      a) 含义:不带参数,用于向外部指定字段的值,通常使用return 语句返回某个变量的值 在属性取值时自动调用
      b) get 访问器的使用:

     代码如下 复制代码


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace ConsoleApplication2
    {
        public class Employee
        {
            private string name = "微微";

            public string Name
            {
                get
                {
                    Console.WriteLine("程序访问了get访问器");
                    return name;
                }

            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                Employee employee = new Employee();
                Console.WriteLine("访问属性之前");
                Console.WriteLine();
                Console.WriteLine(employee.Name);
                Console.WriteLine();
                Console.WriteLine("访问属性之后");
            }
        }
    }

    4、set 访问器:返回值类型为void

    5、属性类型:

     a) 读/写:有get()和set()访问器的属性
     b) 只读:有get(0访问器的属性,对成员提供读取
     c)只写(不推荐使用):仅包含set() 访问器

    五、方法的参数

    1、值参数:按值传递

    例:

     代码如下 复制代码


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace ConsoleApplication2
    {
        public class Test
        {
            public void Method(int x,int y)
            {
                int temp = x;
                x = y;
                y = temp;
                Console.WriteLine("交换之前:x={0},y={1}",x,y);
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                int a = 2;
                int b = 5;
                Test test = new Test();
                test.Method(a,b);
                Console.WriteLine("交换之后:x={0},y={1}",a,b);
            }
        }
    }

    2、引用参数:向方法传递实参在内存中的地址,按地址传递

    例:

     代码如下 复制代码


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace ConsoleApplication2
    {
        public class Test
        {
            public void Method(ref int  x,ref int y)
            {
                int temp = x;
                x = y;
                y = temp;
                Console.WriteLine("交换之前:x={0},y={1}",x,y);
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                int a = 2;
                int b = 5;
                Test test = new Test();
                test.Method(ref a,ref b);
                Console.WriteLine("交换之后:x={0},y={1}",a,b);
            }
        }
    }

    3、输出参数:从方法传递回一个结果

    关键字:out http://www.111cn.net/net/net/46994.htm

    例:

     代码如下 复制代码


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace ConsoleApplication2
    {
        public class Test
        {
            public void Method(int x,out int y)
            {
                y = x + x;
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                Test test = new Test();
                int outy;
                test.Method(35, out outy);
                Console.WriteLine("y={0}",outy);
            }
        }
    }

    4、数组型参数:参数只允许是一组数组,当方法的参数前带有params关键字时,就是带数组型参数的方法(使用引用传递)

    例:

     代码如下 复制代码


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace ConsoleApplication2
    {
        public class Test
        {
            public void Method(params int[] num)
            {
                Console.WriteLine("数组大小:{0}",num.Length);
                foreach (int i in num)
                {
                    Console.Write("{0}",i);
                }
                Console.WriteLine();
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                int[] nums = { 1, 2, 3, 4, 5 };
                Test test = new Test();
                test.Method(nums);
                test.Method(2,3,4,5);
                test.Method();
            }
        }
    }

    补充 对象 方法 属性有和区别


    在面向对象里面,对象和类是不同的,对象是特定类的一个实例,比如如果车是一个类的话,某个人的一辆奔驰车就是一个对象,它是车这个类的实例。类是抽象的,而对象是具体的。方法是定义在对象上的操作,属性是记录对象性质和状态的变量,拿上面车的例子来说,车的重量,最大速度是车的属性,启动,停在这些动作则可以定义为车的方法。我说的可能不太准确,建议楼主看看面向对象相关的书籍。

    补充:
    对象和类当然是不一样的,对象是类的具体化(其实不准确),再打个比方吧,
    告诉你猫是一个类,它包含两个属性,重量和毛色,
    根据上面的信息你能知道是指是哪只猫吗?不能吧,因为你不知道它的重量和毛色。 现在把猫实例化,即指定它的重量和毛色,假定为1kg、黑色,而这个1kg黑色的猫就是对象了,同样,2kg白色的猫,3kg黄色的猫,等都是对象。

    当然1kg黑色的猫也可以是作为一个类,为这个类加个主人属性,
    实例化类就得到对像,比如李四的(1kg黑色猫),张三的(1kg黑色猫)... 就是这个类的对象。

    接着,李四的lkg黑色猫也可以成为一个类了,那这个类的对象呢,和上面一样,加个能够区分的属性。
    ......
    这样就形成了类的层次结构了,然后父类,子类(派生类),继承等概念都可以理解了。

     
  • 相关阅读:
    委托与事件参数的简单运用
    C#消息队列专题
    项目计划流程简易描述
    cookies 客户端历史记录篇
    朋友做的VS2005插件:等号两边值互换
    SSE2指令集系列之二
    SSSE3指令集
    SSE3指令集系列
    SSE特殊指令集系列之一
    SSE2指令集系列之一
  • 原文地址:https://www.cnblogs.com/alibai/p/3579695.html
Copyright © 2011-2022 走看看