c#学习笔记之类和对象

一、与生活中的类一样 ,程序中的类描述了一种对象,定义一个完整的类,需要包括字段、属性、方法、构造函数和析构函数(析构函数在程序编译时自动生成,可以不必自己写)。

定义类的方法和字段时,要通过访问修饰符来修饰类和类成员的访问权限。

public为公共访问,可以有任何其他类成员访问。private为私有访问,只在定义它的类中访问。protect为受保护访问,可以在类内部和以该类为基类的派生类中访问。internal为内部访问,只有在同一程序集中的文件中,内部成员才能被访问。

eg:

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

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            Time t = new Time();//调用构造函数
            t.output();
            Console.WriteLine(t.year);
            //Console.WriteLine(t.month);编译器不能通过,month被设为private
            Console.WriteLine(t.day);
            Time t1 = new Time(2016, 9, 9);//调用方法,为其赋初值
            t1.output();
        }
    }

    class Time
    {
        public int year;
        private int month;
        internal int day;

        public Time()//构造函数,初始化数值
        {
            year = 2015;
            day = 1;
            month = 1;
        }

        public Time(int year,int month,int day)
        {
            this.year = year;
            this.day = day;
            this.month = month;
        }

        public void output()
        {
            Console.WriteLine("year: {0} monht:{1} day{2}", year, month, day);
        }
    }
}

程序运行结果为:

 

二、静态类

静态类是类的一种,与非静态类的区别为:静态类不能被实例化,即不能使用new关键字创建静态类的实例对象

eg:

public static class day

{

      public static void output()

              Console.WriteLine("qwer");

}

调用时只能使用类名调用

eg:   day.output();

三、函数的重载(overloading)

使用重载的目的是为了使用同一方法名实现不同的功能。说的明白一点就是重载就是写的函数的名字一样,函数的参数不一样。

eg:

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

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            Time t = new Time();//调用构造函数
            t.output();
 
            Time t1 = new Time(2016, 9, 9);//调用第一个函数,为year month day赋初值 
            t1.output();

            Time t2 = new Time(211, 100);//调用第二个重载函数,为year day赋初值
            t2.output();

            Time t3 = new Time(12);//调用第三个重载函数,为month赋初值
            t3.output();
        }
    }

    class Time
    {
        public int year;
        public int month;
        public int day;

        public Time()//构造函数,初始化数值
        {
            year = 1;
            day = 1;
            month = 1;
        }

        public Time(int year,int month,int day)
        {
            this.year = year;
            this.day = day;
            this.month = month;
        }

        public Time(int year,int day)//重载函数
        {
            this.year = year;
            this.day = day;
        }

        public Time(int month)//重载函数
        {
            this.month = month;
        }

        public void output()
        {
            Console.WriteLine("year: {0} monht:{1} day{2}", year, month, day);
        }
    }
}

输出结果:

 当使用重载函数赋值时,各个time对象输出的值不同,但不知道为什么未给赋值时,输出的是0,而不是输出构造函数的1???

四、overriding

overriding是在继承中允许在子类修改函数的功能

eg:

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

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            Time t = new Time();
            t.output();

            Time2 t2 = new Time2();
            t2.output();
        }
    }

    public class Time
    {
        public int year;
        public int month;
        public int day;

        public Time()
        {
            year = 1;
            day = 1;
            month = 1;
        }

        public Time(int year,int month,int day)
        {
            this.year = year;
            this.day = day;
            this.month = month;
        }

        public virtual void output()
        {
            Console.WriteLine("year: {0} monht:{1} day{2}", year, month, day);
        }
    }


    public class Time2:Time
    {
        public override void output()
        {
            Console.WriteLine("hahah");
        }
    }
}

其中,Time2 overriding Time中的output,输出结果为

 

posted @ 2015-03-30 12:12  prog123  阅读(276)  评论(0编辑  收藏  举报