回顾Abstract和Virtual的用法
今天坐班车的时候,突然就想起来这俩个货了;仔细缕缕,居然越缕越乱较:
上代码吧:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace VirtualAndAbstract
{
class Program
{
static void Main(string[] args)
{
#region MyRegion
//people是 爹:子要在爹的灵魂(抽象)庇佑保护下生存;子在功夫上比爹更高一筹
student stu = new student();//student是子
People p = stu;
p.study();//这是儿子继承爹的心法;
Console.WriteLine(stu.get());//这里是儿子直接拿着老子的兵器,斩杀天下负心汉
Console.ReadKey();
#endregion } #region abstract大法好
public abstract class People
{
public abstract void study();
public abstract int Add();
public virtual string VirtualMethod()
{
return "我是abstract下的virtual方法,我必须要有主体";
}
//在abstract下,非abstract的方法要有方法体:get()和VirtualMethod()
public string get()
{
return "Prozkb";
}
}
public class student : People
{
//爹留下的心法(抽象方法),子要把它全部(老子的家当不能丢)实物化(重载)下来,子也可以对爹的心法进行改良
//
//爹留下的兵器(非抽象方法),子可以直接拿过来杀敌
public override void study()
{
Console.WriteLine("hhxx");
} public override int Add()
{
throw new NotImplementedException();
} }
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace VirtualAndAbstract
{
class Program
{
static void Main(string[] args)
{
#region MyRegion
FatherClass father = new FatherClass();
SonClass son = new SonClass();
FatherClass baba = new SonClass();
father.GetAge(); Console.WriteLine(son.GetName());
son.GetAge(); Console.ReadKey();
#endregion
}
#region virtual
public class FatherClass
{
public virtual string GetName()
{
return "方法体";
}
public virtual string SETName()
{
return "SET方法体";
} public void GetAge()
{
Console.WriteLine("年方二八");
} }
public class SonClass : FatherClass
{
//儿子不需要把得的所有虚方法重载;这一点还是比abstract那个爹类有好多了
//
public override string GetName()
{
return base.GetName();//这里的base相当于爹(FatherClass)
}
#region 儿子重写方法,不能改变老子的参数;不然会走火入魔 //public override string GetName(string name)
//{
// return "我把老子的心法改良了,我就是"+name;
//}
#endregion public static string SonGDCQ()
{
return "我是儿子自创的乾坤大法:干倒苍穹;俺爹不能用我";
} //错误:“VirtualAndAbstract.Program.SonClass.GetStr()”是抽象的,但它包含在非抽象类“VirtualAndAbstract.Program.SonClass”中
//所以,抽象方法要放在抽象类中
// public abstract string GetStr(); }
#endregion
}
}
回顾Abstract和Virtual的用法的更多相关文章
- c#中的interface abstract与virtual介绍
abstract 与virtual : 方法重写时都使用 override 关键字,interface中的方法和abstract方法都要求实现 interface用来声明接口1.只提供一些方法规约, ...
- C#中Abstract和Virtual的区别
c# 中 Abstract和Virtual比较容易混淆,都与继承有关,并且涉及override的使用.下面讨论一下二者的区别: 一.Virtual方法(虚方法) virtual 关键字用于在基类中修饰 ...
- C#中Abstract和Virtual
C#中Abstract和Virtual 在C#的学习中,容易混淆virtual方法和abstract方法的使用,现在来讨论一下二者的区别.二者都牵涉到在派生类中与override的配合使用. 一.Vi ...
- Abstract和Virtual和interface , 派生类中重写 override / new关键字
http://www.cnblogs.com/blsong/archive/2010/08/12/1798064.html C#中Abstract和Virtual 在C#的学习中,容易混淆virtua ...
- C#基础知识系列七(base、this、new、override、abstract、virtual、static)
前言 本文主要来讲解一下C#中,自己觉得掌握的不怎么样或者用的不多,不太熟悉的关键字,主要包括base.this.new.override.abstract.virtual以及针对static字段和s ...
- 基础知识系列☞Abstract和Virtual→及相关知识
转载地址→http://www.cnblogs.com/blsong/archive/2010/08/12/1798064.html 在C#的学习中,容易混淆virtual方法和abstract方法的 ...
- [C#] 區分 abstract、virtual、override 和 new
abstract.virtual.override和new是在類別的繼承關係中常用的四個修飾方法的關鍵字,在此略作總結. 1. 常用的中文名稱: n abstract => 抽象方法. n ...
- c#中的interface abstract与virtual
interface用来声明接口1.只提供一些方法规约,不提供方法主体 如 public interface IPerson { void getName();//不包含方法主体 }2.方法不能 ...
- C#中Abstract和Virtual 【转】
http://www.cnblogs.com/blsong/archive/2010/08/12/1798064.html 在C#的学习中,容易混淆virtual方法和abstract方法的使用,现在 ...
随机推荐
- pandas学习笔记 - 常见的数据处理方式
1.缺失值处理 - 拉格朗日插值法 input_file数据文件内容(存在部分缺失值): from scipy.interpolate import lagrange import pandas as ...
- ASP.NET-文件上传代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...
- OKHttp使用简单介绍
如今android网络方面的第三方库非常多,volley.Retrofit.OKHttp等,各有各自的特点,这边博客就来简介下怎样使用OKHttp. 梗概 OKHttp是一款高效的HTTP客户端,支持 ...
- PHP使用数组实现队列(实际就是先进先出怎样实现)
PHP的数组处理函数还能够将数组实现队列,堆栈是"先进后出". 在堆栈中,最后压入的数据(进栈),将会被最先弹出(出栈).而队列是先进先出.就如同银行的排号机 PHP中将数组当做一 ...
- The broken pedometer-纯暴力枚举
The broken pedometer Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu i ...
- Import Example Dataset
Overview The examples in this guide use the restaurants collection in the test database. The followi ...
- pandas入门10分钟——serries其实就是data frame的一列数据
10 Minutes to pandas This is a short introduction to pandas, geared mainly for new users. You can se ...
- HikariCP--一款高性能的 JDBC 连接池
源码地址:https://github.com/brettwooldridge/HikariCP 使用方法: Java 8 maven artifact: <dependency> < ...
- plt.rcParams[]
plt.rcParams[] pylot使用rc配置文件来自定义图形的各种默认属性,称之为rc配置或rc参数.通过rc参数可以修改默认的属性,包括窗体大小.每英寸的点数.线条宽度.颜色.样式.坐标轴. ...
- [NOIP2014普及组]子矩阵
题目:洛谷P2258.Vijos P1914.codevs 3904. 题目大意:给你一个矩阵,要你找一个r行c列的子矩阵,求最小分值(子矩阵和分值的定义见原题). 解题思路:n和m比较小,考虑暴力. ...