黑马程序员-------面向对象基础(一)

 ---------------------- <a href="http://edu.csdn.net"target="blank">ASP.Net+Android+IOS开发</a>、<a href="http://edu.csdn.net"target="blank">.Net培训</a>、期待与您交流! ----------------------
C# 中,面向过程与面向对象是相辅相成的,面向对象弥补了很多面向过程开发的缺点,因此,也要重视学习面向对象。

在面向对象这个概念中,最重要的三个概念就是继承、封装、多态。

继承的作用就是减少开发周期,减少开发难度,使得维护应用程序的成本减少,难度降低。

访问修饰符:public , private ,protected,internal

(一)继承中用protected访问修饰符,可以保护基类或父类中的成员收到保护,有个它的限制,只有它本身和它的派生类才可以访问。

(二)如果限制一个类被继承,可以用sealed声明一个类。

(三)overload 与override的差异;

在学习构造方法时有一个方法重载概念,在学习面向对象是,有一个重写方法概念。

overload:是方法的重载,是一个构造方法,通过参数数量不同,类型不同,顺序不同等来进行选择来构造函数。

override:是面向对象的概念,是当一个父类派生中一个子类是,为了扩充和改变子类,实现多态性而需要对方法进行重写。

(四)继承方法又分为直接继承和改写。

下面是我学习面向对象时,随手写的一个表现中继承方式的简单小程序。

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

namespace 直接继承
{
    class yunsuan
    {
      
        public void jjys()
        {
            int choose;
            int n1;
            int n2;
            int c = 0;
            Console.WriteLine("请选择:1加法运算 2减法运算");
            choose = int.Parse(Console.ReadLine());
            if (choose != 1 && choose != 2) return;
            Console.WriteLine("请输入第一个数:");
            n1 = int.Parse(Console.ReadLine());
            Console.WriteLine("请输入第二个数:");
            n2 = int.Parse(Console.ReadLine());

            switch (choose)
            {
                case 1: c = add(n1, n2); break;
                case 2: c = sub(n1, n2); break;
            }
            Console.WriteLine(c);
            Console.ReadKey();
        }

        int add(int a, int b)
        {
            int c = a + b;
            return c;
        }
        int sub(int a, int b)
        {
            int c = a - b;
            return c;
        }
    }
}

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

namespace 直接继承
{
    class Program :yunsuan
    {
        int c = 0;
        static void Main(string[] args)
        {
            do
            {
                int method;

                Program p = new Program();

                Console.WriteLine("请选择加减乘除运算:1加减运算 2乘除运算");
                method = int.Parse(Console.ReadLine());
                if (method != 1 && method != 2) return;
                switch (method)
                {
                    case 1: p.jjys(); break;
                    case 2: p.ccys(); break;
                }
            }
            while (true);
        }
       void ccys()
        {
            Console.WriteLine("请选择:1乘法运算 2除法运算");
            int choose = int.Parse(Console.ReadLine());
            if (choose != 1 && choose != 2) return;
            Console.WriteLine("请输入第一个数:");
            int n1 = int.Parse(Console .ReadLine ());
            Console.WriteLine("请输入第二个数:");
            int n2 = int.Parse(Console .ReadLine ());
            switch (choose)
            {
                case 1:c= multip(n1, n2); break;
                case 2:c= devide(n1, n2); break;

            }
            Console.WriteLine(c);
            Console.ReadKey();         
        }
       int multip(int a, int b)
       {
            c = a * b;
           return c;
       }
       int devide(int a, int b)
       {
            c = a / b;
           return c;
       }
           
    }   
}
在父类中提供加减运算,在子类中除了具有继承父类的加减运算功能,还能实现自己特有的乘除运算,虽然简单,可是还是体现出了继承的好处。

多态性是一个面向对象的核心概念。感觉有了动态的改变,下边是我模拟打印机的打印方式,针对多态性写的一个例子程序:

 

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

namespace lianxi
{
    class Program
    {
        static void Main(string[] args)
        {
            do
            {
                Program p = new Program();
                Console.WriteLine("请选择:1从头打印 2从第一章开始打印 3从第二章开始打印");
                int a = int.Parse(Console.ReadLine());
                if (a != 1 && a != 2 && a != 3 && a != 4) return;
                switch (a)
                {
                    case 1: zhu u = new zhu(); u.start(); break;
                    case 2: A a1 = new A(); a1.start(); break;
                    case 3: B b = new B(); b.start(); break;

                }
            }
            while (true);

        }
      
    }

    class zhu
    {
        public void ready()
        {
            Console.WriteLine("开始启动");  

        }
        public virtual void start()
        {
            ready();
            Console.WriteLine("从头开始");
            finish("从头打印");
        }
        protected void finish(string str)
        {
            Console.WriteLine("{0}完成了",str);
        }
    }
    class A :zhu
    {
     
     public override  void start()
        {
            ready();
            Console.WriteLine("开始打印第二张");
            finish("第二章");
        }

    }
    class B:zhu
    {
      
     public  override void start()
        {
            ready();
            Console.WriteLine("开始打印第三章");
            finish("第三章");
        }
    }
}


 ---------------------- <a href="http://edu.csdn.net"target="blank">ASP.Net+Android+IOS开发</a>、<a href="http://edu.csdn.net"target="blank">.Net培训</a>、期待与您交流! ----------------------

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值