我想要的是一个函数,每次调用它时都会生成一个新的不同实例(使用不同的名称......)
像这样:
void person::new_person(){
person *(id+index) = new person(name_temp, age_temp, quote_temp);
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用......我不知道我该怎么做...(索引是每次我创建一个新实例时加一个).我意识到每次我制作指针并在其上添加空格,就像这样:
int*p;
*(p+1) = 5;
Run Code Online (Sandbox Code Playgroud)
它编译,但在运行时冻结(我把它得到的记忆不允许),所以"人*(id +索引)"也可能不起作用.你怎么看?
如果我有这样的课程:
public class Example
{
ArrayList<String> vector;
public Example singleton = new Example();
private Example()
{
//Read data from BD and fill the vector. Example vector: ["foo","voo","faa","vuu","vee"]
}
public synchronized removeElement()
{
vector.remove(0);
}
public synchronized changeElement()
{
vector.set(0,"fii");
}
}
Run Code Online (Sandbox Code Playgroud)
如果有多个实例在运行且其中一个实例执行该方法removeElement,那么另一个实例的值会发生什么?如果其中一个执行该方法changeElement?
反对下面的参考线程,即使我评论GC.KeepAlive(),我发现没有区别,它阻止任何其他实例的创建.为什么作者明确提到了它的重要路线?
我的类型Foo是简单的包装Cont a a.我想使Foo类型成为类的实例Monad.我试试这个:
import Control.Monad.Cont
newtype Foo a = Foo {unFoo :: Cont a a}
instance Monad Foo where
return = Foo . return
Foo inner >>= func = Foo (inner >>= newFunc)
where newFunc x = (unFoo $ func x)
Run Code Online (Sandbox Code Playgroud)
但我得到了这个错误:
Couldn't match type `a' with `b'
`a' is a rigid type variable bound by
the type signature for >>= :: Foo a -> (a -> Foo b) -> Foo b …Run Code Online (Sandbox Code Playgroud) 假设我创建了一个对象:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CreateObjOnobj
{
class Program
{
static void Main(string[] args)
{
testcreate myobjecttotest;
myobjecttotest = new testcreate();
myobjecttotest.num = 20;
myobjecttotest.bill = true;
myobjecttotest = new testcreate();
Console.WriteLine(myobjecttotest.bill.ToString());
Console.ReadKey();
}
}
class testcreate
{
public int num = 0;
public bool bill = false;
}
}
Run Code Online (Sandbox Code Playgroud)
这段代码是否会自动删除对象并创建一个新对象而不会丢失内存?
谢谢
在我的一个LoopBack模型中,我想向模型添加一个实例方法,但它不会让我访问this,因为this在prototype方法中未定义:
module.exports = (MyModel) => {
MyModel.prototype.doStuff = () => {
console.log(this); // outputs undefined
}
}
Run Code Online (Sandbox Code Playgroud)
这显然限制了实例方法的有用性.有没有办法做到这一点?
我正在尝试使用自然数创建一些函数.
import Prelude
data Nat = Zero | Succ Nat
add:: Nat -> Nat -> Nat
add Zero n = n
add (Succ m) n = Succ (add m n)
mult :: Nat -> Nat -> Nat
mult x Zero = Zero
mult x (Succ y) = add x (mult x y)
Run Code Online (Sandbox Code Playgroud)
然后当我跑
mult 3 5
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
<interactive>:38:6: error:
• No instance for (Num Nat) arising from the literal ‘3’
• In the first argument of ‘mult’, namely ‘3’
In …Run Code Online (Sandbox Code Playgroud) 我有两节课
public class Singleton<T> : MonoBehaviour where T : Component
{
protected static T _instance;
public static T Instance
{
get
{
return _instance;
}
}
}
Run Code Online (Sandbox Code Playgroud)
和
public class GameManager : Singleton<GameManager>
{
// Lots of Methods and Properties...
}
Run Code Online (Sandbox Code Playgroud)
我想创建3.从GameManager派生的类,例如ShooterGameManager.我可以创建ShooterGameManager
public class ShooterGameManager : GameManager
{}
Run Code Online (Sandbox Code Playgroud)
但是实例仍然是GameManager !!!,我尝试过用类通用类添加额外的类定义.
public class GameManager<T> : Singleton<T> where T : Component
{}
Run Code Online (Sandbox Code Playgroud)
但是这次有两个完全不同的类可用,没有从GameManager继承.我该怎么办 ???
我有一些子类,其中所有子类都由一个公共父类扩展.
我想要的是父类中的具体方法,该方法应该返回给定类类型的新对象.
这是我试过的.
class Vehicle {
public <T> T get(Class<T> type){
//return new T();
}
}
class Car extends Vehicle {
}
class Van extends Vehicle {
}
Run Code Online (Sandbox Code Playgroud)
以上get(...)方法
return new T();
Run Code Online (Sandbox Code Playgroud)
语句给出了编译错误,
需要意外类型:
找到的类:类型参数T.
我不能直接创建子类的对象,比如
new Car();
Run Code Online (Sandbox Code Playgroud)
由于某些原因.可以有任意数量的子类,因此get()每个子类中的重写方法也不是一种选择.任何的想法?
instance ×10
c# ×3
object ×3
haskell ×2
.net ×1
c#-4.0 ×1
c++ ×1
class ×1
constructor ×1
destructor ×1
generics ×1
inheritance ×1
java ×1
loopbackjs ×1
methods ×1
monads ×1
numbers ×1
oop ×1
singleton ×1
strongloop ×1
vb.net ×1
winforms ×1