相关疑难解决方法(0)

85
推荐指数
6
解决办法
2万
查看次数

一个指针,c ++中的两个不同的类

假设我有两个结构ab,分别持有其几个变量(大多数变量是C++的核心类型,但不是全部).

有没有办法创建c一个可以指向其中任何一个的命名指针?或者,有没有办法创建一个可以容纳其中任何一个的集合?

谢谢

c++ pointers class

7
推荐指数
2
解决办法
7240
查看次数

使用标记类来控制逻辑流程

我一直在看一些代码,看到我的同事正在使用"标记类"来控制程序逻辑(参见下面的设计示例).它似乎工作得很好,代码读起来非常好,但有一些东西闻起来......

namespace ConsoleApplication4983
{
    public class MyClass
    {
        static void Main()
        {
            var c = new MyClass();
            c.DoSomething(new Sequential());
            c.DoSomething(new Random());
        }

        public void DoSomething(ProcessingMethod method)
        {
            if (method is Sequential)
            {
                // do something sequential
            }
            else if (method is Random)
            {
                // do something random
            }
        }
    }

    public class ProcessingMethod {}
    public class Sequential : ProcessingMethod {}
    public class Random : ProcessingMethod {}
}
Run Code Online (Sandbox Code Playgroud)

什么是达到同样效果的更好方法?枚举?属性?

.net c#

6
推荐指数
2
解决办法
1171
查看次数

空接口有什么作用?

我正在使用的代码具有一个空接口:

public interface ICube {}
Run Code Online (Sandbox Code Playgroud)

它没有方法或属性。

一些类实现,ICube而其他接口则继承自ICube
请有人告诉我,ICube接口可能有什么好处?

c# oop interface

3
推荐指数
2
解决办法
150
查看次数

避免强制转换和instanceOf

我有一个界面

public interface Details {
   // nothing needed until now
}
Run Code Online (Sandbox Code Playgroud)

它在如下类中使用:

public class Value {
    // many fields
    private Details details;

    public Value(SomeType type) {
        switch (type) {
        case TYPE_1:
        case TYPE_2:
            this.details = new DetailsA();
            break;
        case TYPE_3:
            this.details = new DetailsB();
            break;
        default:
            throw new NotImplementedException("not yet implemented");
        }
    }

    public Details getDetails() {
        return this.details;
    }
}
Run Code Online (Sandbox Code Playgroud)

该接口有两个实现

public class DetailsA implements Details {

    private BigDecimal betragA;

    public DetailsA() {
    }

    public BigDecimal getBetragA() {
      return …
Run Code Online (Sandbox Code Playgroud)

java design-patterns casting instanceof

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

标签 统计

c# ×3

interface ×2

.net ×1

c++ ×1

casting ×1

class ×1

class-design ×1

design-patterns ×1

instanceof ×1

java ×1

oop ×1

pointers ×1