c#入门经典 原书代码Ch11Ex04

 

using  System;
using  System.Collections.Generic;
using  System.Text;

namespace  Ch11Ex04
{
    
interface IMyInterface
    
{ }

    
class ClassA : IMyInterface
    
{ }

    
class ClassB : IMyInterface
    
{ }

    
//ClassC 类没有任何继承
    class ClassC
    
{ }

    
class ClassD : ClassA
    
{
    }


    
struct MyStruct : IMyInterface
    
{
    }


    
class Checker
    
{
        
public void Check(object paraml)
        
{
            
if (paraml is ClassA)
                Console.WriteLine(
"Variable can be converted to ClassA.");
            
else
                Console.WriteLine(
"Variable can't be converted to ClassA.");

            
if (paraml is IMyInterface)
                Console.WriteLine(
"Variable can be converted to IMyInterface.");
            
else
                Console.WriteLine(
"Variable can't be converted to IMyInterface.");

            
if (paraml is MyStruct)
                Console.WriteLine(
"Variable can be converted to MyStruct.");
            
else
                Console.WriteLine(
"Variable can't be converted to MyStruct.");           
        }


    }


    
class Program
    
{
        
static void Main(string[] args)
        
{
            Checker check 
= new Checker();
            ClassA try1 
= new ClassA();
            ClassB try2 
= new ClassB();
            ClassC try3 
= new ClassC();
            ClassD try4 
= new ClassD();

            MyStruct try5 
= new MyStruct();
            
object try6 = try5;
            Console.WriteLine(
"Analyzin ClassA type variable:");
            check.Check(try1);

            Console.WriteLine(
" Analyzin ClassB type variable:");
            check.Check(try2);

            Console.WriteLine(
" Analyzin ClassC type variable:");
            check.Check(try3);

            Console.WriteLine(
" Analyzin ClassD type variable:");
            check.Check(try4);

            Console.WriteLine(
" Analyzin MyStruct type variable:");
            check.Check(try5);

            Console.WriteLine(
" Analyzin boxed MyStruct type variable:");
            check.Check(try6);
            Console.ReadKey();
        }

    }

}
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值