zoukankan      html  css  js  c++  java
  • 父类与子类的转换as,is

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace 子类与父类的相互转换
    {
        class Program
        {
            static void Main(string[] args)
            {
                //try catch finally 与 continue
                //如果在try中遇到continue,则忽略try中continue之后的语句
                //但是依然执行finally中语句
                //finally之外的语句也不执行
                bool _flag = true;
                while(true)
                {
                    try
                    {
                        //if(_flag)
                        //    continue;
                        
                        //如果_falg为true,这下面的两句不执行
                        //Person per = new Student();
                        //per.Say();//此时输出father 
    
                        //类的转换:as is
                        //1. is:返回bool类型,指示是否可以做这个转换
                        //2. as:如果转换成功,则返回对象,否则返回null
                        //作用:我们可以将所有的子类当做是父类来看,针对父类进行编程
                        //      可以写出通用代码,从而适应需求的不断改变  
                        Person per = new Student();
    
                        Student stu1 = per as Student;
                        if(stu1 != null)
                        {
                            stu1.Say();
                        }
    
                        Teacher tea1 = per as Teacher;
                        if(tea1 == null)
                        {
                            Console.WriteLine("转换失败");
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    finally
                    {
                        //如果try中执行了continue,则这两句依然要执行
                        Console.WriteLine("finally");
                        Console.ReadKey();
                    }
    
                    //如果在try中执行continue,则下面的两条语句并不执行
                    Console.WriteLine();
                    Console.ReadKey();
                }
                
            }
        }
    
        class Person
        {
            public void Say()
            {
                Console.WriteLine("father");
            }
            
        }
    
        class Teacher:Person
        {
            public new void Say()
            {
                Console.WriteLine("Teacher");
            }
        }
    
        class Student:Person
        {
            public new void Say()
            {
                Console.WriteLine("Student");
            }
        }
         
    }
    

      

  • 相关阅读:
    bzoj2733: [HNOI2012]永无乡
    bzoj3141: [Hnoi2013]旅行
    bzoj3144: [Hnoi2013]切糕
    bzoj3140: [Hnoi2013]消毒
    bzoj3139: [Hnoi2013]比赛
    bzoj3142: [Hnoi2013]数列
    bzoj3572: [Hnoi2014]世界树
    bzoj2286: [Sdoi2011]消耗战
    bzoj3611: [Heoi2014]大工程
    The Unsolvable Problem
  • 原文地址:https://www.cnblogs.com/my-cat/p/7612356.html
Copyright © 2011-2022 走看看