不鸣则已

海阔凭鱼跃,天高任鸟飞!

首页 新随笔 联系 订阅 管理

1、封装

答:属性封装了字段,通过get和set访问器限制字段对外开放的程度;将重复的代码封装成方法,实现DCR原则(Don't Copy yourself);方法的参数组合可以用类实现,即在方法中不要传入超过3个以上的参数,否则定义一个类,直接将类对应的对象作为参数传入;将实现统一功能的类中所以的方法提取到接口或抽象类中(最明显的体现在三层中,DAL层抽象出一个只定义了增删查改功能的IDAL接口),至少也要写成虚方法,以实现面向对象编程(多态的实现)。

public interface IBaseDAL<T> where T : class,new()
{
    T Add(T model);
    bool Update(T model);
    bool delete(int id);
    T GetModel(int id);
    List<T> GetList();
}
公用DAL层接口
public class BookDAL : IBaseDAL<Book>
{

    public Book Add(Book model)
    {
        throw new NotImplementedException();
    }

    public bool Update(Book model)
    {
        throw new NotImplementedException();
    }

    public bool delete(int id)
    {
        throw new NotImplementedException();
    }

    public Book GetModel(int id)
    {
        throw new NotImplementedException();
    }

    public List<Book> GetList()
    {
        throw new NotImplementedException();
    }
}
这里我就不写全了
具体类实现

2、继承

答:在C#中类是单继承的,而接口是可以多继承的(通常对于接口的继承我们称之为“实现接口”)

public class Student : Person,ICloneable,IEnumerable
{

    public object Clone()
    {
        throw new NotImplementedException();
    }

    public IEnumerator GetEnumerator()
    {
        throw new NotImplementedException();
    }
}
继承的实现

继承的好处:代码重用;多态实现的方式之一(涉及到一个软件开发原则:里氏替换原则),举个例子,在方法中,参数传父类对象,而在调用是,只要传入子类对象就可以了,前提他们是继承关系。

class Program
{
    static void Main(string[] args)
    {
        Child c = new Child();
        c.Name = "TQ";
        c.Age = 21;
        Console.WriteLine(Say(c));
        Console.ReadKey();
    }

    public static string Say(Person p)
    {
        return p.Name;
    }

    public class Person
    {
        private string _name;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public int Age
        {
            get;
            set;
        }

        public string this[int key]
        {
            get
            {
                string result = "";
                switch (key)
                {
                    case 1: result = "a"; break;
                    case 2: result = "b"; break;
                }
                return result;
            }
        }
    }

    public class Child : Person
    {

    }
}
里氏替换

构造函数不能被继承,通过base.方法(),可以在子类中调用父类的方法。

class Program
{
    static void Main(string[] args)
    {
        Child c = new Child();
        c.Name = "TQ";
        c.Age = 21;
        Console.WriteLine(Say(c));
        Console.ReadKey();
    }

    public static string Say(Person p)
    {
        return p.Name;
    }

    public class Person
    {
        private string _name;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public int Age
        {
            get;
            set;
        }

        public void Speach()
        {
        }

        public string this[int key]
        {
            get
            {
                string result = "";
                switch (key)
                {
                    case 1: result = "a"; break;
                    case 2: result = "b"; break;
                }
                return result;
            }
        }
    }

    public class Child : Person
    {
        public void Speach()
        {
            base.Speach();
        }
    }
}
base调用父类方法

类中的成员如果不写访问修饰符默认是private

访问修饰符:Public(接口成员默认)、Protected、Private(类成员默认private)、internal(类默认internal)

访问级别约束:子类的访问级别不能比父类的高;类中属性或字段的访问级别不能比类的访问级别高;方法的访问级别不能比方法的参数和返回值的访问级别高(如果将上例代码中Person类的访问修饰符改为Private,编译就会报错)。

posted on 2013-12-02 22:43  唐群  阅读(152)  评论(0编辑  收藏  举报