您的位置:首页 > 编程语言 > C#

C#学习笔记06:类继承

2013-07-29 21:26 260 查看

1、概念:

   父类,基类

  Person:pid,pname,pbirth,….

  子类、派生类

    Student:继承Person属性和方法,扩展一些自己属性和函数:sno,sdept,smajor,…

Employee: 继承Person属性和方法

Teacher: 继承Person属性和方法

 图形类Shape:求周长、求面积函数。  抽象父(基)类

 三角形类:继承图形类

 矩形类: 继承图形类

 圆类:继承图形类

窗口类:

   登陆窗口类:窗口类

   注册窗口类:窗口类

2、c#继承语法

  父类

  class Person

{

     ….

   }

   class Student:Person

{

     …

   }

子类从父类继承什么?子类从父类继承public,protected部分成员:属性和方法。

  Student s1 = new Student();

3、继承构造函数

   当子类对象实例化,首先自动父类的构造函数,然后才调用自己本身构造函数。当父类构造函数需要参数时,子类构造函数如何定义?

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ex2013_07_22

{

    class Person

    {

        private string pid, pname, pbirth;

        

        public Person()

        {

            Console.WriteLine("Person()");

        }

        public Person(string pid, string pname, string pbirth)

        {

            this.pid = pid; this.pname=pname; this.pbirth = pbirth;

        }

    }

    class Student : Person

    {

        private string sno, sdept;

        public Student():base()

        {

            Console.WriteLine("Student()");

        }

        public Student(string pid, string pname, string pbirth,

            string sno,string sdept):base(pid,pname,pbirth)

        {

            this.sno = sno; this.sdept = sdept;

        }

    }

    class Ex1_1

    {

        

        static void Main(string[] args)

        {

            Student s = new Student("1","zhou","1990-1-1","2010001","math");

            Person p = new Person();

            Console.ReadLine();

        }

    }

}

4、函数覆盖问题

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ex2013_07_22

{

    class Person

    {

        private string pid, pname, pbirth;

        

        public Person()

        {

            Console.WriteLine("Person()");

        }

        public Person(string pid, string pname, string pbirth)

        {

            this.pid = pid; this.pname=pname; this.pbirth = pbirth;

        }

        public void Disp()

        {

            Console.WriteLine(pid+" "+pname+" "+pbirth);

        }

    }

    class Student : Person

    {

        private string sno, sdept;

        public Student():base()

        {

            Console.WriteLine("Student()");

        }

        public Student(string pid, string pname, string pbirth,

            string sno,string sdept):base(pid,pname,pbirth)

        {

            this.sno = sno; this.sdept = sdept;

        }

        public void Disp()

        {

            base.Disp();//调用父类函数或属性

            Console.WriteLine(sno + " " + sdept);

        }

    }

    class Ex1_1

    {

        

        static void Main(string[] args)

        {

            Student s = new Student("1","zhou","1990-1-1","2010001","math");

            s.Disp();

            Console.ReadLine();

        }

    }

}

子类实例未将函数覆盖。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ex2013_07_22

{

    class Person

    {

        private string pid, pname, pbirth;

        

        public Person()

        {

            Console.WriteLine("Person()");

        }

        public Person(string pid, string pname, string pbirth)

        {

            this.pid = pid; this.pname=pname; this.pbirth = pbirth;

        }

        public  void Disp()

        {

            Console.WriteLine(pid+" "+pname+" "+pbirth);

        }

    }

    class Student : Person

    {

        private string sno, sdept;

        public Student():base()

        {

            Console.WriteLine("Student()");

        }

        public Student(string pid, string pname, string pbirth,

            string sno,string sdept):base(pid,pname,pbirth)

        {

            this.sno = sno; this.sdept = sdept;

        }

        public  void Disp()

        {

            Console.WriteLine(sno + " " + sdept);

        }

    }

    class Ex1_1

    {

        

        static void Main(string[] args)

        {

            //父类对象引用子类实例

            Person p = 

                new Student("1","zhou","1990-1-1","2010001","math");

            p.Disp();

            //

            Console.ReadLine();

        }

    }

}

 虚函数及覆盖问题:

 virtual,overide

在父类函数中定义virtual函数,允许子类覆盖该函数,子类使用overide对虚函数覆盖。实现动态绑定,当父类对象引用子类实例时,子类实例函数上传到父类实例将父类虚函数覆盖。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ex2013_07_22

{

    class Person

    {

        private string pid, pname, pbirth;

        

        public Person()

        {

            Console.WriteLine("Person()");

        }

        public Person(string pid, string pname, string pbirth)

        {

            this.pid = pid; this.pname=pname; this.pbirth = pbirth;

        }

        public virtual void Disp()

        {

            Console.WriteLine(pid+" "+pname+" "+pbirth);

        }

    }

    class Student : Person

    {

        private string sno, sdept;

        public Student():base()

        {

            Console.WriteLine("Student()");

        }

        public Student(string pid, string pname, string pbirth,

            string sno,string sdept):base(pid,pname,pbirth)

        {

            this.sno = sno; this.sdept = sdept;

        }

        public override void Disp()

        {

            Console.WriteLine(sno + " " + sdept);

        }

    }

    class Ex1_1

    {

        

        static void Main(string[] args)

        {

            //父类对象引用子类实例

            Person p = new Student("1","zhou","1990-1-1","2010001","math");

            p.Disp();

           Console.ReadLine();

        }

    }

}

5、抽象函数与抽象类

抽象函数是指没有实现(即没有函数体),只有声明。

当一个类包含抽象函数时,该类必须定义为抽象类。抽象类不能实例化,只能作为其他类的父类,抽象基类。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ex2013_07_22

{

    abstract class Shape

    {

        public abstract double area();

        public void dispArea()

        {

            Console.WriteLine(area());

        }

    }

    class Rect : Shape

    {

        private int w, h;

        public Rect(int w,int h)

        {

            this.w = w; this.h = h;

        }

        public override double area()

        {

            return w * h;

        }

    }

    class Circle : Shape

    {

        private int r;

        public Circle(int r)

        {

            this.r = r;

        }

        public override double area()

        {

            return Math.PI*r*r;

        }

    }

    class Ex1_1

    {

        

        static void Main(string[] args)

        {

            Shape shape = new Rect(2, 4);

            shape.dispArea();

            shape = new Circle(5);

            shape.dispArea();

            Console.ReadLine();

        }

    }

}

本章练习题下载地址:点此下载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C# Unity3D