类的定义与使用

using System;

namespace C方法和类
{
    class Program
    {
        public static void Main(string[] args)
        {
            //1,如果要使用一个类,就要引入它所在的命名空间,因为customer位于当前的命名空间下,所以不需要引入,可以之间使用
            Customer customer1;//在这里我们使用Customer模板,声明了一个变量(对象)
            customer1=new Customer();//对象初始化与要加new加类名
            customer1.name="siki";//我们自己定义的类声明的对象,需要初始化,才能使用
            Console.WriteLine(customer1.name);
            customer1.Show();
            //使用对象中的方法
            // TODO: Implement Functionality Here
            
            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
        }
    }
}
//    public class Customer
    {
        //在这里我们定义了一个类
        //数据成员:里包含了4个字段
        public string name;
        public string address;
        public int age;
        public string buyTime;
        //函数成员:定义了一个方法
        public void Show(){
            Console.WriteLine("名字:"+name);
            Console.WriteLine("年龄:"+age);
            Console.WriteLine("地址:"+address);
            Console.WriteLine("购买时间:"+address);
        }
        
    }
}

 

posted @ 2018-10-30 22:11  逐梦武威  阅读(154)  评论(0编辑  收藏  举报