什么是虚拟类的真实例子,为什么我们使用它? [英] What are the real life examples of virtual class and why do we use it?

查看:129
本文介绍了什么是虚拟类的真实例子,为什么我们使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经历了抽象,虚拟和界面。但我仍然无法理解为什么这些在编程中使用。请用一些实际的例子向我解释这个概念。

I have gone through the abstract, virtual and interfaces. But I still can't understand why does these use in the programming. Please, explain me this concept with some real life examples.

推荐答案

一个类可以实现许多接口,而它只能从一个类继承;因此,接口的约束较少。有一本开创性的编程书籍设计模式:可重复使用的面向对象软件的元素,它促进了组合而不是继承。这本书已经很老了,但它仍然是一本很好的读物,可能会给你更多的思考,而不是适合这个帖子。



界面是合约;除了它应该履行合同之外,它与实现无关。



抽象类允许你在类本身内使用抽象方法或属性,只是不要在构造函数中执行。



一般来说,我避免抽象类或保持它们内部并且只暴露接口。



虚拟版本容易出错并且在语义上不正确。



摘要说此方法未在此处实现。您必须将其实现为让这个班级工作



虚拟说我有一个默认的实施,但如果你需要你可以改变我



如果您的最终目标是可测试性,那么接口通常是最佳选择。 (这个类做x而不是这个类是x)。您可能需要将类拆分为更小的组件,但这样才能很好地工作。
A class can implement many interfaces whereas it can only inherit from one class; as such, interfaces are less constraining. There's a seminal programming book, 'Design Patterns: Elements of Reusable Object-Oriented Software' which promotes composition over inheritance. The book is pretty old but it's still a good read and will likely give you much more food for thought than will fit in this thread.

An interface is a contract; it has no bearing on the implementation other than it should fulfil the contract.

Abstract classes allow you to use the abstract method or property within the class itself, just don't do it in the constructor.

Generally, I avoid abstract classes or keep them 'internal' and only expose interfaces.

The virtual version is both bug prone and semantically incorrect.

Abstract is saying "this method isn't implemented here. you must implement it to make this class work"

Virtual is saying "I have a default implementation but you can change me if you need"

If your ultimate objective is testability then interfaces are normally the best option. (this class does x rather than this class is a x). You might need to break your classes into smaller components though for this to work nicely.


在c#中没有虚拟类这样的东西。



接口是一个契约,任何实现接口的类都必须实现该接口中的所有方法和属性。它非常适用于创建服务的多个实例,特别是用于测试。



抽象类可用作基类,可以实现和扩展常用功能。



抽象方法是必须由扩展类的类实现的方法。



虚方法是可以由扩展程序类覆盖的基本实现,或保留原样。



There is no such thing as a virtual class in c#.

Interfaces are a contract, any class that implements an interface must implement all the methods and properties within that interface. Its very useful for creating multiple instances of a service, specially for testing.

Abstract classes are useful as base classes, where common features can be implemented, and extended from.

Abstract methods are methods that must be implemented by classes that extend the class.

Virtual methods are base implementations that can be overridden by the extender class, or left as they are.

using System;

namespace SayHello
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            EnglishPerson e = new EnglishPerson();
            GermanPerson g = new GermanPerson();

            Console.WriteLine(e.SayHello + " " + e.SayName());
            Console.WriteLine(g.SayHello + " " + g.SayName());

            Console.ReadKey();
        }
    }

    public interface IPerson
    {
        int Age { get; }

        string SayHello { get; }

        string SayName();
    }

    public abstract class Person : IPerson
    {
        private DateTime dob = DateTime.Now.AddYears(-50);

        //Implemented in base class
        public int Age
        {
            get
            {
                return DateTime.Today.Year - dob.Year;
            }
        }

        public virtual string SayHello
        {
            get
            {
                return "Hello";
            }
        }

        public abstract string SayName();
    }

    public class GermanPerson : Person
    {
        public override string SayHello
        {
            get
            {
                return "Guten Tag";
            }
        }

        public override string SayName()
        {
            return ("Ich heiße Johan");
        }
    }

    public class EnglishPerson : Person
    {
        public override string SayName()
        {
            return "My name is Jon";
        }
    }
}


抽象类的链接: -



http://msdn.microsoft.com/en- in / library / k535acbf(v = vs.71).aspx [ ^ ]



虚拟: -



http://msdn.microsoft.com/en-us/library/9fkccyh4.aspx [ ^ ]

: -



http://msdn.microsoft.com/en-us/library/87d83y5b.aspx [ ^ ]
Link for abstract classes:-

http://msdn.microsoft.com/en-in/library/k535acbf(v=vs.71).aspx[^]

for virtual:-

http://msdn.microsoft.com/en-us/library/9fkccyh4.aspx[^]

for interface:-

http://msdn.microsoft.com/en-us/library/87d83y5b.aspx[^]


这篇关于什么是虚拟类的真实例子,为什么我们使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆