foo

python实现单例模式

三世轮回 提交于 2019-12-05 17:55:46
class SingletonType(type): def __init__(self,*args,**kwargs): super(SingletonType,self).__init__(*args,**kwargs) def __call__(cls, *args, **kwargs): # 这里的cls,即Foo类 print('cls',cls) obj = cls.__new__(cls,*args, **kwargs) cls.__init__(obj,*args, **kwargs) # Foo.__init__(obj) return obj class Foo(metaclass=SingletonType): # 指定创建Foo的type为SingletonType def __init__(self,name): self.name = name def __new__(cls, *args, **kwargs): return object.__new__(cls) obj = Foo('xx') 来源: https://www.cnblogs.com/zhaogang0104/p/11938683.html

继承与派生

ε祈祈猫儿з 提交于 2019-12-05 17:35:54
一、继承与派生 1 、继承介绍 2 、继承的作用 3、如何实现继承 3.1 如何查看父类 二、寻找继承关系 1、如何寻找继承关系: 2、继承有什么用处 三、在继承背景下对象属性的查找顺序 四、派生 1、什么是派生 2、派生后继承关系查找验证 五、子类继承父类 派生出自己的属性和方法,并且重用父类的属性与方法 1、两种解决办法 1.1 直接引用父类的__init__ 1.2 使用super 六、了解 1、经典类与新式类 1.1 新式类 1.2 经典类 2、super严格遵循mro继承顺序 3、钻石继承(菱形继承) TOC 一、继承与派生 1 、继承介绍 继承是一种新建类的方式,新建的类称之为子类或派生类,继承的父类称之为 基类 或 超类 在python中,一个子类可以继承多个父类 在其他语言中,一个子类只能继承一个父类 2 、继承的作用 减少代码的冗余 3、如何实现继承 先确认谁是子类,谁是父类 在定义子类时, 子类名(父类名) # 父类 class Father1: x = 1 pass class Father2: pass # 子类 class Sub(Father1, Father2, Father3): pass 3.1 如何查看父类 子类.__bases__ 查看父类 print(Sub.__bases__) # 查看父类 <class '__main__

leetCode例题引出的多线程CountDownLatch和CyclicBarrier使用例题

╄→尐↘猪︶ㄣ 提交于 2019-12-05 07:16:07
先介绍下 CountDownLatch 和CyclicBarrier; CountDownLatch : new CountDownLatch( n ), 初始化时会赋值,并且不可以重新赋值。 countDown() ,值减1,操作,当值为0时,会跳过所有的阻塞方法await(); await(),阻塞方法,一直阻塞到值为0; CyclicBarrier: new CyclicBarrier(m ),m代表着,每次阻拦的阈值,当阻拦的await方法的个数等于m时,所有的阻塞方法则会跳过。 然后开始阻塞下一批线程,每批数量为m。 await(),阻塞方法,每次阻塞的格式为m。 题目1115: 我们提供一个类: class FooBar { public void foo() { for (int i = 0; i < n; i++) { print("foo"); } } public void bar() { for (int i = 0; i < n; i++) { print("bar"); } } } 两个不同的线程将会共用一个 FooBar 实例。其中一个线程将会调用 foo() 方法,另一个线程将会调用 bar() 方法。 请设计修改程序,以确保 "foobar" 被输出 n 次。 线上最受赞答案: 1 import java.util.concurrent

递归算法之斐波那契数列

时间秒杀一切 提交于 2019-12-05 02:02:56
使用递归计算斐波那契数列,例如下面计算第30个数(1,1,2,3,5,8,13。。。) 1 public class MainClass 2 { 3 public static void Main() 4 { 5 Console.WriteLine(Foo(30)); 6 } 7 8 public static int Foo(int i) 9 { 10 if (i <= 0) 11 return 0; 12 else if(i > 0 && i <= 2) 13 return 1; 14 else return Foo(i -1) + Foo(i - 2); 15 } 16 } 来源: https://www.cnblogs.com/MirZhai/p/11896702.html

Go-Type

被刻印的时光 ゝ 提交于 2019-12-05 01:36:50
在谈论 struct 和 interface 已经用到了type这个关键字。 另外,Go的type另外一种常用功能,是类似于C/C++的typedef。在Go的package中,这种用法非常常见。 A type declaration defines a new named type that has the same underlying type as an existing type. The named type provides a way to separate different and perhaps incompatible uses of the underlying type so that they can’t be mixed unintentionally. type name underlying-type 示例: package main import ( "fmt" ) type Hello interface { out() } type MyInt int func (myInt *MyInt) out() { fmt.Println("MyInt.out():", *myInt) } func foo(i MyInt) { fmt.Println("foo():", i) } func main() { var i MyInt i = 5 i

golang积累-WaitGroup包装

痴心易碎 提交于 2019-12-04 23:46:43
golang的协程使用非常方便,但为了确保协程能够在主程序退出之前确保执行,会采用各种手段。 笨点的time大法: func foo(){ //do something } func main(){ go foo() //do anotherthing time.Sleep(time.Second) //还真不知道foo运行多久,只能靠猜 } 稍微好点的通道阻塞: var signalchan= make ( chan int ,0 ) //阻塞通道 func foo(){ //do something signalchan <-1 //传递一个完成信号 } func main(){ go foo() //do anotherthing <-signalchan } 再聪明点的,使用sync.WaitGroup 代码可以这样写: func foo(){ //do something } func main(){ var w sync.WaitGroup w.Add (1 ) go func (){ foo() w.Done() } //do anotherthing w.Wait() 虽然没有了全局变量,但感觉代码多了,感觉不清爽,能够优化呢。 WaitGroup Wrapper封装 代码参考如下: package waitgroup import ( "sync" ) type

PHP对象继承

≡放荡痞女 提交于 2019-12-04 10:51:57
PHP对象继承 <?php class foo { public function printItem($string) { echo 'Foo: ' . $string . PHP_EOL; } public function printPHP() { echo 'PHP is great.' . PHP_EOL; } } class bar extends foo { public function printItem($string) { echo 'Bar: ' . $string . PHP_EOL; } } $foo = new foo(); $bar = new bar(); $foo->printItem('baz'); // Output: 'Foo: baz' $foo->printPHP(); // Output: 'PHP is great' $bar->printItem('baz'); // Output: 'Bar: baz' $bar->printPHP(); // Output: 'PHP is great' ?> 来源: https://www.cnblogs.com/newmiracle/p/11856253.html

__doc__

白昼怎懂夜的黑 提交于 2019-12-04 08:57:58
__doc__ 一、__doc__ 返回类的注释信息 class Foo: '我是描述信息' pass print(Foo.__doc__) 我是描述信息 该属性无法被继承 class Foo: '我是描述信息' pass class Bar(Foo): pass print(Bar.__doc__) #该属性无法继承给子类 None 来源: https://www.cnblogs.com/Dr-wei/p/11851373.html

显式关键字是什么意思?

。_饼干妹妹 提交于 2019-12-03 18:17:29
explicit 关键字在C ++中是什么意思? #1楼 允许编译器进行一次隐式转换,以将参数解析为函数。 这意味着编译器可以使用可通过 单个参数 调用的构造函数从一种类型转换为另一种类型,以便获得参数的正确类型。 这是带有可用于隐式转换的构造函数的示例类: class Foo { public: // single parameter constructor, can be used as an implicit conversion Foo (int foo) : m_foo (foo) { } int GetFoo () { return m_foo; } private: int m_foo; }; 这是一个带有 Foo 对象的简单函数: void DoBar (Foo foo) { int i = foo.GetFoo (); } 这就是 DoBar 函数的调用位置。 int main () { DoBar (42); } 该参数不是 Foo 对象,而是 int 。 但是, Foo 有一个采用 int 构造函数,因此可以使用该构造函数将参数转换为正确的类型。 允许编译器对每个参数执行一次此操作。 在 explicit 关键字前面加上构造函数,可以防止编译器将该构造函数用于隐式转换。 将其添加到上述类中将在函数调用 DoBar (42) 创建编译器错误。 现在必须使用

php中const与define的使用区别 详解

坚强是说给别人听的谎言 提交于 2019-12-03 08:41:00
1、const用于类成员变量定义,一旦定义且不能改变其值。define定义全局常量,在任何地方都可以访问。 2、define不能在类中定义而const可以。 3、const不能在条件语句中定义常量 if (...) { const FOO = 'BAR'; // invalid } but if (...) { define('FOO', 'BAR'); // valid } 4、const采用一个普通的常量名称,define可以采用表达式作为名称。 const FOO = 'BAR'; for ($i = 0; $i < 32; ++$i) { define('BIT_' . $i, 1 << $i); } 5、const只能接受静态的标量,而define可以采用任何表达式。 const BIT_5 = 1 << 5; // invalid but define('BIT_5', 1 << 5); // valid 6、const 总是大小写敏感,然而define()可以通过第三个参数来定义大小写不敏感的常量 define('FOO', 'BAR', true); www.2cto.com echo FOO; // BAR echo foo; // BAR 总结: 使用const简单易读,它本身是一个语言结构,而define是一个方法,用const定义在编译时比define快很多