Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1721354
  • 博文数量: 171
  • 博客积分: 11553
  • 博客等级: 上将
  • 技术积分: 3986
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-25 20:28
文章分类

全部博文(171)

文章存档

2012年(2)

2011年(70)

2010年(9)

2009年(14)

2008年(76)

分类: C/C++

2008-03-28 00:11:04

using System;

///



/// 一个类继承自另一个类,子类对父类的调用方法

/// 子类重写父类的方法

///


public class Window
{
    public Window()
    {
        top = 0;
        left = 0;
    }
    //构造函数

    //参数

    //指定左上角座标(left,top)

    public Window(int top, int left)
    {
        this.top = top;
        this.left = left;
    }
    //显示当前左上角坐标值

    public void DrawWindow()
    {
        Console.WriteLine("Drawing Window at ({0},{1})", top, left);
    }

    public static void test()
    {
        Window win = new Window(10, 20);
        win.DrawWindow();
    }
    private int top = 0;
    private int left = 0;
}

public class ListBox : Window
{
    public ListBox():base()
    {
        
    }
    //调用父类的构造方法

    public ListBox(int top, int left, string theContent):base(top,left)
    {
        m_theContent = theContent;
    }
    //绘制ListBox,先调用父类的方法再执行ListBox的方法

    public new void DrawWindow()
    {
        base.DrawWindow();
        Console.WriteLine("Content of ListBox is {0}", m_theContent);
    }
    //此函数也覆盖了Window的test方法

    public static void test()
    {
        ListBox listBox = new ListBox(40, 80,"ListBox");
        listBox.DrawWindow();
    }
    
    private string m_theContent = null;
}

//基类实现一个虚函数Button,

//其两个子类CheckButton和RadioButton分别实现各自的绘制函数

public class Button
{
    public Button(){}
    //实现一个虚方法,绘制自己的内容

    public virtual void DrawButton()
    {
        Console.WriteLine("DrawButton");
    }
};

public class RadioButton : Button
{
    public RadioButton() : base() { }
    public override void DrawButton()
    {
        //先绘制父类,再绘制自己定义的内容

        base.DrawButton();
        Console.WriteLine("RadioButton");
    }

}
public class CheckButton : Button
{
    public CheckButton() : base() { }
    public override void DrawButton()
    {
        //先绘制父类,再绘制自己定义的内容

        base.DrawButton();
        Console.WriteLine("CheckButton");
    }
}
public class VirtualButton
{
    /****************************************************
     * 创建三个Button类型的对象引用,分别指向三个类的实例
     * 都调用函数DrawButton,其输出结果各不相同
     * **************************************************/

    public static void test()
    {
        Button b1 = new Button();
        b1.DrawButton();
        Button b2 = new RadioButton();
        b2.DrawButton();
        Button b3 = new CheckButton();
        b3.DrawButton();
    }
}
/************************************************************************/
/*
 * 创建虚拟函数的基类,其子类分别使用override和new,查看函数的调用情况
 */

/************************************************************************/
public class Inheritance
{
    public Inheritance(){}
    //用于override的虚函数

    public virtual void OverrideDemo()
    {
        Console.WriteLine("Override Demo");
    }
    //用于new的虚函数

    public virtual void NewDemo()
    {
        Console.WriteLine("NewDemo");
    }
};
/*
 * 继承自Inheritance类,用于演示override关键字
 */


public class OverrideKeyWord : Inheritance
{
    public override void OverrideDemo()
    {
        //base.OverrideDemo();

        Console.WriteLine("OverrideKeyWord");
    }
};
/*
 * 继承自Inheritance类,用于演示new关键字
 */

public class NewKeyWord : Inheritance
{
    public new void NewDemo()
    {
        //base.NewDemo();

        Console.WriteLine("NewKeyWord");
    }
};
/*
 * 关键字override和new的测试类
 */

public class KeyWords
{
    public static void test()
    {
        Inheritance ihKeyWord = new Inheritance();
        Inheritance orKeyWord = new OverrideKeyWord();
        Inheritance nwKeyword = new NewKeyWord();
        //调用Inheritance的OverrideDemo

        ihKeyWord.OverrideDemo();
        //调用Ineritance的NewDemo

        ihKeyWord.NewDemo();
        //调用OverrideKeyWord的OverrideDemo

        orKeyWord.OverrideDemo();
        //调用Inheritance的NewDemo,将不调用NewKeyWord的NewDemo

        nwKeyword.NewDemo();
    }
};

/*
 *创建一个抽象类
 */

abstract public class AbstractMsg
{
    abstract public void CallAbstract();
}
/**
 * 创建一个AbstractMsg的一个子类
 **/

public class SendMsg : AbstractMsg
{
    public override void CallAbstract()
    {
        Console.WriteLine("SendMsg");
    }
}
/*
 * 创建一个AbstractMsg的一个子类
 */

public class RecvMsg : AbstractMsg
{
    public override void CallAbstract()
    {
        Console.WriteLine("RecvMsg");
    }
}
/*
 * 创建抽象类的测试类
 */

public class CallAbstract
{
    public static void test()
    {
        AbstractMsg am = new SendMsg();
        am.CallAbstract();
        am = new RecvMsg();
        am.CallAbstract();
    }
}
/*
 * 创建一个Sealed类
 */

sealed public class Sealed
{
    public void CallSealed()
    {
        Console.WriteLine("Sealed");
    }
}
/*
 * 如果从Sealed类继承,则会出现如下的错误提示
 * 'SubSealed': cannot derive from sealed type 'Sealed'    
 * public class SubSealed : Sealed
 * {
 * }
 *
 */


public class CallSealed
{
    public static void test()
    {
        Sealed sl = new Sealed();
        sl.CallSealed();
    }
}

/*
 * 继承自Object类,重载toString方法
 */

public class CommonObj
{
    public override string ToString()
    {
        Console.WriteLine("CommonObj");
        return base.ToString();
    }
}
/*
 * Object类测试
 */

public class CallCommonObj
{
    public static void test()
    {
        CommonObj co = new CommonObj();
        Console.WriteLine(co);
    }
}
/*
 * Box一个int类型数值
 */

public class BoxBuiltIn
{
    public void Box(Object o)
    {
        Console.WriteLine(o);
    }
}
/*
 * Box的一个测试类
 */

public class CallBoxBuiltIn
{
    public static void test()
    {
        BoxBuiltIn bbi = new BoxBuiltIn();
        bbi.Box(100);
    }
}
/*
 * 建立一个unbox类
 */

public class UnboxObject
{
    public void Unbox(int n)
    {
        Console.WriteLine(n);
    }
}
/*
 * 测试UnboxObject的类
 */

public class CallUnboxBuiltIn
{
    public static void test()
    {
        Object o = 100;
        UnboxObject uo = new UnboxObject();
        uo.Unbox((int)o);
    }
}
/*
 * 创建一个嵌套类,并在类中使用嵌套类的静态方法和一般方法
 */

public class OuterClass
{
    public class InnerClass
    {
        public static void WriteLine()
        {
            Console.WriteLine("InnerClass");
        }
        public void Write()
        {
            Console.WriteLine("WriteInner");
        }
    }
    public void WriteLine()
    {
        InnerClass ic = new InnerClass();
        ic.Write();
        InnerClass.WriteLine();
    }
}
/*
 * 嵌套类的一个测试类
 */

public class CallOuterClass
{
    public static void test()
    {
        OuterClass oc = new OuterClass();
        oc.WriteLine();
    }
};


文件:Window.rar
大小:1KB
下载:下载
阅读(2370) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2009-11-20 23:30:15

谢谢提供代码下载,学习学习