天道酬勤,学无止境

self

Does a typedef to self have any effect?

I've come across some C++ code that has the following: typedef Request Request; Is this just a no-op or does this typedef actual have an effect, and if so, what effect does it have?

2022-06-05 21:00:09    分类:问答    c++   typedef   self   noop

Why do I get the error “Protocol … can only be used as a generic constraint because it has Self or associated type requirements”?

问题 我在Int上写了一个扩展,如下所示。 extension Int { func squared () -> Int { return self * self } } print(10.squared()) // works 上面的代码有效。 现在我想扩展IntegerType协议,以便 Int、UInt、Int64 等都符合。 我的代码如下。 extension IntegerType { func squared () -> IntegerType { // this line creates error return self * self } } 我得到错误: Protocol 'IntegerType' 只能用作通用约束,因为它具有 Self 或关联的类型要求 我已经看过这个问题及其视频和这个问题,仍然无法理解。 我只知道有一些associatedType在这种情况下是Self但无法连接点。 我觉得我对Generics学科缺乏知识也是一个原因...... 有人可以详细说明一下这个主题,为什么扩展会产生错误? 回答1 你只需要返回Self 编辑/更新: 注意:您可以在 Swift 4 中扩展所有数值类型(整数和浮点数)来扩展数值协议 斯威夫特 4 extension Numeric { func squared() -> Self { return self *

2022-05-14 23:46:34    分类:技术分享    swift   generics   protocols   self   protocol-extension

Self Joins in Mysql... How would I self join this table?

问题 我如何self join表格以显示由 Bl Blake or Jones管理的人员的姓名、员工编号、经理姓名? 我正在尝试按以下方式排列它: SELECT FROM INNER JOIN ON WHERE 我遇到的问题是到目前为止我已经很好地理解了MySQL ,而且我似乎无法掌握表连接本身的概念......任何帮助将不胜感激。 提前致谢 回答1 SELECT * FROM table1 AS t1 INNER JOIN table1 AS t2 ON t1.col_name=t2.col_name WHERE t1.col_name='xyz' 回答2 select t1.name,t1.employee_number,t1.manager_name from table t1 join table t2 where t1.manager_name = t2. manager_name and t2.manager_name in ('Blake','Jones');

2022-05-12 10:43:04    分类:技术分享    php   mysql   join   self

static:: vs. self:: - are there any downsides?

问题 在这个 StackOverflow 问题中,我了解到self::在static::是(在 PHP 中)的位置不支持继承。 在类中定义一堆常量时,如果要在子类中覆盖这些常量以更改默认“行为”,则必须使用static::以便在父类上引用该常量的方法,荣誉“覆盖”。 自从我提出那个原始问题以来的 2 年里,我开始广泛使用static:: ,以至于我很少使用self::因为self::似乎会限制使用常量的类的可扩展性,其中static::没有这个限制。 即使我目前不打算在子类中覆盖一个常量,我最终还是使用static:: ,以防万一 - 所以我以后不必做一堆搜索和替换,如果它原来我会想要扩展类并覆盖常量。 但是,在其他人的代码中,我很少看到使用static:: 。 直到 2012 年,我什至都不知道它的存在。 那么为什么理所当然地使用static::代替self::不是一种普遍的做法呢? 那么,我的问题是:使用static::来引用类常量,而不是self::有什么明显的缺点吗? 我在这里使用粗暴的反模式是否有罪? 回答1 实际上,这仅取决于您需要的用途。 如果您需要访问调用它的类的常量,请使用self 。 如果您需要后期静态绑定,请使用static 。 从性能的角度来看, self和static是相当的。 另请注意,广泛使用静态组合来覆盖/继承并不是一个好主意。 为了直接回答您的问题

2022-05-09 12:13:19    分类:技术分享    php   static   constants   self   late-static-binding

before_create still saves

问题 在一切之前,我要感谢您的帮助 我有一个这样的模型: attr_protected nil belongs_to :product belongs_to :user before_create :add_ammount def carted_product_price(ammount, price) ammount * price end def add_ammount carted_product = CartedProduct.where(:product_id => self.product_id, :user_id => self.user_id) if carted_product carted_product.first.ammount += self.ammount carted_product.first.update_attributes(:ammount => carted_product.first.ammount) else self.save end end 它将采购订单保存在名为 Carted_Products 的表中,该表连接到 belogings 中的用户和产品 问题是,当执行之前创建时,我希望它更新表中的记录,如果记录已经存在,则添加控制器传递的数量,如果不存在,则创建一个,就iv完成而言,它会更新数量,但仍然使用按顺序传递的参数创建一个新的

2022-05-03 22:45:03    分类:技术分享    ruby-on-rails   self   update-attributes

Self Joins in Mysql... How would I self join this table?

How would I self join a table to show the Name, employee number, manager's name of those who are managed by either Blake or Jones? I'm trying to line it up in the following manner: SELECT FROM INNER JOIN ON WHERE The problem I am having is I have understood MySQL very well up until now, and I cannot seem to grasp the concept of the table joining itself.... any help would be appreciated. Thanks in advance

2022-04-30 12:41:30    分类:问答    php   mysql   join   self

python global name 'self' is not defined

Just started learning python and I am sure its a stupid question but I am trying something like this: def setavalue(self): self.myname = "harry" def printaname(): print "Name", self.myname def main(): printname() if __name__ == "__main__": main() The error I am getting is: NameError: global name 'self' is not defined I saw this way of using the self statement to reference variables of different methods in some code I read that is working fine. Thanks for the help

2022-04-30 03:57:02    分类:问答    python   variables   self

del self vs self.__del__() - and what is the proper way to cleanup in python?

问题 我有一个包含类的 python 脚本。 这个类有一个用于通常清理的__del__方法。 如果我在脚本正常执行期间删除了类,我希望进行一些清理。 我还有一个信号处理程序,它会进行一些不寻常的清理。 如果脚本在收到信号时正在执行其他操作,则需要进行一些额外的清理,然后进行常规清理。 我注意到做del self和self.__del__()是有区别的。 即self.__del__()调用del方法两次。 你能解释一下为什么会这样吗? 这是一个用于说明问题的模型脚本: import sys import signal from optparse import OptionParser class TestClass(object): def __init__(self, del_self): self.del_self = del_self print "__init__ with del_self = %s" % self.del_self def __del__(self): print "Now performing usual cleanup." def signal_handler(self, arg_1, arg_2): print "Received signal. Now performing unusual cleanup." # Unusual cleanup

2022-04-29 17:11:05    分类:技术分享    python   python-2.7   python-3.x   self   code-cleanup

How do I avoid 'missing arguments' in instances and classes

I am currently writing a for school that should have 3 classes: Student, Teacher, School. (all of them in their own .py document). The class "Teacher" holds a List of Name, subjects and Age The class ''Student'' holds a List of Name, classes and student_IDs The class "School'' is the class that brings them both together, it contains them. Now if I call the instance method Student.print_list() it tells me I am lacking one Parameter. What does the self do, why is it mandatory, how do I avoid having to enter parameters from other classes? My second but same problem is when I want to print out one

2022-04-29 03:27:58    分类:问答    python   python-3.x   methods   instance   self

static:: vs. self:: - are there any downsides?

In this StackOverflow question I learned that self:: was not inheritance-aware where static:: was (in PHP). When it comes to defining a bunch of constants within a class, if you want to override those constants in a subclass to change default "behaviours", it becomes necessary to use static:: so that a method on the parent class that references the constant, honours the "override". In the 2 years since I asked that original question, I have started using static:: extensively, to the point that I rarely use self:: since self:: would appear to limit the extensibility of a class that uses

2022-04-27 06:10:39    分类:问答    php   static   constants   self   late-static-binding