C#面向对象11 里氏转换
2024-09-05 07:58:33
里氏转换
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 内部保护访问。只限于本项目或是子类访问,其他不能访问
最新文章
- 关于装完系统出现a disk read error occurred的解决方法
- postgresql数据库实用操作
- wpf,CollectionViewSource,使用数据过滤 筛选 功能。
- MVC 的各个部分都有那些技术来实现?如何实现?
- 《.NET 编程结构》专题汇总(C#)
- HDU 3401 Trade dp+单调队列优化
- 图解Android - Android GUI 系统 (2) - 窗口管理 (View, Canvas, Window Manager)
- ImageView中XML属性src和background的区别
- XML基础知识
- LOV里的值直接引用系统里定义的值集的值,且具有值集的安全性控制
- Volatile的那些事
- 南京邮电大学java程序设计作业在线编程第七次作业
- Some notes in Stanford CS106A(1)
- μC/OS-II 创建一个任务的流程
- 排序算法--插入排序(Insertion Sort)_C#程序实现
- Eclipse 安装阿里巴巴代码规范插件
- spark DataFrame 常见操作
- Android Studio最全插件整理
- 解决tableView中cell动态加载控件的重用问题
- PhoneGap 获得APP的VersionName