小编sia*_*mak的帖子

使用"方法隐藏"有什么意义?

可能重复:
使用有效示例隐藏在c#中的方法.它是否在框架中实现.?什么是真实世界的优势.

我想知道方法隐藏在c#中有用吗?如果你深入研究这个概念,你会发现隐藏的方法令人困惑,并驱使我们采用意大利面条代码风格.有一些原因:

1-如果在Derived类中需要一个新方法,则可以在派生类中使用新名称定义新方法.

2 - 在使用upcasting时以多态方式使用,将无法实现预期的行为,这可能会令人困惑.

(特别是如果Base类和Derived Class位于不同的库中并且您希望在代码中的某个位置使用它们的多态用途,这种奇怪的行为可能会非常明显和明显)

class Base
{  
    public void Method() { Console.WriteLine("I am Base Class"); }
}
class Derived: Base
{
    public new void Method() { Console.WriteLine("I am Derived Class");}
}
class Program
{
    static void Main(string[] args)
    {
        Base C = new Derived();
        C.Method(); //output:"I am Base Class"
    }
}
Run Code Online (Sandbox Code Playgroud)

这样的行为可能是混乱的,但如果我们使用" 虚拟的"," 基本 "和" 覆盖在"" 派生 "(我的意思是多态的使用)的输出是:"我在派生类".这个输出是我认为我们大多数人所期望的.

换句话说,我相信在层次结构中,大多数开发人员期望在派生类中使用重写方法而不是新方法,而是希望隐藏基类实现.

我不明白为什么这样一个没有优势的坏东西有c#语言?至少我想知道隐藏在c#中的方法的最佳用法是什么?

谢谢您的帮助

c# methods method-hiding

9
推荐指数
1
解决办法
4285
查看次数

共用墙和旁边的房间之间有什么样的关系?

我想知道共同墙(位于相邻房间)和房间之间的关系.
我知道房间和墙壁之间的关系是Composition not Aggregation(我是对的吗?)

And according to the definition of 组合 the contained object can't be shared between two containers, whereas in 聚合 it is possible.

现在我很困惑,最好的建模方法是什么,以表示共同墙和旁边的房间之间的关系?

如果您能为您的建议提供一些代码,那将是非常赞赏的.

| -------- | -------- |

Approch1:

(wall class ---- room class) /Composition
Run Code Online (Sandbox Code Playgroud)

Approach2:

wall class ----- room class /Aggregation
Run Code Online (Sandbox Code Playgroud)

Approch3:

我们有一个墙类和一个公共墙类,公共墙类继承自墙类

adjoining room class ---- (1) Common wall class /Aggregation
adjoining room class ---- (6) wall class / composition
Run Code Online (Sandbox Code Playgroud)

方法4: 我是开发人员而非设计师:)所以这是我的想法:

class Room 
{
  private wall _firstwall  ;
  private …
Run Code Online (Sandbox Code Playgroud)

c# composition relationship aggregation

6
推荐指数
1
解决办法
817
查看次数

为什么Array类明确地实现Ilist接口而不是隐式?

我的目标语言是带有.net框架的C#.我想知道这个主题背后的意义或原因是什么?

任何建议和建议都会得到高度赞赏.

编辑

为什么我问这个问题?因为现在,Array类的一些有用的成员就像索引一样在演员背后!我想知道如果微软分裂ilist接口会更好吗?

.net arrays interface explicit-interface explicit-implementation

4
推荐指数
1
解决办法
1987
查看次数

c#语言中"类型"一词的最佳定义是什么?

这可能是一个非常简单的问题,但我对类型定义感到困惑.

当你想提供一个术语类型的定义,如Int, String或...时,我会想到
这个词 Template,但它的"Class"定义如此接近,现在我想知道什么是术语类型的定义.

Is it the same as class definition!?Type == class!?

如果是这样,我们知道还有其他的(参考)在C#中不属于的类型 Class ,如InterfaceDelegate.

是否可以互换使用Type和Class?
你能否给我一个关于Type的全面定义.
提前致谢.

c# types programming-languages definition

2
推荐指数
1
解决办法
483
查看次数

当我将它转换为字符串时,为什么null合并运算符不能在我的可空int上运行?

起初我试着写一个If-Then-Else statement使用a ternary operator.
它工作正常然后出于好奇我决定使用a编写相同的代码,
null-coalescing operator但它不能按预期工作.

public System.Web.Mvc.ActionResult MyAction(int? Id)
{
    string MyContetnt = string.Empty;

    //This line of code works perfectly
    //MyContent = Id.HasValue ? Id.Value.ToString() : "Id has no value";

    //This line of code dosent show "Id has no value" at all 
    MyContetnt = (System.Convert.ToString(Id) ?? "Id has no value").ToString();

    return Content(MyContetnt);
}
Run Code Online (Sandbox Code Playgroud)

如果我通过路线Mysite/Home/MyAction/8777运行程序,一切都很完美,输入的Id数字将会显示.

但是,如果我没有任何Id通过MySite/Home/MyAction路线运行程序,那么 什么都不会发生并且MyContetnt将是空的,而我希望在屏幕上看到" Id没有价值 ".

我错过了什么吗?

编辑:我很好奇是否有可能通过使用??编写代码?(null合并运算符)?

c# asp.net-mvc nullable null-coalescing

2
推荐指数
1
解决办法
789
查看次数

如何使用Type Constraint创建实现两个接口的Generic Collection?

对不起我在c#中没有处理泛型

根据这个问题,如何制作一个实现两个接口的generc集合我正在寻找这样的直接方式:当然它会产生错误并且完全是错误的.

interface IEmployee {void DisplayInfo();}

interface ISalary {void CalculateSalary();}


class Nurse : IEmployee, ISalary
{
 //some Implementation
}


class Doctor : IEmployee, ISalary
{
 //some Implementation
}

class EntryPoint
{
 static void Main(string[] args)
  { 
  System.Collections.Generic .List<T>  employees where T: ISalary,IEmployee
   =new System.Collections.Generic .List<T>();
  }

 Nurse oNurse = new Nurse();
 Doctor oDoctor = new Doctor();

 employees.Add(oNurse);
 employees.Add(oDoctor);
}
Run Code Online (Sandbox Code Playgroud)

经过一些阅读后,我发现可能我必须首先定义这样的泛型类:

public class HospitalEmployee<T> where T : IEmployee, ISalary
Run Code Online (Sandbox Code Playgroud)

{

}

不幸的是,它很有用,现在我很困惑,不知道该怎么做,请帮助,谢谢你

c# generics syntax

1
推荐指数
1
解决办法
1381
查看次数