这应该很容易,但因为我遗漏了一些东西.
我有一个完全符合我预期的对象.
class TextElement(ContentItemElement):
'''
Single String Elements, for example, headlines
'''
def __init__(self, name, data):
super(TextElement, self).__init__()
self.name=name
self.text=data
def prettyPrint(self):
printstring = u'*HTML* '
self.name.encode('utf-8')
printstring += u'<h3> '+self.name+u' </h3>'
self.text.encode('utf-8')
printstring += u'<p> '+self.text+u' </h3>'
print printstring
Run Code Online (Sandbox Code Playgroud)
好的,很好,我可以实例化它,它完全符合我的要求.但我真的想创建一个更具体的TextObjects版本.所以我这样做:
class CiteElement(TextElement):
'''
Single String Elements, for example, headlines
'''
def __init__(self, name, data):
super(CiteElement, self).__init__()
self.validValues=['Crap I make up', 'Crap I found on the web']
Run Code Online (Sandbox Code Playgroud)
但是当我尝试实例化它时,这有效:
ee = TextElement(element, self.raw[element])
ee.validValues=['Crap I make up', 'Crap I found on …Run Code Online (Sandbox Code Playgroud) 在C++中,Decorator软件设计模式适合在有类型时使用,可以实例化但有一个限制,不允许您从该类继承以添加某些功能.
我的问题是:在C++中我可以拥有一个类的实例但不能从同一个类继承的情况是什么?
我在Java编译器中长大,只要类中没有显式构造函数,就会自动生成一个默认构造函数; 当我有任何显式构造函数时不生成.
据我所知,构造函数定义了所需的依赖项,属性定义了可选的依赖项(很可能是默认值...由构造函数设置).如果你坚持上述规则(我在职业生涯中根据经验选择),那么在面向对象的代码中能够在<init>() 没有定义时调用是完全错误的.
这是一个我尝试过的简单测试,并注意到即使使用显式构造函数,也可以很容易地实例化没有args的对象.如何在编译时或运行时在标有????的行上使该程序失败?
class TestGroovy {
private final String name
TestGroovy(String name) {
this.name = name
}
static void main(String[] args) {
testStatic()
println()
testDynamic()
println()
testReflection()
}
@groovy.transform.CompileStatic
static void testStatic() {
println new TestGroovy("static");
println "compile error"
// Groovyc: [Static type checking] - Cannot find matching method TestGroovy#<init>().
// Please check if the declared type is right and if the method exists.
//println new TestGroovy(); // correct
}
static void testDynamic() …Run Code Online (Sandbox Code Playgroud) 所以我有一个作业说:
请使用包/类的通用实例化。必须在通用包/模板内的系统堆栈中为每个 BMR(矩阵)分配空间,可能是在通用实例化期间!您特别不能使用“new”、“malloc”或任何其他运算符,这些运算符在运行时以任何语言在堆中分配空间。用荧光笔清楚地标记这部分代码!您必须阅读所有事务并打印所有结果在通用包/模板中。I/O 例程必须作为通用参数传递
和不相关的代码:
generic
type subscript is (<>); --range variable to be instantiated
type myType is private; --type variable to be intantiated
package genericArray is
type userDefinedArray is array(subscript range <>) of myType;
end genericArray;
Run Code Online (Sandbox Code Playgroud)
with ada.text_io; use ada.text_io;
with genericArray;
procedure useGenericArray is
type month_name is (jan, feb, mar, apr, may, jun,
jul, aug, sep, oct, nov, dec);
type date is
record
day: integer range 1..31; month: month_name; year: integer;
end record;
type family is (mother, father, …Run Code Online (Sandbox Code Playgroud) 我正在查看关于Cascade Classifiers的OpenCV教程,并且碰巧看到了这种语法:
std::vector<Rect> faces;
Mat frame_gray;
Mat faceROI = frame_gray( faces[i] );,
Run Code Online (Sandbox Code Playgroud)
在frame_gray实例化和faceROI实例化之间还有一些其他代码.我的问题是 - faceROI实例化行正在做什么/它是如何工作的?它看起来像复制构造函数用法,但faces [i]参数让我感到困惑.
http://docs.opencv.org/trunk/db/d28/tutorial_cascade_classifier.html - 教程 http://docs.opencv.org/3.1.0/d3/d63/classcv_1_1Mat.html#af1d014cecd1510cdf580bf2ed7e5aafc - Mat类的文档
我有一Component节课:
class Component
{
}
Run Code Online (Sandbox Code Playgroud)
然后是一个Collider继承自它的类:
class Collider :public Component
{
}
Run Code Online (Sandbox Code Playgroud)
并且SphereCollider继承自Collider:
class SphereCollider :public Collider
{
}
Run Code Online (Sandbox Code Playgroud)
最后,GameObject您可以使用添加或删除组件的类.
该GameObject.h标题:
class GameObject
{
//Add functionality
template <class T>
T& AddComponent();
//Get functionality
template <typename T>
T& GetComponent();
}
Run Code Online (Sandbox Code Playgroud)
该GameObject.cpp来源:
template <typename T>
T& GameObject::AddComponent(){}
template <typename T>
T& GameObject::GetComponent(){}
//Explicit instantiation
template Component &GameObject::GetComponent<Component>();
template Component &GameObject::AddComponent<Component>();
template SphereCollider &GameObject::GetComponent<SphereCollider>();
template SphereCollider &GameObject::AddComponent<SphereCollider>();
Run Code Online (Sandbox Code Playgroud)
一切都很好.我可以在没有指针的情况下从GameObject添加或获取组件,如下所示:
GameObject …Run Code Online (Sandbox Code Playgroud) 例如,考虑来自 Google I/O '17“Android Animations Spring to Life”的幻灯片:
SpringForce force = new SpringForce(0)
.setDampingRation(0.4f)
.setStiffness(500f);
for (int i = 0; i < heads.getChildCount(); i++) {
View child = heads.getChildAt(i);
SpringAnimation anim;
anim = new SpringAnimation(child, DynamicAnimation.ROTATION);
anim.setSpring(force).setStartValue(-25).start();
}
Run Code Online (Sandbox Code Playgroud)
在那里我们可以看到变量anim在一行上定义,变量的实例在下一行创建。有时我也在一些开源项目中看到这种方法。
使用这种方法是否有真正的好处,或者只是风格或可读性的问题?或者,在幻灯片的情况下,这是适合幻灯片宽度的问题?但如果是这样,他们可能会写这样的东西:
SpringAnimation anim = new SpringAnimation(
child, DynamicAnimation.ROTATION);
Run Code Online (Sandbox Code Playgroud) 我遇到了一个类似的代码:
SampleObject<int>* example = new SampleObject<int>::aMethod();
Run Code Online (Sandbox Code Playgroud)
无法理解它,我不熟悉 C++。
有人可以向我解释一下吗?
我正在尝试使用三元组来返回多个值,但我得到“无法实例化三元组类型”我尝试了多种方法但没有任何效果。什么是正确的语法?
import org.apache.commons.lang3.tuple.Triple;
private static Triple<String, String, String> test() {
Triple<String, String, String> triple = new Triple<>();
...
return triple;
}
Run Code Online (Sandbox Code Playgroud) 我有一个课程列表:
val availableClasses = listOf<Whatever>(
classA(),
classB(),
classC()
)
Run Code Online (Sandbox Code Playgroud)
我使用以下方法从该列表中随机选择一个项目:
private var selection: Whatever = availableClasses.random()
Run Code Online (Sandbox Code Playgroud)
不幸的是,我认为这种方法是在加载列表时实例化列表中包含的每个类。
我希望通过用字符串列表替换类列表来解决这个问题:
val availableClasses = listOf<String>(
"classA",
"classB",
"classC"
)
Run Code Online (Sandbox Code Playgroud)
然后一旦我选择了一个字符串,就只实例化那个字符串;就像是:
private var selection: String = availableClasses.random()
// pseudo-code
val chosenClass = selection.toClass()
Run Code Online (Sandbox Code Playgroud)
我可以使用带有getattr函数的字符串来引用 Python 中的类。
在 Kotlin 中有这样的事情吗?
我也愿意接受更好的方法来解决这个问题。
instantiation ×10
c++ ×4
java ×2
object ×2
ada ×1
android ×1
constructor ×1
decorator ×1
generics ×1
groovy ×1
heap-memory ×1
inheritance ×1
instance ×1
kotlin ×1
oop ×1
opencv ×1
python ×1
stack-memory ×1
templates ×1
triples ×1