首页 \ 问答 \ 将int []转换为short [],反之亦然(Converting int[] to short[] and vice versa)

将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[] to int[] and vice versa. to explain more...

        short[] rawDepthData = new short[depthFrame.PixelDataLength];
        depthFrame.CopyPixelDataTo(rawDepthData);

But I need the rawDepthData in int[] format..

Help appreciated Many Thanks


原文:https://stackoverflow.com/questions/36842664
更新时间:2022-09-13 08:09

最满意答案

有人可以帮助我理解为什么在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 of MainViewController() because MainViewController 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 class 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)

Interestingly enough, type is even its own metaclass:

print(type(type)) # type

So to reiterate: the class MyClass is actually an instantiation of type. 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 method type.__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 of MyMeta happens before anything else (including before __new__ AND before __init__).

相关问答

更多

相关文章

更多

最新问答

更多
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
  • 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
  • 以编程方式创建UITableView(Creating a UITableView Programmatically)
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
  • Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
  • 设置kotlin扩展名(Setting a kotlin extension)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 如何在vim中启用python3?(How to enable python3 in vim?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • dedecms如何安装?
  • 在哪儿学计算机最好?
  • 学php哪个的书 最好,本人菜鸟
  • 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
  • 如何覆盖错误堆栈getter(How to override Error stack getter)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
  • 电脑高中毕业学习去哪里培训
  • 正则表达式验证SMTP响应(Regex to validate SMTP Responses)