C#学习进程-面向对象-多态

来源:互联网 发布:淘宝法克鞋店 编辑:程序博客网 时间:2024/06/05 15:02
有四种形式的多态
  1.虚函数多态
  2模板多态

  3重载(Overload)

重载-类中定义的方法可能有不同的版本

public bool withdraw(int amt,string name)

public bool withdraw(int amt)

特点(两必须一可以)

方法名必须相同

参数列表必须不相同

返回值类型可以不相同(这个自己理解一点就不详细写了)

  4转换


 

虚拟函数

1、声明虚方法

使用virtual 关键字 public virtual bool withdraw(..);

2、调用虚方法,运行时将确定调用对象是什么类的实例,并调用适当的覆写的方法。

3、虚方法可以有实现体。

 

覆写(0verride

1、子类中为满足自己的需要来重复定义某个方法的不同实现覆写

2、通过使用关键字Override来覆写

Public override bool withdraw(…)

3、只有虚方法和抽象方法才能被覆写

4、要求:(三相同)

相同的方法名称

相同的参数列表

相同的返回值类型

 

Items

Override覆写

Overload重载

位置

存在于继承关系的类中

存在于同一类中

方法名

相同

相同

参数列表

相同

必须不同

返回值

相同

可以不相同

 

抽象方法

1、抽象方法是必须被派生类覆写的方法。

2、抽象方法是可以看成是没有实现体的虚方法

3、如果类中包含抽象方法,那么类就必须定义为抽象类,不论是否还包含其它一般方法

public abstract bool withdraw(……);

 

虚拟函数与覆写实例

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

 

namespace 虚拟函数与覆写

{

    public partial class Form1 : Form

    {

        //private checkaccount chk;

        account acc;//多态

        public Form1()

        {

            InitializeComponent();

            //chk = new checkaccount();

            //chk.balance = 1000;

            //this.label1.Text = chk.balance.ToString();

 

           //多态

            acc = new checkaccount();

            acc.balance = 1000;

            this.label1.Text = acc.balance.ToString();

        }

          public class account

        {

            public int balance;

            public virtual bool withdraw(int amt)

            {

                balance -= amt;

                return true;

            }

        }

        public class checkaccount : account

        {

            public string info;

            public override bool withdraw(int amt)

            {

                if (amt <= base.balance)

                    return base.withdraw(amt);

                else

                    return false;

            }

 

        }

               

        private void button1_Click(object sender, EventArgs e)

        {

            //chk.withdraw(Int32.Parse(this.textBox1.Text));

            //this.label1.Text = chk.balance.ToString();

            acc.withdraw(Int32.Parse(this.textBox1.Text));

            this.label1.Text = acc.balance.ToString();

        }

    }

}

 

抽象方法与覆写实例

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

 

namespace 抽象方法与覆写

{

    public partial class Form1 : Form

    {

        //private checkaccount chk;

        account acc;//多态

        public Form1()

        {

            InitializeComponent();

            //chk = new checkaccount();

            //chk.balance = 1000;

            //this.label1.Text = chk.balance.ToString();

 

           //多态

            acc = new checkaccount();

            acc.balance = 1000;

            this.label1.Text = acc.balance.ToString();

        }

          public abstract class account

        {

            public int balance;

              public abstract bool withdraw(int amt);

            //{

            //    balance -= amt;

            //    return true;

            //}

        }

        public class checkaccount : account

        {

            public string info;

            public override bool withdraw(int amt)

            {

                if (amt <= base.balance)

                {

                    balance -= amt;

                    return true;

                }

                else

                    return false;

            }

 

        }

                

        private void button1_Click(object sender, EventArgs e)

        {

            //chk.withdraw(Int32.Parse(this.textBox1.Text));

            //this.label1.Text = chk.balance.ToString();

            acc.withdraw(Int32.Parse(this.textBox1.Text));

            this.label1.Text = acc.balance.ToString();

        }

    }

}

 

何时使用继承?

1、代码重用,减少编写的代码量

2、设计重用

公用的字段和方法可以放到父类中,然后由其派生新的子类,之类有自己的字段和方法。

3、经验而言----is a”的关系:

如果A类是从B类中继承而来,即AB的子类,那么我匀可以说:“clash A is a class B