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("方法声明");
}

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();



        }
    }
}

 四、属性

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(); Console.WriteLine("交换之前:x={0},y={1}",a,b);

test.Method(
ref a,ref b); } } }

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

关键字:out

例:

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();
        }
    }
}


 

原文地址:https://www.cnblogs.com/DBtwoer/p/3073796.html