在看DatabaseLibrary时,突然发现Query类里使用到了ConnectionManager类里面的变量_dbconnection,很不能理解,Query类里没有对ConnectionManager类进行实例化,也没有初始化该变量,怎么就能使用这个变量了。首先咨询了一下大佬,大佬回复说向上找。。。一脸懵逼,于是决定自己探索。
先说结论,在python这种语言中,父类的终旨就是服务于子类,只要子类调用的时候拥有这个方法或者变量,父类中就可以写这个方法或变量,不会像java编译就报错,因为java(应该是所有非脚本语言都有的规则),要使用先定义。而python根本不关注这个,类里面先写起来,你后面使用报错了,不关哥什么事。
举例:
class A(object):
def __init__(self):
self.currency = 1
def a(self):
print("this is class A,currency:", self.currency)
class B(object):
def b(self):
print("this is class B,currency:", self.currency)
a = self.currency
import example.A
import example.B
class C(example.A.A, example.B.B):
def c(self):
c = self.currency
print("this is class C,currency:", self.currency)
from example import A
from example import B
from example import C
a = A.A()
b = B.B()
c = C.C()
a.a()
c.b()
c.c()
理解我这个例子,这个知识点应该也就掌握的差不多了。我真想吐槽哦,python这种方式,无可避免的带来一个缺点,类的通用性下降,我写的类里面完全可能包含很多完全没定义的方法或变量,这个类指定给某个子类使用的。那么这个类还能给别人用吗?
print_r('点个赞吧');
var_dump('点个赞吧');
NSLog(@"点个赞吧!")
System.out.println("点个赞吧!");
console.log("点个赞吧!");
print("点个赞吧!");
printf("点个赞吧!\n");
cout << "点个赞吧!" << endl;
Console.WriteLine("点个赞吧!");
fmt.Println("点个赞吧!")
Response.Write("点个赞吧");
alert(’点个赞吧’)