标签: overloading

可以像Python 2.6中的oct()和hex()一样重载bin()吗?

在Python 2.6(及更早版本)中hex(),oct()内置函数可以通过定义__hex____oct__特殊函数在类中重载.但是,没有一个__bin__特殊的函数来重载Python 2.6的新bin()内置函数的行为.

我想知道是否有任何方式可以灵活地重载bin(),如果不是,我想知道为什么不一致的界面?

我知道__index__可以使用特殊功能,但这不灵活,因为它只能返回一个整数.我的特定用例来自bitstring模块,其中前导零位被认为是重要的:

>>> a = BitString(length=12)       # Twelve zero bits
>>> hex(a)
'0x000'
>>> oct(a)
'0o0000'
>>> bin(a)
'0b0' <------ I want it to output '0b000000000000'
Run Code Online (Sandbox Code Playgroud)

我怀疑没有办法实现这一目标,但我认为这不会有问题!

python binary overloading python-2.6

14
推荐指数
2
解决办法
2497
查看次数

PHP __call vs method_exists

我正在处理的项目包含类似call_user_func(_array)的包装器,它在执行之前执行一些检查.其中一个检查是method_exists(In Case,提供的第一个参数是类的实例,第二个参数是方法名称)另一个是is_callable.如果其中一个检查失败,该函数将抛出异常.

我的代码包含一个带有函数名称的数组(setFoo,setBar等)和用于重载(__call)的php magic函数,它处理某些变量的设置,替换和删除(更好的某些数组元素).

问题:如果未定义函数,method_exists将返回false.

如果__call函数正确处理请求,我是否有机会得到真的?

php overloading

14
推荐指数
3
解决办法
9269
查看次数

有没有办法在.NET中重载属性

我已经做了很多方法重载,但现在我有一个实例,我想重载一个属性.Visual Studio中的IDE似乎允许它,因为我实际上可以设置两个重载,但是我得到一个错误,说它无效,因为它们的类型不同.我想我的语法中缺少一些东西?

我希望能够使用两个(或更多)不同的自定义类作为我的属性的类型.

Public Overloads Property myFlexibleProperty() As myCustomClass1
      Get
         Return _myFlexibleProperty1
      End Get
      Set(ByVal value As myCustomClass1)
         _myFlexibleProperty1 = value
      End Set
   End Property

   Public Overloads Property myFlexibleProperty() As myCustomClass2
      Get
         Return _myFlexibleProperty2
      End Get
      Set(ByVal value As myCustomClass2)
         _myFlexibleProperty2 = value
      End Set
   End Property
Run Code Online (Sandbox Code Playgroud)

到目前为止,我发现的所有帮助都与重载方法有关.尽管IDE让我这么做,我开始认为这是不可能的?

vb.net overloading properties

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

如果我在Scala中定义多个重载的构造函数,我不能定义默认值吗?

我已经定义了多个构造函数,在所有构造函数中都有一些默认参数值.看起来正确(我看不出任何歧义),但Scala(2.8)编译器抱怨:

多个重载的构造函数替代定义默认参数

这是否意味着我根本无法为重载的构造函数定义默认值?

让我来说明一下情况(当然是原始的,但是说明性的):


class A(subject : Double, factor : Int = 1, doItRight : Boolean = true) {

  def this (subject : Int, factor : Int = 1, doItRight : Boolean = true) = {
    this(subject.toDouble , factor, doItRight)
  }

  def this (subject : String, factor : Int = 1, doItRight : Boolean = true) = {
    this(subject.toDouble , factor, doItRight)
  }

  def this () = {
    this(defaultSubject)
  }

}



constructor default scala overloading

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

在doxygen中对重载进行分组

在我的库中,我有很多函数重载的形式:

/// \brief Does thing.
///
/// \details The thing that is done is very special.
template<typename T>
void do_stuff(const T& t);

/// \brief Does thing repeatedly. 
/// \copydetails do_stuff()
template<typename T>
void do_stuff(const T& t, std::size_t x);
Run Code Online (Sandbox Code Playgroud)

这通常是有效的,并且非常好,但是多次创建相同的文档部分.我想要的是将这些功能组合在一起.有详细说明和每个重载注释的简要说明.我也不反对可以做这样的事情或输入过滤器的别名.

我可以想象的一种方式是:

文档结果应如下所示:

template<typename T>
void do_stuff(const T& t);                (1)

template<typename T>
void do_stuff(const T& t, std::size_t x); (2)

The things that is done is very special.

(1) Does thing.

(2) Does thing repeatedly.
Run Code Online (Sandbox Code Playgroud)

当然我可以创建一个新页面并手动编写这种文档,但是它需要我在页面上重复函数声明,然后将链接打到实际的函数文档中,但这比其他任何东西都要糟糕.

有没有办法轻松实现这一目标?甚至暗示将其破解为doxygen也会受到赞赏.

c++ documentation doxygen overloading

14
推荐指数
2
解决办法
3031
查看次数

Java重载方法选择

我试图了解Java如何选择执行哪个方法:

//Example 1 prints Square:add(Figure)
Figure fs = new Square();
fs.add(fs);

//Example 2 prints Square:add(Figure)
Rectangle rs = new Square();
rs.add(fs);

//Example 3 prints Rectangle:add(Rectangle). Expected Square:add(Square)
rs.add(new Square());

//Example 4 prints Rectangle:add(Rectangle). Expected Square:add(Figure)
Square ss = new Square();
ss.add(rs);

class Figure
{
    public void add(Figure f){ System.out.println("Figure:add(Figure)"); }
}

class Rectangle extends Figure
{
    @Override
    public void add(Figure f){ System.out.println("Rectangle:add(Figure)"); }
    public void add(Rectangle r){ System.out.println("Rectangle:add(Rectangle)"); }
}

class Square extends Rectangle
{
    @Override
    public void add(Figure f){ …
Run Code Online (Sandbox Code Playgroud)

java overloading

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

是否有可能合法地重载字符串文字和const char*?

在C++ 11中是否有可能重载const char*和字符串文字(const char[])?我们的想法是避免在strlen已知此长度时调用以查找字符串长度.

这个片段打破了G ++ 4.8和Clang ++ 3.2:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

template<typename T, int N>
void length(const T(&data)[N]) {
  printf("%u[]\n", N - 1);
}

template<typename T>
void length(const T* data) {
  printf("*%u\n", (unsigned)strlen(data));
}

int main() {
  length("hello");
  const char* p = "hello";
  length(p);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

错误(Clang):

test2.cpp:16:3: error: call to 'length' is ambiguous
  length("hello");
  ^~~~~~
test2.cpp:6:6: note: candidate function [with T = char, N = 6]
void length(const …
Run Code Online (Sandbox Code Playgroud)

c++ templates overloading c++11

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

警告:[重载]方法m1可能与方法m2不明确

import java.util.function.*;

class Test { 
    void test(int    foo, Consumer<Integer> bar) { }
    void test(long   foo, Consumer<Long>    bar) { }
    void test(float  foo, Consumer<Float>   bar) { }
    void test(double foo, Consumer<Double>  bar) { }
}
Run Code Online (Sandbox Code Playgroud)

当我编译这个时,javac -Xlint Test.java我会得到一些警告:

Test.java:4: warning: [overloads] test(int,Consumer<Integer>) in Test is potentially ambiguous with test(long,Consumer<Long>) in Test
    void test(int    foo, Consumer<Integer> bar) { }
         ^
Test.java:6: warning: [overloads] test(float,Consumer<Float>) in Test is potentially ambiguous with test(double,Consumer<Double>) in Test
    void test(float  foo, Consumer<Float>   bar) { } …
Run Code Online (Sandbox Code Playgroud)

java overloading compiler-warnings java-8 functional-interface

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

使用enable_if进行C++模板重载:使用g ++和clang进行不同的行为

在解析基类的模板化成员函数的重载期间,我观察到g ++(5.2.1-23)和clang(3.8.0)之间的不同行为-std=c++14.

#include <iostream>
#include <type_traits>

struct Base
{
  template <typename T>
  auto a(T t) -> void {
    std::cout<< "False\n";
  }
};

template <bool Bool>
struct Derived : public Base
{

  using Base::a;
  template <typename T, bool B = Bool>
  auto a(T t) -> std::enable_if_t<B, void>
  {
    std::cout<< "True\n";
  }
};

int main()
{
  Derived<true> d;
  d.a(1); // fails with g++, prints "true" with clang
  Derived<false> d2;
  d2.a(1); // fails with clang++, prints "false" with g++
}
Run Code Online (Sandbox Code Playgroud)

Derived<true>::a …

c++ templates overloading c++14

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

如何正确地重载__add__方法?

我需要写一个涉及日期的课程.我应该重载+运算符以允许将日期添加到日期.解释它Date是如何工作的:一个对象以年,月,日的格式表示为(2016,4,15).向此添加整数10应该会产生(2016,4,25).该Date班有值self.year,self.month,self.day.

我的问题是代码应该在form(Date + 10)和(10 + Date)中工作.还有Date - 1.应该在添加负数天的意义上工作.Date(2016, 4, 25) - 1回报Date(2016, 4, 24).

我的代码完美地以(Date + 10)的形式工作,但不是以(10 + D)或(D - 1)形式.

def __add__(self,value):
    if type(self) != int and type(self) != Date or (type(value) != int and type(value) != Date):
        raise TypeError
    if type(self) == Date:
        day = self.day
        month = self.month
        year …
Run Code Online (Sandbox Code Playgroud)

python overloading class

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