里氏转换

1.子类可以赋值给父类。

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)
{
//******
//1.子类可以赋值给父类:如果有一个地方需要一个父类作为参数,我们可以给一个子类代替 //Student s = new Student();
//Person p = s;
Person p = new Student();
//例如
string str = string.Join("|", new string []{"","",""});
Console.WriteLine(str); Console.ReadKey();
}
} public class Person
{
public void PersonSay()
{
Console.WriteLine("Person");
}
}
public class Student:Person
{
public void StudentSay()
{
Console.WriteLine("Student");
}
}
public class Teacher:Person
{
public void TeacherSay()
{
Console.WriteLine("Teacher");
}
} }

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)
{
//******
//1.子类可以赋值给父类:如果有一个地方需要一个父类作为参数,我们可以给一个子类代替 Person p = new Student(); //******
//2.如果父类中装的是子类对象,那么可以将这个父类强转为子类对象。
Student ss = (Student)p;
ss.StudentSay(); Console.ReadKey();
}
} public class Person
{
public void PersonSay()
{
Console.WriteLine("Person");
}
}
public class Student:Person
{
public void StudentSay()
{
Console.WriteLine("Student");
}
}
public class Teacher:Person
{
public void TeacherSay()
{
Console.WriteLine("Teacher");
}
} }

3.子类对象可以调用父类的中的成员,但是父类对象永远只能调用自己的成员

4.

is: 表示类型的转换,如果能够转换成功,则返回一个true,否则返回false。

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)
{
//******
//1.子类可以赋值给父类:如果有一个地方需要一个父类作为参数,我们可以给一个子类代替 Person p = new Student(); //******
//2.如果父类中装的是子类对象,那么可以将这个父类强转为子类对象。
//Student ss = (Student)p;
//ss.StudentSay(); //is转换 if (p is Student)
{
Student tt = (Student)p;
tt.StudentSay();
}
else
{
Console.WriteLine("NO");
} Console.ReadKey();
}
} public class Person
{
public void PersonSay()
{
Console.WriteLine("Person");
}
}
public class Student:Person
{
public void StudentSay()
{
Console.WriteLine("Student");
}
}
public class Teacher:Person
{
public void TeacherSay()
{
Console.WriteLine("Teacher");
}
} }

as:表示类型的转换,如果能够转换,则返回对应的对象,否则返回null。

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)
{
//******
//1.子类可以赋值给父类:如果有一个地方需要一个父类作为参数,我们可以给一个子类代替 Person p = new Student(); //******
//2.如果父类中装的是子类对象,那么可以将这个父类强转为子类对象。
//Student ss = (Student)p;
//ss.StudentSay(); //as转换,看对象PP是否为null,不为null则转换成功
Student pp = p as Student;
pp.StudentSay(); Console.ReadKey();
}
} public class Person
{
public void PersonSay()
{
Console.WriteLine("Person");
}
}
public class Student:Person
{
public void StudentSay()
{
Console.WriteLine("Student");
}
}
public class Teacher:Person
{
public void TeacherSay()
{
Console.WriteLine("Teacher");
}
} }

小例子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Person [] pers = new Person[]; Random r = new Random();
for (int i = ; i < pers.Length;i++ )
{
int rnum = r.Next(, );//1-3
switch (rnum)
{
case : pers[i] = new Person();
break;
case : pers[i] = new Student();
break;
case : pers[i] = new Teacher();
break;
}
} for(int j=;j<pers.Length;j++)
{
if(pers[j] is Student)
{
Student tt = (Student)pers[j];
tt.StudentSay();
}
else if(pers[j] is Teacher)
{
Teacher tt = (Teacher)pers[j];
tt.TeacherSay();
}
else
{
pers[j].PersonSay();
}
} Console.ReadKey();
}
} public class Person
{
public void PersonSay()
{
Console.WriteLine("Person");
}
}
public class Student : Person
{
public void StudentSay()
{
Console.WriteLine("Student");
}
}
public class Teacher : Person
{
public void TeacherSay()
{
Console.WriteLine("Teacher");
}
} }

*********

在C#语言中,共有五种访问修饰符:public、private、protected、internal、protected internal。作用范围如下表:
访问修饰符 说明
public 公有访问。不受任何限制。
private 私有访问。只限于本类成员访问,子类,实例都不能访问。
protected 保护访问。只限于本类和子类访问,实例不能访问。
internal 内部访问。只限于本项目内访问,其他不能访问。
protected internal 内部保护访问。只限于本项目或是子类访问,其他不能访问

最新文章

  1. 关于装完系统出现a disk read error occurred的解决方法
  2. postgresql数据库实用操作
  3. wpf,CollectionViewSource,使用数据过滤 筛选 功能。
  4. MVC 的各个部分都有那些技术来实现?如何实现?
  5. 《.NET 编程结构》专题汇总(C#)
  6. HDU 3401 Trade dp+单调队列优化
  7. 图解Android - Android GUI 系统 (2) - 窗口管理 (View, Canvas, Window Manager)
  8. ImageView中XML属性src和background的区别
  9. XML基础知识
  10. LOV里的值直接引用系统里定义的值集的值,且具有值集的安全性控制
  11. Volatile的那些事
  12. 南京邮电大学java程序设计作业在线编程第七次作业
  13. Some notes in Stanford CS106A(1)
  14. μC/OS-II 创建一个任务的流程
  15. 排序算法--插入排序(Insertion Sort)_C#程序实现
  16. Eclipse 安装阿里巴巴代码规范插件
  17. spark DataFrame 常见操作
  18. Android Studio最全插件整理
  19. 解决tableView中cell动态加载控件的重用问题
  20. PhoneGap 获得APP的VersionName

热门文章

  1. ArcGIS Python 获得坐标
  2. ip效验和ip段的效验
  3. 博客&amp;git收藏
  4. 图解Python 【第五篇】:面向对象-类-初级基础篇
  5. 数据分析 - seaborn 模块
  6. 001-软件架构概览、maven补充【分包工程、合并包、web容器插件】、git补充
  7. 九十一:CMS系统之cms用户模型定义
  8. IDEA 2017 安装和破解
  9. Python3 循环_break和continue语句及循环中的else子句
  10. Pytorch-tensor的维度变化