将int []转换为short [],反之亦然(Converting int[] to short[] and vice versa)
我正在尝试使用Xbox 360和SDK 1.8的Kinect来计算玩家的表面积。 我遇到了一个小呃逆,我需要将
short[]
转换为int[]
,反之亦然。 解释更多...short[] rawDepthData = new short[depthFrame.PixelDataLength]; depthFrame.CopyPixelDataTo(rawDepthData);
但我需要
int[]
格式的rawDepthData
..帮助赞赏非常感谢
I'm trying to calculate the surface area of a player using Kinect for Xbox 360 and SDK 1.8. I've ran into a small hiccup where I need to convert an
short[]
toint[]
and vice versa. to explain more...short[] rawDepthData = new short[depthFrame.PixelDataLength]; depthFrame.CopyPixelDataTo(rawDepthData);
But I need the
rawDepthData
inint[]
format..Help appreciated Many Thanks
原文:https://stackoverflow.com/questions/36842664
最满意答案
有人可以帮助我理解为什么在Python中,即使没有除self之外的构造函数参数,也需要用括号实例化一个类。
原因很简单:当你实例化一个对象时,你实际上正在调用它的类(它本身就是一个对象),并用
()
调用对象。在Python中, 一切都是一流的对象 ,甚至是类(和函数!)本身。 为了使一个类成为第一类对象,它遵循这个类需要它自己的类( 元类 )来定义它的行为。 我们称之为“元类”类的类,以避免在讨论类和类时产生混淆。
回答你的问题的第二部分:当你使用
MainViewController
而不是MainViewController()
时,发生了“事情”,因为MainViewController
是一个完整的对象 ,就像任何其他对象一样。所以你可能会问:什么是
MainViewController
对象的类 - 实际上是元类?
如你所知,你可以像这样创建一个类:
class MyClass: pass
当你这样做时,你实际上正在创建一个名为
type
的元类的新实例。请注意,您可以通过这种方式创建相同的类; 下面和上面几乎没有区别:
MyClass = type('MyClass', (object,), {})
type
元类是所有类的基本元类。 所有的python“新风格的类”(不再是“新”了,因为它们是在python 2.1中实现的,我相信)是类type
:print(type(MyClass)) # type print(type(list)) # type print(type(int)) # type # Note that above, type is being used as a "function" (it's really just a callable)
有趣的是,
type
甚至是它自己的元类:print(type(type)) # type
所以重申:类
MyClass
实际上是一个type
的实例。 接下来, 调用该类将导致运行其元类的__call__
方法。当你这样做时:
obj = MyClass()
...您正在调用
MyClass
,结果(在后台)运行方法type.__call__()
。所有用户定义的类都是这种情况,顺便说一句; 如果在类中包含
__call__
方法,则可以调用您的类,并且在调用类实例时执行__call__
方法:class MyCallable(): def __call__(self): print("You rang?") my_instance = MyCallable() my_instance() # You rang?
你可以看到这在行动。 如果通过子
type
创建自己的元类,那么当创建基于自定义元类的类实例时,可能会导致发生这种情况。 例如:class MyMeta(type): def __call__(self, *args, **kwargs): print "call: {} {} {}".format(self, args, kwargs) return super().__call__(*args, **kwargs) # Python 3: class MyClass(metaclass = MyMeta): pass # Python 2: class MyClass(): __metaclass__ = MyMeta pass
现在,当您执行
MyClass()
,您可以看到MyMeta
的__call__
方法发生在其他任何事情之前(包括__new__
之前和__init__
之前)。Can someone please help me understand why in Python you need to instantiate a class with parenthesis even if there are no constructor arguments other than self.
The reason is simple: when you instantiate an object, you are actually calling its class (which is itself an object), and you call objects using
()
.In python, everything is a first-class object, even classes (and functions!) themselves. In order for a class to be a first class object, it follows that the class needs its own class (metaclass) to define its behavior. We call the class of a class "metaclass" so as to avoid confusion when talking about classes and classes of classes.
To answer the second part of your question: "things" were happening when you used
MainViewController
instead ofMainViewController()
becauseMainViewController
is a full-fledged object, just like any other object.So you might ask: what is the class - actually the metaclass - of the
MainViewController
object?
As you know, you can create a class like this:
class MyClass: pass
When you do this, you are in actuality creating a new instance of the metaclass known as
type
.Note that you can create the same class this way; there is literally no difference between the below and the above:
MyClass = type('MyClass', (object,), {})
The
type
metaclass is the base metaclass of all classes. All python "new style classes" (not so "new" anymore since they were implemented in python 2.1, I believe) are of the classtype
:print(type(MyClass)) # type print(type(list)) # type print(type(int)) # type # Note that above, type is being used as a "function" (it's really just a callable)
Interestingly enough,
type
is even its own metaclass:print(type(type)) # type
So to reiterate: the class
MyClass
is actually an instantiation oftype
. It follows, then, that calling the class results in running the__call__
method of its metaclass.When you do:
obj = MyClass()
...you are calling
MyClass
, which results (in the background) in running the methodtype.__call__()
.This is the case with all user defined classes, btw; if you include the
__call__
method in your class, your class is callable, and the__call__
method is executed when you call class instances:class MyCallable(): def __call__(self): print("You rang?") my_instance = MyCallable() my_instance() # You rang?
You can see this in action. If you create your own metaclass by subclassing
type
, you can cause things to happen when an instance of the class based on your custom metaclass is created. For example:class MyMeta(type): def __call__(self, *args, **kwargs): print "call: {} {} {}".format(self, args, kwargs) return super().__call__(*args, **kwargs) # Python 3: class MyClass(metaclass = MyMeta): pass # Python 2: class MyClass(): __metaclass__ = MyMeta pass
Now when you do
MyClass()
, you can see that the__call__
method ofMyMeta
happens before anything else (including before__new__
AND before__init__
).
相关问答
更多-
这是GCC的错误58751 。 你的代码应该像在Clang中那样进行编译。 过去,GCC在继承具有虚拟继承的构造函数时遇到了问题。 解决方法是手动编写转发构造函数。 class B : virtual public A { public: B(int i) : A(i) {} }; class C : virtual public A { public: C(int i) : A(i) {} }; This is GCC bug 58751. Your code should compile a ...
-
两种设计方法要考虑 精华模式 流畅的界面模式 这些都是意图相似的,因为我们慢慢建立一个中间对象,然后在一个步骤中创建我们的目标对象。 流动界面的一个例子是: public class CustomerBuilder { String surname; String firstName; String ssn; public static CustomerBuilder customer() { return new CustomerBuilder(); ...
-
你的B :: B(const B&)期望引用const B,但你提供的是A的实例.B从A派生并不重要; 即使每个B都是A,反之亦然。 实际上,object1是-A,但是不是B. 为了澄清更多:如果你想要B可以从A构造,你将需要提供一个带A的构造函数,如: B::B(const A& copyA): A(copyA) {} Your B::B(const B&) expects a reference to a const B, but you're providing an instance of an A ...
-
将继承的类构造函数参数传递给基础构造函数参数(Pass Inherited Class Constructor Arguments to Base Constructor Arguments)[2021-03-05]
更符合OO标准的方法是使用抽象属性。 这意味着基类已定义属性,但扩展类必须实现它。 这也意味着您无法直接创建vehicle实例,而必须创建一个派生类型。 abstract class vehicle { vehicle() { } public abstract string Model { get; } } // Inherited classes class tesla : vehicle { private readonly string model = "Mod ... -
是否可以引用Scala案例类构造函数参数的类型?(Is it possible to refer to the types of Scala case class constructor arguments?)[2021-02-24]
看作是一个可接受的解决方案允许将预处理功能从实例移动到关联的对象,剩下的主要困难是您希望能够抽象出案例类构造函数的参数和类型(即形状)。 这对于HList实现和多态函数值无形是可能的。 首先是一些预赛, import shapeless.HList._ import shapeless.Functions._ import shapeless.Poly._ import shapeless.TypeOperators._ // Implementation of your Input wrapper ca ... -
子类构造函数是否需要超类构造函数的所有参数?(Does a subclass constructor require all arguments of superclass constructor?)[2023-12-07]
您必须提供构造函数的所有参数。 在你的情况下,你仍然可以调用Staff的构造函数,但是你必须提供一些默认值,如下所示: super(number, title, name, "Entry level Advanced Staff", 'A'); 这与您在AdvancedStaff的构造函数中已经完成的工作完成相同的工作,现在只有Staff类设置私有变量的值,因为您通过构造函数传递它。 另外,如果您计划从子类访问这些私有变量,那么您应该真正使它们protected 。 You have to provide ... -
有人可以帮助我理解为什么在Python中,即使没有除self之外的构造函数参数,也需要用括号实例化一个类。 原因很简单:当你实例化一个对象时,你实际上正在调用它的类(它本身就是一个对象),并用()调用对象。 在Python中, 一切都是一流的对象 ,甚至是类(和函数!)本身。 为了使一个类成为第一类对象,它遵循这个类需要它自己的类( 元类 )来定义它的行为。 我们称之为“元类”类的类,以避免在讨论类和类时产生混淆。 回答你的问题的第二部分:当你使用MainViewController而不是MainViewC ...
-
StructureMap和不能接受构造函数参数的类(StructureMap and classes that cannot accept constructor arguments)[2022-03-25]
使用setter注入。 看到这里 For().Use (); Policies.FillAllPropertiesOfType (); Use setter injection. See here For ().Use (); Policies.FillAllPropertiesOfType (); -
为什么COM类需要构造函数?(Why COM classes need constructor? How to use COM class without registration?)[2022-03-03]
它与COM无关,只是框架以这种方式构建,并且DirectShow BaseClasses中的构造函数参数是将所有内容组合在一起所必需的 - 类,祖先,实例化工厂。 使用COM类而不注册可能是也可能不可能。 我怀疑你可能有兴趣在没有注册的情况下使用DirectShow过滤器,并且有一篇关于该主题的好文章: 使用没有注册的过滤器 。 It has nothing to do with COM, it is just the framework is built this way and constructor ... -
您的代码段中没有可选的括号。 以下示例在构造函数类名称周围有可选括号 ,并忽略这些括号: class MyClass { (MyClass)(); }; MyClass::MyClass() { } There are no optional parentheses in your code snippet. The following example has optional parentheses around the constructor class name, and these pa ...