(大三上_游戏开发设计模式_上课_1)多态练习_计算机

灵活性,可扩展,灵活性,可维护

封装

继承:要有基类

代码片段

写整个属性

        基类里面  基态
            基方向,虚方法

//虚方法,可以写在实例方法里
 

public virtual double GetResult() 
{
    return 0;
}

实例方法,可以创建实例

抽象方法,要写在抽象类里
 

 public abstract double GetResult();

抽象类,不能创建实例

abstract class Prgram

namespace 多态练习_计算机
{
    /// <summary>
    /// 运算类,抽象类
    /// </summary>
    internal abstract class Prgram
    {
      static void Main(string[] args)
        {
            Console.Write("请输入数字A:");
            string strNumberA = Console.ReadLine();

            Console.Write("请选择运算符号(+、-、*、/):");
            string strOperate = Console.ReadLine();

             Console.Write("请输入数字B:");
           string  strNumberB = Console.ReadLine();

            string strResult = "";
            Opration opration=null;//基类的引用
         
           switch (strOperate)
            {
                case "+":
                    //基类的引用指向子类的对象-多态
                    opration = new AddOperation();
                  
                break;
                case "-":
                    //基类的引用指向子类的对象-多态
                   opration = new SubOperation();
                  
                    break;
            }
            //多态结果
            strResult = opration.GetResult().ToString();
        }
    }
}

基类

namespace 多态练习_计算机
{
    /// <summary>
    /// 运算类,抽象类
    /// </summary>
    internal abstract class Opration
    {
        private double _strNumberA;
        private double _strNumberB;


        public double StrNumberA
        {
            get { return _strNumberA; }
            set
            {
                if (value != 0)
                    _strNumberA = value;
                else
                    Console.WriteLine("因子不能为0");
            }
        }
        public double StrNumberB
        {
            get { return _strNumberB; }
            set
            {
                if (value != 0)
                    _strNumberB = value;
            }
        }
        //获取运算结果的方法
        //抽象方法,要写在抽象类里
        public abstract double GetResult();

    }
}

add

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 多态练习_计算机
{
    internal class AddOperation : Opration
    {
        public override double GetResult()
        {
            return StrNumberA + StrNumberB;
        }
    }
}

mul

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 多态练习_计算机
{
    internal class MulOperation:Opration
    {
        public override double GetResult()
        {
            return StrNumberA * StrNumberB;
        }
    }
}

sub

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 多态练习_计算机
{
    internal class SubOperation:Opration
    {
        public override double GetResult()
        {
            return StrNumberA -StrNumberB;
        }
    }
}

div

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 多态练习_计算机
{
    internal class DivOperation: Opration
    {
        public override double GetResult()
        {
            return StrNumberA / StrNumberB;
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值