在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)
我怀疑没有办法实现这一目标,但我认为这不会有问题!
我正在处理的项目包含类似call_user_func(_array)的包装器,它在执行之前执行一些检查.其中一个检查是method_exists(In Case,提供的第一个参数是类的实例,第二个参数是方法名称)另一个是is_callable.如果其中一个检查失败,该函数将抛出异常.
我的代码包含一个带有函数名称的数组(setFoo,setBar等)和用于重载(__call)的php magic函数,它处理某些变量的设置,替换和删除(更好的某些数组元素).
问题:如果未定义函数,method_exists将返回false.
如果__call函数正确处理请求,我是否有机会得到真的?
我已经做了很多方法重载,但现在我有一个实例,我想重载一个属性.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让我这么做,我开始认为这是不可能的?
我已经定义了多个构造函数,在所有构造函数中都有一些默认参数值.看起来正确(我看不出任何歧义),但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)
}
}
在我的库中,我有很多函数重载的形式:
/// \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也会受到赞赏.
我试图了解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) 在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) 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
在解析基类的模板化成员函数的重载期间,我观察到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 …
我需要写一个涉及日期的课程.我应该重载+运算符以允许将日期添加到日期.解释它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) overloading ×10
c++ ×3
java ×2
python ×2
templates ×2
binary ×1
c++11 ×1
c++14 ×1
class ×1
constructor ×1
default ×1
doxygen ×1
java-8 ×1
php ×1
properties ×1
python-2.6 ×1
scala ×1
vb.net ×1