是否可以用两种不同的方式实例化一个类的对象? [英] Is it possible to instantiate an object of one class in two different ways?
本文介绍了是否可以用两种不同的方式实例化一个类的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此处是创建点的示例作为p=Point(x, y)
.假设我有一些数组ppp=(x, y)
,其中x
和y
是数字,我想将其设置为类Point
,但方式是:p=Point(ppp)
.我可以采用一种或另一种方式,但不能同时进行.可以同时有两种方法吗?
Here is an example which creates a point as p=Point(x, y)
. Assume that I have some array ppp=(x, y)
where x
and y
are numbers and I want to make it of class Point
but in the way: p=Point(ppp)
. I can do either one or another way but not both simultaneously. Is it possible to have both ways?
推荐答案
如果您知道在创建实例时拥有一个元组/列表,则可以执行:p = Point(*ppp)
,其中
If you know that you have a tuple/list while creating the instance, you can do: p = Point(*ppp)
, where ppp
is the tuple.
这篇关于是否可以用两种不同的方式实例化一个类的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文