精华内容
下载资源
问答
  • Instantiation

    2010-08-11 16:20:24
    [b]What does the new operator do?[/b] [code="java"] function Ninja(){ this.name = "Ninja"; } var ninjaA = Ninja();...Is undefined, not an instance of Ninja."... var ni...
    [b]What does the new operator do?[/b]
    
     function Ninja(){
    this.name = "Ninja";
    }

    var ninjaA = Ninja();
    assert( !ninjaA, "Is undefined, not an instance of Ninja." );

    var ninjaB = new Ninja();
    assert( ninjaB.name == "Ninja", "Property exists on the ninja instance." );

    [b]We have a 'this' context that is a Ninja object.[/b]
     function Ninja(){ 
    this.swung = false;

    // Should return true
    this.swingSword = function(){
    this.swung = !this.swung;
    return this.swung;
    };
    }

    var ninja = new Ninja();
    assert( ninja.swingSword(), "Calling the instance method." );
    assert( ninja.swung, "The ninja has swung the sword." );

    var ninjaB = new Ninja();
    assert( !ninjaB.swung, "Make sure that the ninja has not swung his sword." );

    [b]QUIZ: Add a method that gives a name to the ninja.[/b]
     function Ninja(name){
    // Implement!
    }

    var ninja = new Ninja("John");
    assert( ninja.name == "John", "The name has been set on initialization" );

    ninja.changeName("Bob");
    assert( ninja.name == "Bob", "The name was successfully changed." );

    [b]Add a new property and method to the object.[/b]
     function Ninja(name){
    this.changeName = function(name){
    this.name = name;
    };

    this.changeName( name );
    }

    var ninja = new Ninja("John");
    assert( ninja.name == "John", "The name has been set on initialization" );

    ninja.changeName("Bob");
    assert( ninja.name == "Bob", "The name was successfully changed." );

    [b]What happens when we forget to use the new operator?[/b]
    function User(first, last){ 
    this.name = first + " " + last;
    }

    var user = User("John", "Resig");
    assert( typeof user == "undefined", "Since new wasn't used, the instance is undefined." );

    [b]What happens when we forget to use the new operator? (cont.)[/b]
     function User(first, last){ 
    this.name = first + " " + last;
    }

    window.name = "Resig";
    var user = User("John", name);

    assert( name == "John Resig", "The name variable is accidentally overridden." );

    [b]We need to make sure that the new operator is always used.[/b]
     function User(first, last){ 
    if ( !(this instanceof User) )
    return new User(first, last);

    this.name = first + " " + last;
    }

    var name = "Resig";
    var user = User("John", name);

    assert( user, "This was defined correctly, even if it was by mistake." );
    assert( name == "Resig", "The right name was maintained." );

    [b]QUIZ: Is there another, more generic, way of doing this?[/b]
     function User(first, last){ 
    if ( !(this instanceof ___) )
    return new User(first, last);

    this.name = first + " " + last;
    }

    var name = "Resig";
    var user = User("John", name);

    assert( user, "This was defined correctly, even if it was by mistake." );
    assert( name == "Resig", "The right name was maintained." );

    [b]A solution using arguments.callee.[/b]
     function User(first, last){ 
    if ( !(this instanceof arguments.callee) )
    return new User(first, last);

    this.name = first + " " + last;
    }

    var name = "Resig";
    var user = User("John", name);

    assert( user, "This was defined correctly, even if it was by mistake." );
    assert( name == "Resig", "The right name was maintained." );

    [b]Flexib
    展开全文
  • Java - 实例化(instantiation) 详解.pdf
  • E:\project\qt\Pokemon3\PokemonServer\pokemon.cpp:470: error: specialization of ‘Race::Race() [with int N = 0]’ after instantiation Race<0>::Race() : PokemonBase(ATK) ^ 报错2: explicit ...

    报错1:
    E:\project\qt\Pokemon3\PokemonServer\pokemon.cpp:470: error: specialization of ‘Race::Race() [with int N = 0]’ after instantiation
    Race<0>::Race() : PokemonBase(ATK)
    ^
    报错2:
    explicit specialization of ‘Race’ after instantiation
    implicit instantiation first required here

    instantiation 实例化
    specialization 特殊化/具体化

    错误原因:显示具体化放在实例化之后,这里首先需要隐式实例化

    查阅资料后了解到:
    显式具体化声明在关键字 template后面包含<>,而显式实例化没有。

    源码 pokemon.h
    template <int N>
    class Race : public PokemonBase
    {
    public:
        Race();
        virtual ~Race() = default;
        bool attack(Pokemon& attacker, Pokemon& aim, string& msg, int skillIndex = 0) const;
    };
    
    源码pokemon.cpp
    
    template <>
    Race<0>::Race() : PokemonBase(ATK)
    {
        // _raceName = "Charmander";
        _raceName = "小火龙";
        _expCurve[0] = 5;
        for (int i = 1; i < 14; ++i)
        {
            _expCurve[i] = _expCurve[i - 1] + 5 * i;
        }
        _skillName[0] = "撞击";
        _skillName[1] = "火花";
        _skillName[2] = "怒气";
        _skillName[3] = "火球";
        _skillDscp[0] = "普通攻击";
        _skillDscp[1] = "忽略敌人一半防御的攻击";
        _skillDscp[2] = "增加攻击力";
        _skillDscp[3] = "伤害很高的大招";
        _pp[0] = 10;
        _pp[1] = 3;
        _pp[2] = 5;
    }
    
    template <>
    bool Race<0>::attack(Pokemon& attacker, Pokemon& aim, string& msg, int skillIndex) const
    {
        // dbout << attacker.name() << " uses " << attacker.skillName(skillIndex) << "!\n";
    
        msg += attacker.skillName(skillIndex) + ' ';
    
        switch (skillIndex)
        {
        case 1: //spark
        {
            if (dodge(attacker.cspeed(), aim.cspeed(), msg))
                return false;
    
            int dmg = attacker.catk() + attacker.lv() * 2 - aim.cdef() / 2 + f(4);
            // return aim.takeDamage(dmg);
            bool result = aim.takeDamage(dmg);
            msg += to_string(aim.hp()) + " 1 1 1 ";
            for (int i = 0; i < 3; ++i)
            {
                msg += to_string(aim.cpp(i)) + ' ';
            }
            msg += to_string(attacker.hp()) + " 1 1 1 ";
            for (int i = 0; i < 3; ++i)
            {
                msg += to_string(attacker.cpp(i)) + ' ';
            }
            return result;
    
            break;
        }
        case 2:				 //rage
            msg += "0 "; // can not dodge
    
            attacker.changeAtk(attacker.atk() / 8);
            msg += to_string(aim.hp()) + " 1 1 1 ";
            for (int i = 0; i < 3; ++i)
            {
                msg += to_string(aim.cpp(i)) + ' ';
            }
            msg += to_string(attacker.hp()) + " 2 1 1 ";
            for (int i = 0; i < 3; ++i)
            {
                msg += to_string(attacker.cpp(i)) + ' ';
            }
            break;
        case 3: //fireball
        {
            if (dodge(attacker.cspeed(), aim.cspeed(), msg))
                return false;
    
            int dmg = attacker.catk() * 1.5 - aim.cdef() + 8 + f(4 + attacker.lv());
            bool result = aim.takeDamage(dmg);
            msg += to_string(aim.hp()) + " 1 1 1 ";
            for (int i = 0; i < 3; ++i)
            {
                msg += to_string(aim.cpp(i)) + ' ';
            }
            msg += to_string(attacker.hp()) + " 1 1 1 ";
            for (int i = 0; i < 3; ++i)
            {
                msg += to_string(attacker.cpp(i)) + ' ';
            }
            return result;
    
            break;
        }
        default:
        {
            //普通攻击
            if (dodge(attacker.cspeed(), aim.cspeed(), msg))
                return false;
    
            int dmg = attacker.catk() - aim.cdef() + f(4);
            bool result = aim.takeDamage(dmg);
            msg += to_string(aim.hp()) + " 1 1 1 ";
            for (int i = 0; i < 3; ++i)
            {
                msg += to_string(aim.cpp(i)) + ' ';
            }
            msg += to_string(attacker.hp()) + " 1 1 1 ";
            for (int i = 0; i < 3; ++i)
            {
                msg += to_string(attacker.cpp(i)) + ' ';
            }
            return result;
    
            break;
        }
        } //switch
        return false;
    }
    //共实例化了四个类,不一一例举
    

    调用优先级:普通函数>显式具体化>显式实例化>普通模版

    也就是具体化应该在实例化之前,我做的是具体化,之前的实例化在哪?

    发现在以上程序前还有这样一段代码:

    PokemonBase* Race0 = new Race<0>();
    PokemonBase* Race1 = new Race<1>();
    PokemonBase* Race2 = new Race<2>();
    PokemonBase* Race3 = new Race<3>();
    
    const PokemonBase* Pokemon::races[] = {Race0,Race1,Race2,Race3};
    
    

    我们可以回到本源上思考,模板类是减少代码量而产生,模板类定义了,真正的类还没有定义,既然没有定义,新建也就无从谈起

    解决方案是将后一段代码移植到前一段代码下方,问题解决。

    展开全文
  • Creational Patterns ...A creational pattern provides a particular instantiation mechanism. It can be aparticular object factory or even a class factory. This is an important pattern in compiled languag

    • Creational Patterns

      A creational pattern provides a particular instantiation mechanism. It can be aparticular object factory or even a class factory.

      This is an important pattern in compiled languages such as C, since it is harder togenerate types on-demand at run time.

    page343image432

    Useful Design Patterns

    But this feature is built-in in Python, for instance the type built-in, which lets youdefine a new type by code:

       >>> MyType = type('MyType', (object,), {'a': 1})
       >>> ob = MyType()
       >>> type(ob)
       <class '__main__.MyType'>
    
       >>> ob.a
       1
       >>> isinstance(ob, object)
       True
    

    Classes and types are built-in factories and you can interact with class and objectgeneration using meta-classes, for instance (see Chapter 3). These features are thebasics to implement the Factory design pattern and we won't further describe it inthis section.

    Besides Factory, the only other creational design pattern from the GoF that isinteresting to describe in Python is Singleton.

    Singleton

    Singleton restricts instantiation of a class to one object.

    The Singleton pattern makes sure that a given class has always only one livinginstance in the application. This can be used, for example, when you want to restricta resource access to one and only one memory context in the process. For instance,a database connector class can be a Singleton that deals with synchronization andmanages its data in memory. It makes the assumption that no other instance isinteracting with the database in the meantime.

    This pattern can simplify a lot the way concurrency is handled in an application.Utilities that provide application-wide functions are often declared as Singletons.For instance, in web applications, a class that is in charge of reserving a uniquedocument ID would benefit from the Singleton pattern. There should be one andonly one utility doing this job.

    Implementing the Singleton pattern is straightforward with the __new__ method:>>> class Singleton(object):

    page343image16032 page343image16192 page343image16616 page343image17040

    .........

    def __new__(cls, *args, **kw):
        if not hasattr(cls, '_instance'):
    
         orig = super(Singleton, cls)
    

    [ 326 ]

    page343image25024 page343image25184
    page344image416
    ...             cls._instance = orig.__new__(cls, *args, **kw)
    ...         return cls._instance
    ...
    >>> class MyClass(Singleton):
    

    ... a =1
    ...
    >>> one = MyClass()>>> two = MyClass()>>> two.a = 3
    >>> one.a
    3

    Although the problem with this pattern is subclassing; all instances will be instancesof MyClass no matter what the method resolution order (__mro__) says:

    >>> class MyOtherClass(MyClass):... b= 2
    ...
    >>> three = MyOtherClass()

       >>> three.b
       Traceback (most recent call last):
    
         File "<stdin>", line 1, in ?
       AttributeError: 'MyClass' object has no attribute 'b'
    

    To avoid this limitation, Alex Martelli proposed an alternative implementation basedon shared state called Borg.

    The idea is quite simple. What really matters in the Singleton pattern is not thenumber of living instances a class has, but rather the fact that they all share the samestate at all times. So Alex Martelli came up with a class that makes all instances of theclass share the same __dict__:

       >>> class Borg(object):
    
    •    ...      _state = {}
      
    •    ...      def __new__(cls, *args, **kw):
      
    •    ...          ob = super(Borg, cls).__new__(cls, *args, **kw)
      
    •    ...          ob.__dict__ = cls._state
      
    •    ...          return ob
         ...
         >>> class MyClass(Borg):
      

    ... a =1
    ...
    >>> one = MyClass()>>> two = MyClass()>>> two.a = 3
    >>> one.a

    [ 327 ]

    Chapter 14

    page344image15976 page344image16136 page344image16296
    page345image424

    Useful Design Patterns

    3
    >>> class MyOtherClass(MyClass):... b= 2
    ...
    >>> three = MyOtherClass()
    >>> three.b
    2
    >>> three.a
    3
    >>> three.a = 2
    >>> one.a
    2

    This fixes the subclassing issue, but is still dependent on how the subclass codeworks. For instance, if __getattr__ is overridden, the pattern can be broken.

    Nevertheless, Singletons should not have several levels of inheritance. A class that ismarked as a Singleton is already specific.

    That said, this pattern is considered by many developers as a heavy way to deal withuniqueness in an application. If a Singleton is needed, why not use a module withfunctions instead, since a Python module is a Singleton?

    The Singleton factory is an implicit way of dealing with the uniquenessin your application. You can live without it. Unless you are working in aframework à la Java that requires such a pattern, use a module insteadof a class. 





    instantiation

     
    英 [ɪnstænʃɪ'eɪʃən]      美 [ɪnstænʃɪ'eɪʃən]    
    • n.实例化;用具体例子说明;例示
    释义常用度分布图海词统计
    实例化
    例示
    用具体例..

    instantiation是什么意思,词典释义与在线翻译:

    行业释义

    英英释义

    Noun:
    1. a representation of an idea in the form of an instance of it;

      "how many instantiations were found?"

    instantiation的用法和样例:

    例句

    1. An object is instantiation of a class.
      对象是类的实列。
    2. Needed for instantiation and views.
      听 听 听 听 听-- raw symbol map.
    3. A new rule is created through the instantiation of a template.
      透过范本实例定义新的规则。
    4. Use delegate inference instead of explicit delegate instantiation.
      使用委托定义而不是直接的委托实例。
    5. The target instance to be created on template instantiation.
      将被创建在模板例示上的目标范例。
    词性常用度分布图海词统计
    名词

    展开全文
  • array instantiation can be simplified 数组的实例化可以简化

     

    array instantiation can be simplified
    数组的实例化可以简化

    展开全文
  • gobject instantiation

    2008-09-27 13:42:00
    <!-- @page { size: 8.5in 11in; margin: 0.79in } P { margin-bottom: 0.08in } --> MamanBar *bar = g_object_new(MAMAN_TYPE_BAR, NULL);first instantiation of an object,
  • Template Instantiation

    2008-12-30 23:01:00
    Template Instantiation The act of creating a new definition of a function, class, or member of a class from a template declaration and one or more template arguments is called template instantiation....
  • An Efficient Generic Framework for Three-Factor Authentication With Provably Secure Instantiation
  • spring-bean-instantiation

    2021-06-20 11:43:30
    Bean 实例化(Instantiation) • 常规方式 • 通过构造器(配置元信息:XML、Java 注解和 Java API ) • 通过静态工厂方法(配置元信息:XML 和 Java API ) • 通过 Bean 工厂方法(配置元信息:...
  • CArray,CList,CMap如何实化(实例化,instantiation).pdf
  • <div><p>The compiler needs to support class instantiation via class aliases where the aliased class is not simply instantiated with an argument list which is based solely on the parameter list of the ...
  • org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter’: Instantiation of bean ...
  • 启动报错 Bean instantiation via factory method failed 在引入 mybatis-plus 之后,启动项目总是报错 Bean instantiation via factory method failed。 排查的结果是,两个依赖的版本不一致,导致了错误的发生。
  • Spring中的Instantiation接口: Spring中的Initializing接口: 区别 Instantiation方法是在Bean的实例化阶段被调用。 Initializing方法是在Bean实例化完成后的初始化阶段被调用。 作用 Instantiation因为在...
  • Lazy Instantiation即被动初始化,当需要用到某个property时,再在此property的getters中进行初始化,例如 - (Foo *)aFoo { if (!_aFoo) { _aFoo = [[Foo alloc] init]; } return _aFoo; } ...
  • drop apply INSTANTIATION for one table http://www.itpub.net/viewthread.php?tid=1114837 begindbms_apply_adm...
  • 【Druid IO】Instantiation of [simple type, class druid.indexing.kafka.supervisor.KafkaSupervisorTunin.pdf
  • On-Demand InstantiationThis on-demand instantiation feature sets C++ templates apart from similar facilities in other compiled languages. It is sometimes also called implicit or automatic instantiatio...
  • error: implicit instantiation of undefined template ‘ssdm_int<3072, false>’ 问题描述 在编写HLS代码过程中,使用任意精度数据ap_int<>时报错,而且是头文件出了问题。 D:/xilinx/Vivado/2018.3/...
  • Instantiation of utility class 'xxx’的原因 通常会出现以下提示 原因 在对象的Class中只存在static静态变量,这里再new一个对象就多余了 解决 解决 可以在相应对象的Class中将static修饰符删除,或者在对象中...
  • Instantiation of Chaincode using Fabric Node SDK gives API error (404): manifest for hyperledger/fabric-ccenv:latest not found 原因 latesttag forhyperledger/fabric-ccenvdoes not exist anymore ...
  • 默认情况下,在Spring AOP里面的切面类是单例模式(Singleton),也...Aspect Instantiation Models 这一章节的直接翻译就是切面实例化模型,首先还是看看官网怎么说的,以下引用来自官网: By default, there is a s
  • 一个stream试验-pre_instantiation_setup/post_instantiation_setup 模仿piner的全库stream试验
  • 问题: 创建了两个自定义类:ScoreInfo 和 DataSource,...implicit instantiation of undefined template 'QList’ 解决方法: 在DataSource中增加QObject头文件引用。 代码展示: ScoreInfo.h class ScoreInfo { ...
  • <p>The calls from the editor looks legit and are done, but then cartodb.js enters an infinite loop of requests between checking status of a node and doing an map-instantiation: - ...
  • 分析处理 几个关键词:BeanCreationException、Bean instantiation via factory method failed、BeanInstantiationException、StackOverflowError。 可以分析出来是生成bean时报错了,然后看到有StackOverflowError...
  • 今天在微信开发者工具创建了一个新的小...Unhandled promise rejection TypeError: WebAssembly Instantiation: Argument 0 must be a buffer source or a WebAssembly.Module object 就很难受 去了微信开发者...
  • Unhandled promise rejection TypeError: WebAssembly Instantiation: Argument 0 must be a buffer source 今天在微信开发者工具编译小程序项目 但是打开的时候出现了这样的bug 让我觉得很摸不着头脑 重新编译又不...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 24,916
精华内容 9,966
关键字:

instantiation