Tag: oop

如何在类中交叉引用对象

我有一个Person类和两个名为Parent和Child的inheritance类。 父母可以有n个孩子,孩子可以有n个父母。 OOD在父和子之间创建引用的最佳方法是什么。 我应该在引用连接的父/子的每个类中创建一个List,还是有更好的方法?

inheritance自具有相同方法签名的多个接口的类

说,我有三个接口: public interface I1 { void XYZ(); } public interface I2 { void XYZ(); } public interface I3 { void XYZ(); } inheritance自这三个接口的类: class ABC: I1,I2, I3 { // method definitions } 问题: 如果我这样实现: ABC类:I1,I2,I3 { public void XYZ() { MessageBox.Show(“WOW”); } } 它编译得很好,也运行得很好! 这是否意味着这个单一方法实现足以inheritance所有三个接口? 如何实现所有三个接口的方法并调用它们 ? 像这样的东西: ABC abc = new ABC(); abc.XYZ(); // […]

访客模式示例

public class Song { public string Genre { get; protected set; } public string Name { get; protected set; } public string Band { get; protected set; } public Song(string name, string band, string genre) { Name = name; Genre = genre; Band = band; } } public interface IMusicVisistor { void Visit(List items); } […]

私有构造函数和公共参数构造函数

我听说私有构造函数阻止了外部世界的对象创建。 当我有一个代码 public class Product { public string Name { get;set;} public double Price {get;set;} Product() { } public Product(string _name,double _price) { } } 在这里我仍然可以声明一个公共构造函数(参数),它不会破坏私有构造函数的目的吗? 我们什么时候需要代码中的私有和公共构造函数(参数)? 我需要详细说明。

类和数据类型有什么区别?

我听到以下声明: 我们可以说class是数据类型或数据类型是一种类。 任何人都可以向我解释这究竟是什么意思?

为什么c#中的main方法总是放在类中而不是c ++中

为什么我们将main()方法总是放在C#中的class中,而在c ++中它始终放在class之外。

哪个OO概念是“Base b = new Derived()”的一个例子?

我正在通过测试并遇到一个问题,我们没有与同事达成协议。 С++ 1 class Base {}; 2 class Derived : public Base {}; 3 class Foo 4 { 5 public: 6 Foo() 7 { -8- Base* b = new Derived(); // Concept name is? 9 } 10 }; C# 1 abstract class Base{} 2 public class Derived : Base{} 3 4 public class Foo 5 […]

如何告诉inheritance类不调用其基类’无参数构造函数?

我很惊讶地发现,无论何时调用派生类中的任何构造函数,都会调用我的基类的无参数构造函数。 我认为这就是: base()用于显式调用基础构造函数,如果我想要的话。 在实例化派生类时,如何防止调用基本构造函数? using System; namespace TestConstru22323 { class Program { static void Main(string[] args) { Customer customer = new Customer(“Jim”, “Smith”); Customer c = new Customer(); Console.WriteLine(customer.Display()); Console.ReadLine(); } } public class Person { public Person() { Console.WriteLine(“I don’t always want this constructor to be called. I want to call it explicitly.”); } […]

Unity – 使用其他参数构造函数注入

我有一个类,其构造函数如下所示: public BatchService(IRepository repository, ILogger logger, string user) 在我的DI bootstrapper类中,我有以下RegisterType命令: .RegisterType( new InjectionConstructor( new ResolvedParameter(“SomeRepository”), new ResolvedParameter(“DatabaseLogger”))) 在我的客户端代码中,我想实例化BatchService,如下所示: BatchService batchService = DIContainer.Resolve() 如您所见,我有一个名为“user”的字符串参数作为BatchService的构造函数的一部分,该参数不是DI逻辑的一部分。 如果我需要在BatchService类中使用“user”,我该如何才能最好地处理这种情况? 预先感谢您提供的任何帮助!

为什么C#将匿名方法和闭包实现为实例方法,而不是静态方法?

因为我不是编程语言方面的专家,所以我很清楚这可能是一个愚蠢的问题,但我最好能告诉C#通过将它们变成匿名嵌套类的实例方法来处理匿名方法和闭包[1] ,实例化这个类,然后将委托指向那些实例方法。 看来这个匿名类只能被实例化一次(或者我错了吗?),那么为什么不让匿名类变为静态呢? [1]实际上,看起来有一个闭包类和一个不捕获任何变量的匿名方法,我不完全理解其中的任何一个。