相关疑难解决方法(0)

如何在C#中评论/记录覆盖?

有时我会覆盖基类中的方法.有时我甚至用空方法覆盖它们,因为我想要的是防止这种行为.

在过去,我会写这样的东西来表明绕过基本方法意图:

protected override void OnMouseUp(MouseEventArgs e)
{
    // base.OnMouseUp(e);
}
Run Code Online (Sandbox Code Playgroud)

(我知道注释的代码行是一件坏事.我曾经这么做过)

但我想做得更好:

  • 如何记录覆盖的意图?特别:
  • 我在override的XML(<summary>?)文档中写了什么

c# inheritance comments overriding xml-documentation

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

在实例化类时声明函数

我遇到了以下java代码,我不确定它是什么意思.我们可以在实例化一个类之后在'{'中编写代码,例如新的TestClass {*/code goes here*/}

但是当我尝试运行代码时,我没有在输出中看到"Z是10".有人可以给我一些链接,我可以得到一些与java的这个功能相关的更多信息.

class TestClass {
    int z;
    public TestClass(){
    z=10;
    }
    public int getZ(){
    return z;
    }
    public void setZ(int z){
    this.z=z;
    }
}

class A
{   
    public static void main (String[] args) throws java.lang.Exception
    {
    TestClass TC = new TestClass() {
            public void testprint() {
        System.out.println("Z is " + getZ());
        }
     };
    }
}
Run Code Online (Sandbox Code Playgroud)

java anonymous-inner-class

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

运算符继承

我有以下代码:

class Rectangle
{
protected:
    int a, b;
public:
    Rectangle(int a, int b) : a(a), b(b) {}
    int area() { return a*b; }
    Rectangle operator+(const Rectangle & other)
    {
        Rectangle temp(0, 0);
        temp.a = a + other.a;
        temp.b = b + other.b;
        return temp;
    }
    void operator=(const Rectangle & other)
    {
        a = other.a;
        b = other.b;
    }
};

class Square : public Rectangle
{
public:
    Square(int a) : Rectangle(a, a) {}
};

int main()
{
    Square s1(3);
    Square s2(1); …
Run Code Online (Sandbox Code Playgroud)

c++

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

如果当前类中有一个同名的函数,我如何调用父方法(如果可能)

我正在阅读方法重写,在其他语言中,似乎要完全重写,该方法必须具有相同的签名(参数、返回类型...等)

所以我试图检查 python 是否是这样工作的,我尝试了下一个代码

class Person():
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def print_name(self, last_name):
        print(self.name + " " + last_name)

class Superhero(Person):
    def __init__(self, name, age, power):
        super().__init__(name, age)
        self.power = power

    def print_name(self):
        print(self.name)


human = Person("Ron", 23)
super_human = Superhero("Superman", 30, "Flying")

human.print_name("Wesley")
super_human.print_name("Kent")
Run Code Online (Sandbox Code Playgroud)

我收到一个错误,super_human.print_name("Kent")它需要一个参数,但我传递了两个参数,我知道 MRO 存在于 python 中,我在其中查看(对象>类>父类),所以我想知道是否有一个这样我就可以调用print_name()父类中存在的函数而不是当前函数,因为它们采用不同的参数。

python python-3.x python-class

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

IList in interface,List in implementation

在我的接口中,通常有一个IList<ISomeType>代表List类型的成员,并且说我期望一个支持Add方法的实现.

但是在接口实现中,有一个

IList<ISomeType> = new List<ISomeType>()
Run Code Online (Sandbox Code Playgroud)

例如,每当我使用List时,我都必须进行转换

(this.MyList as List<IMyType>).AddRange(someType.ToList());
Run Code Online (Sandbox Code Playgroud)

有一个更好的方法吗?如何避免这种演员?

- 编辑以询问更多信息 -

而不是扩展方法,是否有一个小的linq表达式来解决这个问题?

IList<string> list = new List<string>();
var items = new[] { "1", "2", "3" };
items.ToList().ForEach(x => list.Add(x));
Run Code Online (Sandbox Code Playgroud)

但这看起来并不是很直接,因为它颠覆了正在做的事情.(操作在要添加的项目上,而不在列表中).

有什么比这更好的?可以在列表上做些什么?

c# abstraction interface list

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

依赖注入类型选择

最近,我遇到了一个必须基于参数选择类型的问题。例如:用于发送通知的类,该类应根据输入参数选择正确的通道(电子邮件,短信,...)。

我看起来像这样:

public class NotificationManager 
{
    IEmail _email;
    ISms _sms;

    public NotificationManager (IEmail email, ISMS sms) 
    {
        _email = email;
        _sms = sms;
    }

    public void Send(string type) 
    {
        switch(type) 
        {
            case "email":
                _email.send;
                break;

            case "sms":
                _sms.send;
                break;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这里的问题是,当我使用这种构造时,构造函数在发送通知的所有不同方法中迅速增长。

我真的不喜欢这样,这使得对选择单元进行单元测试变得难以操作。

我不能简单地说,new email();因为通知类型的电子邮件将依赖IEmailManager,这只会解决问题。

是否有某种模式可以执行相同的操作,但是方式更好,更清洁?

c# architecture dependency-injection

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

为什么我们应该使用Interface而不是具体类型?

在Java中使用集合时,建议使用Interface而不是具体类型.

喜欢: List<Object> list = new ArrayList<Object>();

但是,使用ArrayList<Object> list = new ArrayList<Object>();也会做同样的工作,对吧?

java collections arraylist

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

c#是否可以更改父类的字段类型?

如果我有2个类,一个用于数据,例如:

public class Cords
{
    public double x;
    public double y;
}
Run Code Online (Sandbox Code Playgroud)

一,使用这些数据:

public class Geometry
{
    public Cords()
    {
        points = new List<Cords>();
    }
    public void SomeMathWithPoints()
    {
         MagicWithPoints(points);
    }

    protected List<Cords> points;
}
Run Code Online (Sandbox Code Playgroud)

我想用一些特定的函数,使用继承来扩展这个类,但这次我需要一些Cords类的附加数据.所以我试着这样做:

public class ExtendedCords: Cords
{
    public double x;
    public double y;
    public string name;
}

public class ExtendedGeometry : Geometry
{
     protected SomeNewMagicWithPoints(){...}
     protected List<ExtendedCords> points;
}
Run Code Online (Sandbox Code Playgroud)

但我注意到,如果我愿意:

    ExtendedGeometry myObject = new ExtendedGeometry();
    myObject.SomeMathWithPoints();
Run Code Online (Sandbox Code Playgroud)

此函数将使用旧(parrents)字段points.那么如何让它使用一个类型的新ExtendedCords?我的意思是,我希望能够在新领域使用child和parrent的功能.

c# oop inheritance field

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

覆盖受保护的变量 C++

假设我有以下代码:

class Board
{
  protected:
    std::vector<Piece*> pieces;
}

class ChessBoard : public Board
{
  protected:
    std::vector<ChessPiece*> pieces;

}
Run Code Online (Sandbox Code Playgroud)

我希望派生类pieces用这个 new覆盖受保护的变量vector,但是我遇到了困难,而且这似乎是不好的做法。

c++ oop

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

如何使用继承的operator []来防止赋值?

我有一个自定义结构SortedArrayList<T>,根据比较器对其元素进行排序,我想阻止使用operator[].

例:

ArrayList.h

template <typename T> class ArrayList : public List<T> {
    virtual T& operator[](const int& index) override; //override List<T>
    virtual const T operator[](const int& index) const override; //override List<T>
}
Run Code Online (Sandbox Code Playgroud)

SortedLinkedList.h包含以下运算符

template <typename T> class SortedArrayList : public ArrayList<T> {
   public:

   SortedArrayList<T>(const std::function<bool(const T&, const T&)>& comparator);

   T& operator[](const int& index) override; //get reference (LHS)
   const T operator[](const int& index) const override; //get copy (RHS)
}
Run Code Online (Sandbox Code Playgroud)

Test.h

ArrayList<int>* regular = new ArrayList<int>();
ArrayList<int>* sorted = …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance containers list

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