用实例分别解释封装,继承,多态

.Net技术 码拜 9年前 (2015-05-10) 1446次浏览 0个评论
 

帮忙解释封装,继承,多态的概念,并且用编程小实例来说明,谢谢

10分
类的封装性,继承性,多态性,是面向对象的重要机制。继承的使用可以提高软件模块的可重用性和可扩展性。提高开发效率:
继承:
class A //A为基类
{

}
class B:A //B继承了A
{
}
/*
 * 程序名称:InherConsoleApp.Program.cs
 * 程序功能:类的继承
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace InherConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            double x, y, r1, r2, r3, r4;
            //类的实例化
            Derived d = new Derived();
            Console.WriteLine(“请输入一个数:”);
            x = double.Parse(Console.ReadLine());
            Console.WriteLine(“请输入一个数:”);
            y = double.Parse(Console.ReadLine());
            //调用方法进行计算
            r1 = d.Add(x, y);
            r2 = d.Subtract(x, y);
            r3 = d.Multiply(x, y);
            r4 = d.Divide(x, y);
            //输出计算结果
            Console.WriteLine(“\n计算结果如下:\n”);
            Console.WriteLine(“{0}+{1}={2}”, x, y, r1);
            Console.WriteLine(“{0}-{1}={2}”, x, y, r2);
            Console.WriteLine(“{0}×{1}={2}”, x, y, r3);
            Console.WriteLine(“{0}÷{1}={2}\n”, x, y, r4);
        }
    }
    /// <summary>
    /// 基类,包含了加、减法的计算方法
    /// </summary>
    class Base
    {
        /// <summary>
        /// 加法计算
        /// </summary>
        /// <param name=”n1″>加数</param>
        /// <param name=”n2″>另一加数</param>
        /// <returns>计算结果:和</returns>
        public double Add(double n1, double n2)
        {
            return n1 + n2;
        }
        /// <summary>
        /// 减法计算
        /// </summary>
        /// <param name=”n1″>被减数</param>
        /// <param name=”n2″>减数</param>
        /// <returns>计算结果:差</returns>
        public double Subtract(double n1, double n2)
        {
            return n1 – n2;
        }
    }
    /// <summary>
    /// 继承了基类的子类,包含了乘、除法的计算方法
    /// </summary>
    class Derived : Base
    {
        /// <summary>
        /// 乘法计算
        /// </summary>
        /// <param name=”n1″>因数</param>
        /// <param name=”n2″>另一因数</param>
        /// <returns>计算结果:积</returns>
        public double Multiply(double n1, double n2)
        {
            return n1 * n2;
        }
        /// <summary>
        /// 除法计算
        /// </summary>
        /// <param name=”n1″>被除数</param>
        /// <param name=”n2″>除数</param>
        /// <returns>计算结果:商</returns>
        public double Divide(double n1, double n2)
        {
            return n1 / n2;
        }
    }

}

多态:同一操作作用于不同的类的实例,不同的类将进行不同的解释,最后产生不同的执行结果。
/*
 * 程序名称:PolymorConsoleApp.Program.cs
 * 程序功能:类的多态性
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PolymorConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Vehicle vl = new Vehicle(4, 2);
            Car cr = new Car(5, 2, 6);
            Truck tk = new Truck(7, 5, 3, 9);
            vl.Sp();
            vl = cr;
            vl.Sp();
            cr.Sp();
            vl = tk;
            vl.Sp();
            tk.Sp();

        }
    }
    /// <summary>
    /// 定义汽车类 
    /// </summary>
    class Vehicle   
    {
        //公有成员:轮子个数
        public int wheels;
        //保护成员:重量
        protected float weight;  
        public Vehicle(int w, float g)
        {
            wheels = w;
            weight = g;
        }
        public virtual void Sp()
        {
            Console.WriteLine(“汽车正在鸣笛!”);
        }
    }
    /// <summary>
    /// 定义轿车类,继承了汽车类
    /// </summary>
    class Car : Vehicle   
    {
        //私有成员,乘客数
        int passengers;   
        public Car(int w, float g, int p)
            : base(w, g)
        {
            wheels = w;
            weight = g;
            passengers = p;
        }
        public override void Sp()
        {
            Console.WriteLine(“轿车正在鸣笛!”);
        }
    }
    /// <summary>
    /// 定义卡车类,继承了汽车类
    /// </summary>
    class Truck : Vehicle  
    {
        int passengers;
        //私有成员,载重
        float load;    
        public Truck(int w, float g, int p, float l)
            : base(w, g)
        {
            wheels = w;
            weight = g;
            passengers = p;
            load = l;
        }
        public override void Sp()
        {
            Console.WriteLine(“卡车正在鸣笛!”);
        }
    }

}
可见的和不可见的概念就是封装。你写好了一个类,其中不可见的属性和方法,你就把它前面加上private,可见的就加上public。这样,如果你在另外一个类中实例化这个类的对象,你就会看到,所有private的属性和方法都看不到了,而public的就是可以看到的!

10分
封装是把字段封装为属性,是字段不可见,对外提供属性方法

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

namespace ClassLibrary1
{
    public class Class1
    {
        private int id;

        public int Id
        {
            get { return id; }
            set { id = value; }
        }
    }
}

继承是子类继承父类
不过建议尽可能用接口来编程
父类

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

namespace ClassLibrary1
{
    public class Class1
    {
        protected int id;

        public int Id
        {
            get { return id; }
            set { id = value; }
        }
    }
}

子类

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

namespace ClassLibrary1
{
    public class Class2:Class1
    {
    }
}

在子类继承父类是把父类的字段改成protected ,这样使子类可以访问父类的字段

多态,其实我感觉多太是最难理解的
多态就是一种形式多种表现
多态的实现可以是虚方法,和抽象方法
虚方法用virtual修饰可以写方法体
抽象方法用abstract修饰没有方法体
这两中都可以实现多太
还有一中就是toString()方法也可以实现多态
抽象方法

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

namespace ClassLibrary1
{
    public abstract class Class1
    {
        protected int id;

        public int Id
        {
            get { return id; }
            set { id = value; }
        }


        public override int son();
    }
}

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

namespace ClassLibrary1
{
    public class Class2:Class1
    {
        public override int son()
        {
            return base.son();
        }
    }
}

虚方法

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

namespace ClassLibrary1
{
    public abstract class Class1
    {
        protected int id;

        public int Id
        {
            get { return id; }
            set { id = value; }
        }


        public virtual int son() {
            int i = 0;

        }
    }
}

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

namespace ClassLibrary1
{
    public class Class2:Class1
    {
        public override int son()
        {
            return base.son();
        }
    }
}

up up  up  
1分
学习,帮顶~~~
5分
封装就是 class a(){…………..}
继承这个概念你可以从抽象类与接口的学习中获得认识。

多态同样可以从接口中获得认识,同一个功能定义的不同实现。

xk1126(未来的明天) 你好!你的解释很清楚,你提到的尽量用接口编程,能帮我举个用接口实现的例子呢?我对接口的概念理解不深。非常感谢!
 
3分
能感觉到楼主懒的举手~
3分
引用 8 楼 healer_kx 的回复:

能感觉到楼主懒的举手~

+1

没具体问题,连例子都要别人敲。看书后自己敲才能学会记得住啊。

up.
引用 9 楼 wuyazhe 的回复:

引用 8 楼 healer_kx 的回复:
能感觉到楼主懒的举手~

+1

没具体问题,连例子都要别人敲。看书后自己敲才能学会记得住啊。

不好意思,让你们见笑了:(   我是新手,所以以前有点想偷懒,被你们看穿了,我自己先找做一下把不懂的拿出来再向大侠们请教了

3分
封装  就理解成  一个盒子  你只能看外面的(就像你看到的类)  里面的东西看不到(类中的属性)
5分
接口是规范类的行为
接口里可以是方法,但方法没有方法体
字段,属性,索引器
接口

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

namespace WindowsApplication1
{
    public interface Interface1
    {
        public void sayHi();
        public int execute();
    }
}

类实现接口的方法

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

namespace WindowsApplication1
{
    class Class1:Interface1
    {
        #region Interface1 成员

        public void sayHi()
        {
            
        }

        public int execute()
        {
            return 0;
        }

        #endregion
    }
}

其实多用接口来编程是更好的维护继承和多态的

学习了,多谢

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明用实例分别解释封装,继承,多态
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!