这是一个简化的示例:
class PersonParms{
name:string;
lastName:string;
age?:number;
get fullName(){return this.name + " "+this.lastName;}
}
class Person{
constructor(prms:PersonParms){
}
}
new Person({name:'John',lastName:'Doe'}) // ts error: Property 'fullName' is missing in type '{ name: string; lastName: string; }'.
Run Code Online (Sandbox Code Playgroud)
这个想法是将一个文字对象作为PersonParms的inizizalizer传递,但是拥有该getter既不能声明该getter可选,也不能将该属性添加到对象文字中。还有另一种方法可以实现吗?
我有这个 javascript 代码
class Customer{
...
}
let customers:Customer[]=[];
customers.lastDateRetrieved=new Date;
Run Code Online (Sandbox Code Playgroud)
不知何故,客户既是一个类(它有属性)又是一个数组。你会如何在打字稿中声明这个结构?我找不到从数组派生的方法(如果有道理的话)
我想截取QSlider上的QPaintEvent并绘制它.但是我无法找到关于物体几何形状的细节.我可以知道整个小部件的rect(),但是如何判断小部件矩形中第一个tickmark或最后一个的位置?(跟踪通道左右两侧有一个边距).还是"手柄"的矩形?
我想用不包含该类中所有元素但其元素在该类中的对象文字初始化一个类的实例。
class ATest{
aStr:string;
aNum:number;
result(){return this.aStr + this.aNum;}
}
let test=new ATest;
test={aStr:"hello",aNum:12}; // error, result() is missing in the object literal
Run Code Online (Sandbox Code Playgroud)
利用ts的大部分验证进行分配的方式是什么?
test=Object.assign(test,{aStr:"hello",aNom:12});
Run Code Online (Sandbox Code Playgroud)
可以,但是您错过了输入字段的验证-请参阅aNom错了,但可以进入
我想删除“我接受协议” / “不接受”复选框,并将“下一步”按钮的文字替换为简单的“我同意”,毕竟看起来更清晰直接。
您能给我一些示例或在文档中查找的位置吗?
是否可以设置样式QMenu:item以设置自定义高度?
我需要它用于弹出菜单,通过我一直看到它似乎不可能,但也许有人确切知道.
令人惊讶的是,Typescript 可以给出函数或类方法的返回类型,ReturnType<>如下所示:
class Foo{
bar(){ return {one:1, two:2};}
}
type typeBar = ReturnType<Foo['bar']>;
Run Code Online (Sandbox Code Playgroud)
但是,如果该方法是异步的,是否可以获得已解决的 Promise 的类型?
class Foo{
async asyncBar() { return new Promise((resolve) => resolve({ one: 1, two: 2 }));}
}
type typeBar = ReturnType<Foo['asyncBar']>; // the type here is Promise
Run Code Online (Sandbox Code Playgroud)
那么要从什么运营商那里得到{one:number, two:number}呢Foo['asyncBar']?
什么是最干净的方法来判断PHP是否已经通过POST调用,而不是来自html(在web broser下)
谢谢,
让我们说有
class Foo{
public:
bool error;
......
bool isValid(){return error==false;}
};
Run Code Online (Sandbox Code Playgroud)
在某个地方
Foo *aFoo=NULL;
Run Code Online (Sandbox Code Playgroud)
我通常会这样做 if (aFoo!=NULL && aFoo->isValid()).....
但是如果在isValid方法中我测试了nullity:
bool isValid(){return this!=NULL && error==false)
Run Code Online (Sandbox Code Playgroud)
这将通过简单的调用简化外部测试 if (aFoo->isValid())
我已经在一些编译器中对它进行了测试并且它可以工作但是我想知道它是否是标准的并且在移植到其他环境时可能会导致问题.
在宏中,我想生成一个具有不同名称的变量,并尝试用__LINE__它来区分它们.简化示例:
#define UNIQUE_INT int prefix##__LINE__
UNIQUE_INT;
UNIQUE_INT;
Run Code Online (Sandbox Code Playgroud)
但似乎__LINE__没有扩大,因为我在第二个中得到"int prefix__LINE__':重新定义".
我想这__LINE__不能在宏定义中使用,就好像它扩展会对#definition的行号而不是宏的调用行所做的那样,但是让我问一下,以防有人有话要说.
async function sleep(msecs){
if (!somebodyAwaitsMe()) // that's the unknown
throw ("call me with await");
return new Promise(resolve => setTimeout(resolve, ms));
}
await sleep(5000); // correct
sleep(5000); // forgot await, returns immediately,
Run Code Online (Sandbox Code Playgroud)
我想知道,在异步函数中,是否使用await调用了它,这可能吗?
该代码举例说明了一种可能的情况,以帮助检测错误,但它在其他情况下可能很有用,在其他情况下,可以通过设计调用函数,无论有没有等待。
我有一个大纹理(一张照片)和一个小的简单网格,它们应该显示主纹理的区域。这些网格应该有一个 alphaMap,例如一个圆。
在网格材质的纹理 (PlaneBufferGeometry) 中,我更改了 UV 映射,以便它将显示主纹理的正确区域。问题是 UV 贴图也应用于 alphaMap,但我希望它独立。
const planeGeometry = new THREE.PlaneBufferGeometry(sz.x, sz.y);
let uvs= planeGeometry.attributes.uv;
for (let ix = 0; ix< uvs.array.length; ix++){
let x = uvs.getX(ix);
let y = uvs.getY(ix);
uvs.setXY(ix, x*.5, y*.75); // just testing
}
const planeMaterial = new THREE.MeshPhongMaterial({
map: imageTexture,
transparent: true,
alphaMap ,
normalMap,
});
Run Code Online (Sandbox Code Playgroud)
有没有办法只改变材质贴图的UV贴图,而让其他贴图(alphaMap和法线贴图)保持独立......或者还有其他方法吗?
有没有办法在c ++中确定方法在运行时是纯虚拟的?实际上问题是,是否有办法知道派生类的析构函数是否已经执行但基类仍然存活.
这是我的情况(简化):
class BaseClass{
private:
class ThreadUtil *threadUtil;
public:
Mutex mutex;
~BaseClass(){
threadUtil->Terminate();
MutexLocker ml(mutex); // Avoid destruction during use
}
virtual Size size()=0;
};
class Derived:public BaseClass{
public:
Size size()override{return Size(100,80);}
};
class ThreadUtil{
private:
bool terminate;
BaseClass *owner;
public:
void Run(){
while(!terminate){
if (!IS_OWNER_SIZE_FN_PURE_THAT_S_THE_QUESTION){
MutexLocker ml(owner->mutex);
DoSomething(owner->size()); // Runtime error if in the dtor of BaseClass
}
}
}
};
Run Code Online (Sandbox Code Playgroud)
"纯虚函数称为"运行时错误非常偶发(当执行~BaseClass时调用DoSomething时).
在派生类中终止线程+锁定是安全的,但我想在BaseClass中执行它(特别是如果有许多派生类).
是否有便携和干净(没有标志)的方式来实现这个?...或者上述设计有什么问题?
编辑: - - - - - - - - - -
正如一些人所指出的那样,纯虚拟并不是真正的问题.它正在进入基类的析构函数,线程仍在运行.实际的问题可能是," 有没有办法在基类中使用预析构函数方法? " …
typescript ×4
c++ ×3
asynchronous ×2
qt ×2
arrays ×1
class ×1
custom-draw ×1
destructor ×1
getter ×1
inno-setup ×1
javascript ×1
macros ×1
menu ×1
node.js ×1
null ×1
optional ×1
php ×1
pure-virtual ×1
qmenu ×1
return-type ×1
slider ×1
three.js ×1