C# - 我应该使用什么,接口,抽象类或两者? [英] C# - What should I use, an Interface, Abstract class, or Both?

查看:154
本文介绍了C# - 我应该使用什么,接口,抽象类或两者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,假设我在C#中构建某种房地产应用程序。对于每种类型的属性,我将创建一个类,如ResidentialProperty和CommercialProperty。这两个类以及所有其他属性类将共享一些常见属性,例如Id,Title,Description和Address信息。

So, hypothetically, I'm building some sort of real estate application in C#. For each type of property, I'm going to create a class such as ResidentialProperty and CommercialProperty. These two classes as well as all other property classes will share some common properties, such as Id, Title, Description, and Address information.

我希望能够做什么要做的是:

a)返回一个只包含基本信息的对象集合

b)能够调用一个方法,如GetProperty(id),它将创建和返回一个ResidentialProperty或CommercialProperty,或者调用GetProperties(),它将返回一个或另一个或两者的集合。

What I would like to be able to do is:
a) return a collection of objects that contain just the basic information
b) be able to either call a method such as GetProperty(id) which will create and return either a ResidentialProperty or CommercialProperty, or call GetProperties() which will return a collection of one or the other, or both.

所以说,它可能会使有意义的是创建一个名为BasicProperty(或PropertyBase)的抽象类,它包含所有常见属性,并使ResidentialProperty和CommercialProperty从中扩展。这将解决问题#1,因为我可以创建一个返回BasicProperties集合的方法。

So with that said, it would probably make sense to create an abstract class called BasicProperty (or PropertyBase) which contains all of the common attributes, and have the ResidentialProperty and CommercialProperty extend from it. This would take care of problem #1, as I could create a method that returns a collection of BasicProperties.

但对于#2,能够返回一个属性类型或者另一个,我需要一个接口(IProperty),并从它继承住宅和商业类,然后让GetProperty(id)和GetProperties()返回一个IProperty对象(或者因为它们继承自IProperty,我可以按原样返回它们而不是接口?)?

But for #2, being able to return either one property type or the other, I would need an Interface (IProperty), and have the Residential and Commercial classes inherit from it, and then have the GetProperty(id) and GetProperties() return an IProperty object (or because they inherit from IProperty, can I return them as is and not as the Interface?)?

现在如果我应该使用接口,我该怎么处理BasicProperty类?

- 我是否将其作为摘要并实现接口?或者

- 我是否将它作为摘要保留,所有3个类都实现了接口?或者

- 我不是将它创建为抽象,将所有基本信息放入接口,BasicProperty,ResidentialProperty和CommercialProperty都实现了接口吗?

Now if I should use an Interface, what do I do with the BasicProperty class?
- Do I leave it as an abstract and implement the Interface? Or
- Do I leave it as an abstract and all 3 classes implement the Interface? Or
- Do I not create it as an abstract, put all of the basic information into the Interface, and the BasicProperty, ResidentialProperty and CommercialProperty all implement the Interface?

提前致谢,
Carl J。

Thanks in advance, Carl J.

推荐答案

从我能收集到的,你是在这里谈论两件不同的事情。

From what I can gather, you are talking about two different things here.


  1. 班级结构

  2. 这些班级的数据访问

你认为你应该创建一个抽象类来包含公共属性是正确的,这就是继承的含义:)(除此之外)

You are correct in thinking that you should create an abstract class to contain the common properties, that's what inheritance is for :) (among other things)

但我不明白为什么你不能创建一个方法 GetProperty(id)的数据访问类指定返回类型 PropertyBase

But I dont see why you can't create a data access class that has a method GetProperty(id) that specifies a return type of PropertyBase

public PropertyBase GetProperty(long id)

GetProperty 的实现中,您可以构建 ResidentialProperty CommercialProperty (基于你想要的业务/数据库逻辑)然后返回它,c#允许你这样做。

in the implementation of GetProperty you can construct a ResidentialProperty or CommercialProperty (based on what ever business/database logic you want) then return it, c# allows you to do that.

也许我想念你了?

HTH

:

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }

    class DataAccessLayer
    {
        public PropertyBase GetSomething(int id)
        {
            if (id > 10)
                return new CommercialProperty();
            else
                return new ResidentialProperty();
        }

    }

    class PropertyBase { }
    class ResidentialProperty : PropertyBase { } 
    class CommercialProperty : PropertyBase { }
}

这篇关于C# - 我应该使用什么,接口,抽象类或两者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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