您的位置:首页 > 移动开发 > Unity3D

unity学习之面向对象技术核心之继承

2014-12-03 20:47 267 查看
今天接着讲面向对象技术的三大核心,属性封装写在了上一篇日志中,这次主要讲继承

继承

    (1)继承提供了创建新类的一种方法,继承对开发者来说就是代码共享。

    (2)通过继承创建的子类是作为另一个类的扩充或修正所定义的一个类。

    (3)子类从超类(父类)中继承所有方法和变量。

    (4)子类和超类之间是特化与范化的关系。

子类的声明 

    (1)语法:子类声明:父类{子类体}
               



                   class Son : Father//子类继承父类

     (2)子类可以使用父类的protected和public可见的变量和方法,就像这些变量和方法是自己定义的一样。

          class Father

         {

             //可以用public或者private

             //protected int a = 2;

               public int a = 2;

               public Father()

              {

                    Console.WriteLine("我是父类的里面的构造方法");

              }

          }

     (3)私有成员和方法其实已经被继承了,但是它们却不可以被访问,因为私有成员和方法只能被声明它们的类中才可访问,所以看上去           像是没有被继承。

 

     (4)C# 中,如果类声明时没有声明父类,那么缺省为Object 类的子类。C#中的所有类都是System.Object类的子类。

 

     (5)C#中,子类只能继承一个父类,但一个基类可以有多个直接派生类。继承是可以传递的

 

Sealed

      sealed 修饰符,此修饰符会阻止其他类从该类继承。

          sealed class A

         {

                int test;

                public void Sum(int i,int j)

                {

                      int sum = i + j;

                     Console.WriteLine("I am A ,my sum ={0}",sum);

                }

        }

          class B : A

         {

                 public void Minus(int i,int j)

                {

                       int minus = i - j;

                       Console.WriteLine("I am B ,my minus ={0}", minus);

                       this.Sum(3, 4);       //编译器会报错    

                }

          }

 

方法隐藏



       如果子类方法的方法名和基类的方法名相同时,系统将隐藏基类同名方法,自动调用子类的同名方法

 

       在这里的举例根据前面的例子连接着来:Father--Son--Son2

            //Father类

               class Father

               {

                        //protected int a = 2;

                          public int a = 2;

                          public Father()

                         {

                                   Console.WriteLine("我是父类的里面的构造方法");

                          }

                            public Father(int a)

                           {

                                    Console.WriteLine("我是父类的里面的带参数的构造方法");

                           }

                           public void Get()

                          {

                                     Console.WriteLine("我是父类的里面的Get()");

                           }

                         static void Main(string[] args)

                          {

                          }

                }

                 //Son类

                      class Son : Father//子类继承父类

                     // sealed class Son:Father//子类继承父类

                     {

                                   public Son()

                                               : base(10)

                                  {//显示调用父类里面的带参数的构造方法

                                              Console.WriteLine("我是子类的里面的构造方法");

                                   }

                                   static void Main(string[] args)

                                  {

                                            Son son = new Son();//默认先调用父类构造方法,然后再次调用子类构造方法

                                            //son.Get();

                                            // Console.WriteLine( son.a);

                                            Console.ReadKey();

                                   }

                         }

                        //Son2类

                        class Son2 : Son

                        {

                                 static void Main(string[] args)

                                 {

                                            Son son = new Son();

                                            son.Get();

                                            Console.WriteLine(son.a);

                                            Console.ReadKey();

                                           //a=b  b=c--->a=c;

                                  }

                         }

派生类的建立需要注意:

      (1).派生类会继承基类除了构造函数和析构函数的所有成员。

      (2).派生类调用构造函数时,会先调用基类的构造函数。默认调用没有参数的构造函数。

      (3).用base关键字显式调用基类构造函数(如果基类定义了带有参数的构造函数,那么此构造函数必须被执行,且在派生类中实现该构           造函数,此时我们可以使用base关键字)

更多精彩内容请关注:http://www.gopedu.com/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息