例子1--C#继承的常见问题:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Animal animal = new Animal();
animal.Introduce();
Console.WriteLine(animal.word); //父类指向子类 是可以的
Animal cat = new Cat();
cat.Introduce();
Console.WriteLine(cat.word); //子类指向父类才是不能的 所以下面代码将会报错 不能说 “父亲像儿子”
//Cat cat2 = new Animal();
//cat2.Introduce();
//Console.WriteLine(cat2.word); //Dog类中没有重写父类的虚方法 所以调用的是父类的Introduce方法
Animal dog = new Dog();
dog.Introduce();
Console.WriteLine(dog.word); //子类和父类存在的同名方法 但是子类的方法没用Override重写 此时调用的是子类的方法
Bird bird = new Bird();
bird.Introduce();
Console.WriteLine(bird.word); //子类和父类存在的同名方法 如果new出来的是父类指向子类 而且子类没有重写父类的虚方法 则调用的是父类的同名方法
Animal bird2 = new Bird();
bird2.Introduce();
Console.WriteLine(bird2.word); }
}
public class Animal
{
public string word;
public virtual void Introduce()
{
word = "动物";
}
} public class Cat : Animal
{
public override void Introduce()
{
//重写后继续使用父类的Inroduce方法
//base.Introduce();
//子类自己的
word = "猫";
}
} public class Pig : Animal
{
public override void Introduce()
{
word = "猪";
}
} public class Dog : Animal
{ } public class Bird : Animal
{
//这里没有重写父类的Introduce方法 而是Bird类自己的
public void Introduce()
{
word = "鸟";
}
}
}

结果:

例子2--子类父类字段与方法的优先级:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//优先级
Father son = new Son();
//如果是父类指向子类 “儿子像父亲”所以优先调用父类的同名变量type 故输出“初代”
Console.WriteLine(son.type);
//强制转换为Son 此时输出的是“第二代”
Console.WriteLine(((Son)son).type);
//当调用的是方法时 输出的是子类重写的方法
Console.WriteLine(son.SayHi());
//子类和父类存在的同名方法 如果new出来的是父类指向子类 而且子类没有Override重写父类的虚方法 则调用的是父类的同名方法
//上例子1中的 Animal bird2
}
}
public class Father
{
public string type = "初代"; public virtual string SayHi()
{
return "大家好!我是初代.";
}
} public class Son:Father
{
public string type = "第二代"; public override string SayHi()
{
return "大家好!我是第二代.";
}
}
}

结果:

结论:

子类指向子类

——则优先调用子类自生的所有成员

父类指向子类

——则优先使用父类的所有成员字段(若要调用则强制转换)

——子类重写父类的成员方法则优先调用子类的

——若子类没有重写父类的成员方法而保持同名 则优先调用父类的同名方法

C# -- 继承规则的更多相关文章

  1. css样式继承规则详解

    css样式继承规则详解 一.总结 一句话总结:继承而发生样式冲突时,最近祖先获胜(最近原则). 1.继承中哪些样式不会被继承? 多数边框类属性,比如象Padding(补白),Margin(边界),背景 ...

  2. java泛型类的继承规则

    首先看一看java泛型类的使用: /** * 一个泛型方法:使程序更加安全 * 并且能被更多的使用 * @author 丁** * * @param <T> */ class Pair&l ...

  3. 理解Python中的继承规则和继承顺序

    先来看一段代码: class First(object): def __init__(self): print ("first") class Second(object): de ...

  4. Swift难点-继承中的构造规则实例具体解释

    关于继承中的构造规则是一个难点. 假设有问题,请留言问我. 我的Swift新手教程专栏 http://blog.csdn.net/column/details/swfitexperience.html ...

  5. C#继承简介与规则

    一.C#继承简介 1. 类的层次结构 下面是一个类的层次结构图: 上图反映了鱼类的派生关系,其中最高层的实体往往具有最一般最普遍的特征,越下层的实体就越具体,并且下层包含了上层的特征.如果将上层的实体 ...

  6. python作用域和多继承

    python作用域 python无块级作用域 看c语言代码: #include<stdio.h> int main() { > ) { ; } printf("i = %d ...

  7. 学习Sass 的基本语法规则[Sass和compass学习笔记]

    自从发现可编程的css语法 Sass和基于Sass的css库compass 一个给我的感觉像c# 另外一个给我的感觉像.NET Framework,一切都为了提升开发效率和降低开发大型web的门槛. ...

  8. python学习第十六天 --继承进阶篇

    这一章节主要讲解面向对象高级编程->继承进阶篇,包括类多继承介绍和继承经典类和新式类属性的查找顺序不同之处. 多继承 上一章节我们讲到继承,子类继承父类,可以拥有父类的属性和方法,也可以进行扩展 ...

  9. Sass 的基本语法规则

    转自:http://www.cnblogs.com/qqloving/p/3676852.html 自从发现可编程的css语法 Sass和基于Sass的css库compass 一个给我的感觉像c# 另 ...

随机推荐

  1. HTML URL 编码:请参阅:http://www.w3school.com.cn/tags/html_ref_urlencode.html

    http://www.w3school.com.cn/tags/html_ref_urlencode.html

  2. 分享一个关于R语言黄皮书的网站!

    这个网站上有许多关于R语言黄皮书的系列书籍,而且都是免费的望珍惜 http://bbs.pinggu.org/thread-870446-1-1.html

  3. windows 里面waveOut*接口应用

    #include <windows.h>#include <mmsystem.h>#include <stdio.h>/** some good values fo ...

  4. linux-常用指令1

    掌握下面的命令是最基本的噢!那是我们使用一个系统最基本的操作. 玩过dos么,其实,linux下的文件操作和dos差不多.没什么难的,多练习就记住了.下面如果有条件的话请跟我一样操作吧!百看不如一做. ...

  5. guava_学习_00_资源帖

    一.精选 1.Google Guava 官方教程 二.参考资源 1.Google Guava官方教程(中文版) 2.使用Guava编写优雅代码 3.Google guava工具类的介绍和使用

  6. ADO:连接,执行语句与关闭(sql server数据库)

    一,身份验证: sql server数据库连接身份验证有两种:windows身份验证和SQL Server身份验证 windows验证:是使用windows的安全子系统对用户连接进行有效性验证.(个人 ...

  7. Spring MVC表单提交

    实际应用中,列表中的单条记录的修改,可能需要传很多对象参数到后台服务器,Spring MVC表单标签<form:> 提供了一种简洁的提交方式. <form id="form ...

  8. 51nod 1149 Pi的递推式 组合数

    题目大意: \(F(x) = 1 (0 \leq x < 4)\) \(F(x) = F(x-1) + F(x-\pi) (4 \leq x)\) 给定\(n\),求\(F(n)\) 题解: 我 ...

  9. 11g RAC 如何备份OCR,利用备份恢复OCR,ocrdump

    OCR备份 OCR的备份有2种方式,自动备份和手工备份. 自动备份策略: Oracle Clusterware 每隔4小时,CRSD 进程会自动对OCR 进行一次备份,在任意时刻,oracle 总会保 ...

  10. hdu 5909 Tree Cutting —— 点分治

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5909 点分治,每次的 rt 是必选的点: 考虑必须选根的一个连通块,可以DP,决策就是在每个子树中决定选不 ...