Part 21 Inheritance in c#

Why Inheritance

Pillars(支架) of Object Oriented Programming

1,Inheritance(继承)

2,Encapsulation(封装)

3,Abstraction(抽象类)

4,Poymorphism(多态)

1,Inheritance is one of the primary pillars of object oriented programming.

2,It allows code reuse.(重用)

3,Code reuse can reduce time and errors.

Note:You will specify(指定) all the common fields, properties,methods in the base class,which allows reusability.In the derived class you will only have fields, properties and methods,that are specific to them.

Inheritance Syntax

1,using : to inheritance

2,C# supports only single class inheritance.

3,C#supports multiple interface inheritance.

4,Child class is a specialization(特殊化) of base class.

5,Base classes are automatically instantiated(实例化) before derived classes.

6,Parent class constructor executes before Child Class constructor.

7,Using base keyword to specify which Constructor methods .

Part 22 Method hiding in c#

using the new keyword to hide a base class memeber.You will get a compiler(编译器) warning,if you miss the new keyword.

Different ways to invoke a hidden base class member from derived class

1,Use base keyword.

2,Cast child type to parent type and invoke the hidden member

3,ParentClass pc = new ChildClass();

pc.ParentClassMethod();

public class Parent
{
public void Method()
{
Console.Write("Parent Method.");
}
}
public class Child:Parent
{
public new void Method() //if miss the new keyword wiil get a compiler warning.
{
     base.Method(); //invoke Parent Method.
Console.Write("Child Method.");
}
}
public class Program
{
public static void Main()
{
Child c = new Child();
c.Method(); //invoke from Child
     ((Parent)c).Method(); //invoke from Parent
     Parent p = new Child();
     p.Method(); //invoke from Parent.
Child cc = new Parent(); //will get an compiler error
}
}

Part 23 Polymorphism in c#

Polymorphism is one of the primary pillars of object-oriented programming.

Polymorphism allows you to invoke derived class methods through a base class reference during runtime.

In the base class the method is declared virtual, and in the derived class we override the same method.

The virtual keyword indicates, the method can be overridden in any derived class.

public class Employee
{
public virtual void Print() //virtual is keywork for ChildClass to override.
{
Console.Write("Employee");
}
}
public class PartTimeEmployee:Employee
{
public override void Print()
{
Console.Write("PartTimeEmployee");
}
}
public class FullTimeEmployee:Employee
{
public override void Print()
{
Console.Write("FullTimeEmployee");
}
} public class Program
{
public static void Main()
{
Employee[] employees = new Employee[3];
Employees[0] = new Employee();
Employees[1] = new PartTimeEmployee();
Employees[2] = new FullTimeEmployee();
foreach(Employee e in employees)
{
e.Print();//if ChildClass method don't have override keyword,it will invoke ParentClass method,else,invoke ChildClass method.
}
}
}

Part 24 Difference between method overriding and method hiding

In method overriding a base class reference variable pointing to a child class object, will invoke the overriden method in the child class

In method hiding a base class reference variable pointing to a child class object, will invoke the hidden method in the base class.

public class Parent
{
public virtual void Method()
{
Console.Write("Parent Method.");
}
}
public class Child:Parent
{
public override (or new) void Method()
{   
Console.Write("Child Method.");
}
}
public class Program
{
public static void Main()
{
Parent p = new Child();
p.Method();// if Child method use override keyword,it will invoke Child Method,if use new keyword,it will invoke Parent Method.
}
}

Part 25 Method overloading in c#

Function overloading and method overloading terms are(把..称为..) used interchangeably(互换).

Method overloading allows a class to have multiple methods with the same name, but, with a different signature(签名). So, in C# functions can be overloaded based on the number, type(int, float etc), and kind(Value, Ref or Out) of parameters.

The signature of a method consists of the name of the method and the type, kind, and the number of its formal parameters. the signature of a method does not include the return type and the params modifier. So, it is not possible to overload a function, just based on the return type or params modifier.

Part 26 Why Properties

Marking the class fields public and exposing(暴露) to the external world is bad, as you will not have control over what gets assigned(赋值) and returned.

简单的说,为了保护数据安全和完整性,所以需要属性。

Part 27 Properties in C#

In C# to encapsulate(封装) and protect fields we use properties

1,We use get and set accessors(访问器) to implement properties

2,A property with only get accessor is a Read only property

3,A property with only set accessor is a Write only property

4,A property with both get and set accessor is Read/Write property

Note:The advantage of properties over traditional(传统的) Get() and Set() methods is that, you can access(访问) them as if they were public fields.

Auto implemented Properties

if there is no additional logic in the property accessors, then we can make use of auto implemented properties introduced in C# 3.0

Auto-implemented properties reduce the amount of code that we have to write.

When you use auto-implemented properties, the compiler creates a private, anonymous field that can only be accessed through the property's get and set accessors.

Part 28 Structs in C#

Structs

Just like calsses, structs can have

1,Private Fields

2,Public Properties

3,Constructors

4,Methods

Object initializer syntax(语法), introduced in C# 3.0 can be used to initialize either a struct or a class

public class Person
{
string name;
int age;
public Person(string name,int age)
{
this.name = name;
this.age = age;
}
public void Print()
{
Console.Write(this.name+this.age);
}
}
public class Program
{
public static void Main()
{
Person p = new Person("gester",24);
p.Print();
Person p1 = new Person();
p1.name = "gester1";
p1.age =24;
p1.Print();
//C# 3.0
Person p2 = new Person{
name ="gester2";
age =24;
}
p2.Print();

Part 29 Difference between classes and structs in c#

a struct is a value type where as a class is a reference type.(结构是值类型,类是引用类型)

All the differences that are applicable(适用于) to value types and reference types are also applicable to classes and structs

Structs are stored(存储) on stack(栈), where as classes are stored on the heap(堆).

value type hold their value in memory where they are declared, but reference types hold a reference to an object in memory.

Value types are destroyed(销毁) immediately(马上) after the scope(应用范围,也就是大括号) is lost, where as for reference types only the reference variable(变量) is destroyed after the scope is lost. The object is later destroyed by garbage collector.

when you copy a struct into another struct, a new copy of that struct gets created and modifications on one struct will not affect the values contained by the other struct.

when you copy a class into another class, we only get a copy of the reference variable, both the reference variables point to the same object on the heap. so, operations on one variable will affect the values contanined by the other reference variable.

Classes Vs Structs

Structs can't have destructors, but classes can have destructors.(结构不能有析构函数,类可以有)

Structs cannot have explicit parameter less constructor where as a class can.(结构不可以有无参构造函数,类可以有)

Struct can't inherit from another class where as a class can. Both structs and classes inherit form an interface.(结构不能继承类,类可以继承结构,类和结构都可以继承接口)

Example of structs in the .NET Framework -int(System.Int32),double(System.Double) etc.

Note1: A class or a struct cannot inherit form another struct. when Struct are sealed stypes.

Note2:How do you prevent a class from being inherited? Or what is the significance of sealed Keyword?

Part 30 Interfaces in c#

We create interfaces using interface keyword. just like classes interfaces also contains properties, methods, delegates or events, but only declarations and no implementations.

It is a compile time error to privide implementations for any interface member.

Interface members are public by default, and they don't allow explicit(显式) access modifiers.

Interfaces cannot contain fields.(接口不可以包含字段)

If a class or struct inherits from an interface , it must provide implementation for all interface members. Otherwise, we get a compiler error.

A class or a struct can inherit from more than one interface at the same time, but where as, a class cannot inherit from more than oncee class at the same thime.

Interfaces can inherit from other interfaces. A class that inherits this interface must provide implementation for all interface members in the entire interface inheritance chain(一系列)

We cannot create an instance of an interface, but an interface reference variable can point to a derived class object.(接口不可以实例化,可是接口的引用可以指向子类,例如:IA a = new IA(); 这个是错的,可是如果类A继承了接口IA,那么这样是可以的:IA a = new A();

C# for Beginner Part 21 to 30的更多相关文章

  1. HEALTH_WARN too few PGs per OSD (21 < min 30)解决方法

    标签(空格分隔): ceph,ceph运维,pg 集群环境: [root@node3 ~]# cat /etc/redhat-release CentOS Linux release 7.3.1611 ...

  2. JAVA程序1,1,2,3,5,8,13,21....第30个是什么...?

    解题思路:从第3个数字开始,后一个数字是前2个数字的和public class text{ public static void main(String[] args) { int num1=1,nu ...

  3. ffmpeg-201612[01,08,10,17,21,27,30]-bin.7z

    ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 5 屏幕横向放大 20 像素 6 屏幕横向缩小 20 像素 S 下一帧 [ -2秒 ] +2 ...

  4. C语言基础:初级指针 分类: iOS学习 c语言基础 2015-06-10 21:50 30人阅读 评论(0) 收藏

    指针:就是地址. &   取地址运算符 %p   打印地址占位符 int a=0; printf("%p ",&a);    指针变量:用来存放地址的变量 定义: ...

  5. 【转】Oracle基础结构认知—初识oracle 礼记八目 2017-12-12 21:19:30

    Oracle服务器(oracle server)由实例和数据库组成.其中,实例就是所谓的关系型数据库管理系统(Relational Database Management System,RDBMS), ...

  6. 面试1 SQL SERVER 查询第20行到30之间的数据

    SQL SERVER 查询第20行到30之间的数据 1.先查询前20行的ID,后查询除去20条记录的前10条记录 SELECT TOP 10 * FROM tbBank WHERE BankID NO ...

  7. PKUSC2018训练日程(4.18~5.30)

    (总计:共66题) 4.18~4.25:19题 4.26~5.2:17题 5.3~5.9: 6题 5.10~5.16: 6题 5.17~5.23: 9题 5.24~5.30: 9题 4.18 [BZO ...

  8. SQL SERVER 查询第20行到30之间的数据

    1.先查询前20行的ID,后查询除去20条记录的前10条记录 SELECT TOP * FROM tbBank WHERE BankID NOT IN(SELECT TOP BankID FROM t ...

  9. C#语句2——循环语句(for循环与for循环嵌套)

    循环:反复执行某段代码. 循环四要素:初始条件,循环条件,循环体,状态改变. for(初始条件;循环条件;状态改变) { 循环体 } break ——中断循环,跳出整个循环 continue——停止本 ...

随机推荐

  1. NWERC2020J-Joint Excavation【构造,贪心】

    正题 题目链接:https://codeforces.com/gym/103049/problem/J 题目大意 \(n\)个点\(m\)条边的一张无向图,选出一条路径后去掉路径上的点,然后将剩下的点 ...

  2. CF25E-Test【AC自动机,bfs】

    正题 题目链接:https://www.luogu.com.cn/problem/CF25E 题目大意 给出三个串,然后求一个最短的串包含这三个串. \(1\leq |s_1|,|s_2|,|s_3| ...

  3. 揭秘:懂Python的测试员薪资到底有多高?

    前言 面试的时候,面试官经常会问:会Python吗?有在工作中写过项目吗?会搭建自己的框架吗?我:恩,我只简单写过一些demo. 有时候问一些简单的Python,一问就会懵.比如:json和字典有什么 ...

  4. Kettle学习笔记(四)— 总结

    目录 Kettle学习笔记(一)- 环境部署及运行 Kettle学习笔记(二)- 基本操作 kettle学习笔记(三)- 定时任务的脚本执行 Kettle学习笔记(四)- 总结 Kettle中设置编码 ...

  5. 初探计算机网络之TCP/IP网络协议

    网络协议 ​ 在计算机诞生以来,从最原始的单机模式到现在多台计算机协同工作,形成计算机网络,从前很难想象的信息共享.多机合作.大规模计算在今天也早已成了现实.在早期,计算机网络需要解决的痛点,就是怎样 ...

  6. iOS Swift逻辑运算符

    运算符 运算符分类 从操作数角度看:运算符包括一元.二元.三元.这里的一二三指操作数的数量,操作数指的是被操作的数值. 从运算符位置看:运算符分为前缀.中缀.后缀.例如:!b, b + c, c! 赋 ...

  7. 重磅 | 阿里开源首个 Serverless 开发者平台 Serverless Devs

    Serverless 从概念提出到应用,已经走过了 8 个年头,开发者对 Serverless 的使用热情不断高涨.为帮助开发者实现一键体验多云产品,极速部署 Serverless 项目,10 月 2 ...

  8. typora博客笔记上传图片时不能显示

    前言 markdown具有轻量化.易读易写等特性,并且对于图片.超链接.图片.数学公式都有支持. 但是最近在使用Typora的过程中我发现,在写文章笔记的时候导入的图片,因为图片保存在我们电脑本地,当 ...

  9. 基于linux在线预览

    1.Libreoffice安装 在服务器上安装Libreoffice,在这里就不多说了, import os import sys import subprocess import re def co ...

  10. C/C++入门级小游戏——开发备忘录

    很多工科的学生在大一都有一门课程,叫C语言程序设计.大概就是装个IDE然后和一个黑乎乎的窗口打交道,期末到了考完试就结束了.然而很多人可能都有一个疑惑:C语言究竟能干什么?除开嵌入式单片机这些高大上的 ...