C#快速学习笔记(译)续一
6.虚拟和非虚拟函数
下面是一个非虚拟函数
using System;
namespace Test2 {
class Plane { public double TopSpeed() {return 300.0D; }}
class Jet : Plane { public double TopSpeed() { return 900.0D;}}
class Airport {
static void Main(string[] args) {
Plane plane = new Jet();
Console.WriteLine("planes top speed: {0}",plane.TopSpeed()); //
Console.ReadLine();
}
}
}
因为TopSpeed()是非虚拟的函数,上面的代码将打印出300。为了修正这个问题,我们需要用virtual声明这个方法,并且在子类中用override重写它,如果不设置Jet()为override,结果依然是300,注意观察下面的例子。
class Plane { public virtual double TopSpeed() { return 300.0D;}}
class Jet : Plane { public override double TopSpeed() { return 900.0D;}}
class Airport {
static void Main(string[] args) {
Plane plane = new Jet();
Console.WriteLine("planes top speed: {0}",plane.TopSpeed()); //
Console.ReadLine();
}
7.隐藏继承成员
在子类中,你可以重复使用变量名,但编译器会提醒你,子类的成员是“隐藏”父类的变量。
public class Elephant { public int Weight = ; }
public class AsianElephant : Elephant { public int Weight = ; }
void Main()
{
Console.WriteLine(new AsianElephant().Weight); //
}
如果你真想得到一个新的变量,那么告诉编译器将停止抱怨它,通过使用“new”修饰符(不被与“new”运算符混淆)。
public class AsianElephant : Elephant { public new int Weight = ; }
8.重载运算符示例。注意,血多运算符必须重载为一对,如〉、〈
public class Plane {
public virtual double TopSpeed() { return 300.0D;}
public static bool operator>(Plane one, Plane two) {
return one.TopSpeed() > two.TopSpeed();
}
public static bool operator<(Plane one, Plane two) {
return one.TopSpeed() < two.TopSpeed();
}
}
class Jet : Plane {
public override double TopSpeed() { return 900.0D; }
public override string ToString() { return "I'm a Jet"; }
}
class Airport {
static void Main(string[] args) {
Plane plane = new Jet();
Console.WriteLine("plane's top speed: {0}",plane.TopSpeed());
Jet jet = new Jet();
Console.WriteLine("jet's top speed: {0}",jet.TopSpeed());
Console.WriteLine("Plane > Jet = {0}", plane > jet);
Console.ReadLine();
}
}
9.重载参数
通常在编译时根据声明的类型参数来决定调用重载方法。即使“mamal”的对象是真正的Tiger型,编译器将调用Mammal重载 - 除非它转换为“dynamic”的类型,在这种情况下,它会调用此基础上该方法真正的对象类型。
using System;
namespace ScratchPad
{
public class Linnaeus
{
public class Mammal {}
public class Tiger : Mammal{}
public static void Zoo(Mammal mammal)
{
Console.Out.WriteLine("mammal");
}
public static void Zoo(Tiger tiger)
{
Console.Out.WriteLine("tiger");
}
public static void Main(string[] args)
{
Mammal mammal = new Tiger();
Zoo(mammal); //writes mammal because variable is that type
Zoo((dynamic) mammal); //writes tiger because the object is a Tiger
Console.ReadKey();
}
}
}
10.使用属性访问器方法的例子。注意set和value变量的特殊用途。
public class Plane {
protected double mySpeed = 300.0D;
public double TopSpeed {
get {return mySpeed;}
set { mySpeed = value; }
}
}
class Jet : Plane {
public Jet() { TopSpeed = 900.0D; }
}
class Airport {
static void Main(string[] args) {
Plane plane = new Plane();
Console.WriteLine("plane's top speed: {0}",plane.TopSpeed);
Jet jet = new Jet();
Console.WriteLine("jet's top speed: {0}",jet.TopSpeed);
Console.ReadLine();
}
}
11.输出当前时间
DateTime dt = DateTime.Now;
Console.WriteLine("Current Time is {0} ",dt.ToString());
要指定一个格式: dt.ToString("yyyy/MM/dd")
当前独立的通用格式: dt.ToString("u")
将输出 "yyyy'-'MM'-'dd HH':'mm':'ss'Z'"
12.写入几行文字到文件中
using System.IO;
...
try {
StreamWriter streamWriter = new StreamWriter("tmp.txt"); streamWriter.WriteLine("This is the first line.");
streamWriter.WriteLine("This is the second line.");
} finally {
if(streamWriter != null) {
streamWriter.Close();
}
}
未完待续。
C#快速学习笔记(译)续一的更多相关文章
- Knockout.js快速学习笔记
原创纯手写快速学习笔记(对官方文档的二手理解),更推荐有时间的话读官方文档 框架简介(Knockout版本:3.4.1 ) Knockout(以下简称KO)是一个MVVM(Model-View-Vie ...
- Angular快速学习笔记(2) -- 架构
0. angular 与angular js angular 1.0 google改名为Angular js 新版本的,2.0以上的,继续叫angular,但是除了名字还叫angular,已经是一个全 ...
- Angular 快速学习笔记(1) -- 官方示例要点
创建组件 ng generate component heroes {{ hero.name }} {{}}语法绑定数据 管道pipe 格式化数据 <h2>{{ hero.name | u ...
- CockroachDB学习笔记——[译]CockroachDB中的SQL:映射表中数据到键值存储
CockroachDB学习笔记--[译]CockroachDB中的SQL:映射表中数据到键值存储 原文标题:SQL in CockroachDB: Mapping Table Data to Key- ...
- C#快速学习笔记(译)
下面是通过代码快速学习C#的例子. 1.学习任何语言都必定会学到的hello,world! using System; public class HelloWorld { public static ...
- Java快速学习笔记01
这一波快速学习主要是应付校招笔面试用,功利性质不可避免. 学习网址: http://www.runoob.com/java/java-tutorial.html 执行命令解析: 以上我们使用了两个命令 ...
- CockroachDB学习笔记——[译]在CockroachDB中如何让在线模式更改成为可能
原文链接:https://www.cockroachlabs.com/blog/how-online-schema-changes-are-possible-in-cockroachdb/ 原作者: ...
- CockroachDB学习笔记——[译]Cgo的成本与复杂性
原文链接:https://www.cockroachlabs.com/blog/the-cost-and-complexity-of-cgo/ 原作者:Tobias Schottdorf 原文日期:D ...
- CockroachDB学习笔记——[译]如何优化Go语言中的垃圾回收
原文链接:https://www.cockroachlabs.com/blog/how-to-optimize-garbage-collection-in-go/ 原作者:Jessica Edward ...
随机推荐
- oracle存储过程分页
1.首先在oracle中建包体,用于游标返回当前数据记录集 CREATE OR REPLACE PACKAGE pkg_query AS TYPE cur_query IS REF CURSOR; E ...
- const ;static;extern的使用与作用
const /** const :常量 const ...
- 使用在storyBoard之外的xib创建对象
1.在storyBoard之外的xib 要注意的是:TableView的代理一定要设置为FilesOwner 使用: 方式一: 直接创建对象如下,(如果要使用xib里的控件,那么就要将xib里的控件作 ...
- 用css做类似表格的布局
--2013年6月24日12:08:49 今天突然不想用table了,就在园子里找了几个用css的解决办法,直接上代码: --1.html代码: <!DOCTYPE html PUBLIC &q ...
- I2C驱动程序
i2c_add_driver i2c_register_driver driver->driver.bus = &i2c_bus_type; driver_register(&d ...
- 【数论-数位统计】UVa 11076 - Add Again
Add AgainInput: Standard Input Output: Standard Output Summation of sequence of integers is always a ...
- 【技巧性(+递归运用)】UVa 1596 - Bug Hunt
In this problem, we consider a simple programming language that has only declarations of onedimensio ...
- Hexo中添加emoji表情
国庆的三天假前,都是玩CF和LOL的无限乱斗过来的,输了怨没随机到好的英雄,赢了就高高兴兴的
- javascript工具--控制台详解(转自 阮一峰博客)
大神这篇博客是写在2011年,主要介绍 “Firefox” 浏览器插件 “Firebug” 的操作,如今主流浏览器对控制台都已经提供了很好的支持.我自己用的最多是谷歌的 “chrome” 浏览器,下面 ...
- 北大ACM(POJ1002-487-3279)
Question:http://poj.org/problem?id=1002问题点:字符映射.选重复项及排序. Memory: 1136K Time: 813MS Language: C++ Res ...