C#继承与多态的学习

一、继承

1.1  继承的概念

       描述了类之间关系的一个概念,子类可以使用父类的所有非私有成员,但是继承是单链条的,只能单重继承一个父类,而不允许多重继承多个父类。

        其中上面描述中介绍的父类和子类有下面的阐述:
        父类:是一个大类,封装了本大类通用的一些成员(变量和方法),能够被子类继承和使用。

        子类:是一个大类型下特殊的类,封装了子类型特有的一些成员(变量和方法)。

class Program
	{
		public static void Main(string[] args)
		{
			Teacher teacher=new Teacher();
            Student student=new Student();

			teacher.Walk();//子类使用了父类的方法
			teacher.Teach();//子类调用了子类的特有方法
            
            student.Teach();//方法调用失败,因为这是teacher特有的方法。            

			Console.ReadKey();
		}
	}
//人(父类)
	class Person
	{
		public void  Walk()
		{
			Console.WriteLine("Person can walk");//人共有的方法
		}
		public void Talk()
		{
			Console.WriteLine("Person can talk");//人共有的方法
		}
//教师(子类)
	class Teacher:Person
	{
		public void Teach()
		{
			Console.WriteLine("Teacher can teach");//老师特有的方法
		}
	}
	//学生(子类)
	class Student:Person
	{
		public void Study()
		{
			Console.WriteLine("Student can study");//学生特有的方法
		}
	}

输出结果:

                Person can walk
                Teacher can teach 

1.2  继承的使用方法

1.2.1 继承标识符

        根据上面的代码所示,c#在类与类之间使用继承关系时采用“:”符号。

1.2.2 继承权限关键字

        而在使用继承关系的过程中,继承成员的访问权限是很重要的一部分,c#给我们提供了三个权限修饰符,分别为:private、protected、public。使用方法如下表所示:

权限修饰符同类子类外部类
privatetrue
protectedtruetrue
public

true

truetrue
1.2.3 继承的成员访问规则

        ①如果子类中成员的名字与父类中成员的名字一致,则会直接使用子类的变量或方法,父类中的变量或方法将会被隐藏。

class Program
	{
		public static void Main(string[] args)
		{
			Teacher teacher=new Teacher();
			teacher.Talk();
			Console.ReadKey();
		}
	}
//人(父类)
	class Person
	{
		public void Talk()
		{
			Console.WriteLine("Person can talk");
		}
	}
//教师(子类)
	class Teacher:Person
	{
		new public void Talk()
		{
			Console.WriteLine("Teacher can talk");
		}
	}

输出结果:

                Teacher can talk

        而在子类中的new是告诉编译器你在声明了一个与父类的同名方法。

        ②如果在子类中想访问父类的同名变量或方法,可以使用关键字“base”+“.”+“变量名”的方法。

class Program
	{
		public static void Main(string[] args)
		{
			Teacher teacher=new Teacher();
			teacher.Talk();
			Console.ReadKey();
		}
	}
class Person
	{
		public void Talk()
		{
			Console.WriteLine("Person can talk");
		}
	}
//教师(子类)
	class Teacher:Person
	{
		new public void Talk()
		{
			base.Talk();
		}
	}

输出结果:

                Person can talk

 ③如果在子类中声明的方法签名与父类不同,构成了重载方法,此时不会隐藏父类方法。

class Program
	{
		public static void Main(string[] args)
		{
			Teacher teacher=new Teacher();
			
            teacher.Talk("Mr.Cai");
			teacher.Talk();
			
            Console.ReadKey();
		}
	}
class Person
	{
		}
		public void Talk()
		{
			Console.WriteLine("Person can talk");
		}
	}
class Teacher:Person
	{
		public void Talk(string name)//方法签名与父类不同
		{

			Console.WriteLine("{0} can talk",name);
		}
	}

输出结果:

                Mr.Cai can talk

                Person can talk

1.3  继承的构造方法

1.3.1 实例化一个子类对象时,其构造方法的调用顺序为:父类构造方法—>子类构造方法。
class Program
	{
		public static void Main(string[] args)
		{
			Teacher teacher=new Teacher();
            Console.ReadKey();
		}
	}
class Person
	{
		public Person()
		{
			Console.WriteLine("Person can talk");
		}
    }
class Teacher:Person
	{
		public Teacher()
		{
			Console.WriteLine("Teacher can talk");
		}
    }

输出结果:

                 Person can talk

                 Teacher can talk

 1.3.2 实例化一个子类对象,但父类只有带参数的构造方法时,需要显式调用父类的构造方法。
class Program
	{
		public static void Main(string[] args)
		{
			Teacher teacher=new Teacher();
            Console.WriteLine();
			Teacher teacher1=new Teacher("Miss Lin");
			Console.ReadKey();
		}
	}
class Person
	{
		public Person(string name)
		{
			Console.WriteLine("{0} can talk",name);
		}
    }
class Teacher:Person
	{
        //传递方法一
		public Teacher():base("Mr.Cai")//显示调用父类构造方法
		{
			Console.WriteLine("Teacher can talk");
		}
            
        //传递方法二
        public Teacher(string name):base(name)
        {
            Console.WriteLine("Teacher can talk");
        }
    }

 输出结果:

                Mr.Cai can talk
                Teacher can talk

                Miss Lin can talk
                Teacher can talk

 1.3.3当子类构造方法显示调用父类的某一构造方法的时候,按照其调用进行选择;但当子类没有显式调用父类构造方法的时候,默认寻找其无参构造方法。
class Program
	{
		public static void Main(string[] args)
		{
			Teacher teacher=new Teacher();
			Console.ReadKey();
		}
	}
class Person
	{
		public Person(string name)
		{
			Console.WriteLine("{0} can talk",name);
		}
		
		public Person()
		{
			Console.WriteLine("Person no parameter");
		}
	}
class Teacher:Person
	{
		public Teacher()
		{
			Console.WriteLine("Teacher no parameter");
		}
		
        public Teacher(string name):base(name)
        {
            Console.WriteLine("Teacher can talk");
        }		
	}

输出结果:

                Person no parameter
                Teacher can talk

以上便为继承的一些概念、使用方法、构造方法等知识。

由此可将继承记为方法隐藏

二、多态

2.1多态的概念

        通过父类对象,调用子类对象中的对应方法来进行子类方法的实现,解决同一行为在不同对象上的使用问题,可以做到父类向后进行寻找和兼容。

2.2 多态的使用方法

       父类与子类方法签名需要一致,并且virtual和override必须同时出现才能构成多态,如果不是同类出现则就是上面介绍的方法隐藏。

        ①virtual关键字:将父类方法写为虚拟方法,可以被子类的重写方法完全抹去,继而不实用父类方法。

        ②override关键字:为子类的重写方法,可以抹去父类的对应方法。

class Program
	{
		public static void Main(string[] args)
		{
            //先实例化三个对象
			Dog dog=new Dog();
			Pig pig=new Pig();
			Animal animal=new Animal();
			
            animal.Eat();

			animal=dog;//把子类狗赋给父类
			dog.Eat();

			animal=pig;//把子类猪赋给父类
			pig.Eat();
		}
	}
	class Animal //父类
	{
		virtual public void Eat()
		{
			Console.WriteLine("Animal eat food");
		}
	}
	class Dog:Animal //子类
	{
		override public void Eat()
		{
			Console.WriteLine("Dog eat bond");
		}
	}
	class Pig:Animal //子类
	{
		override public void Eat()
		{
			Console.WriteLine("Pig eat vegetable");
		}
	}

输出结果:

                Animal eat food

                Dog eat bond

                Pig eat vegetable

 注:父类虚拟方法不能使用private!

以上便是关于多态的的一些概念和使用方法。

由此也可将多态记为方法重写

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值