And the main one is, I get to just decide, how do I want to use this, and call it.
但是这真的会有一些好处,最重要的一点是,我需要决定,我要如何来使用这个方法,以及如何来调用它。
I'd really like to have that modularity that says, I'm only going to get access to the values, not by calling their names, but by calling some specific method to get access to their names.
这样我们就丢掉了代码的模块化,我很希望代码有这样的模块化:,我访问变量的值,不是通过直接调用它们的名字,而是通过调用某个可以,访问这些变量名的方法来访问。
Notice the form of it, it's just the name of the class followed by open paren, close paren, treating it like a function.
我该怎么来使用类呢?,好,我可以像调用方法,一样调用类,你可以在下面看到,创建一个名为。
init Init creates a pointer to the instance.
然后这个方法调用了。
Here's the way I would call it.
下面是我调用它的方法。
So you can actually say, how do I know which methods are associated with the class? For that, we can call dir. And what it does, is it gives me back a listing of all the things, dir all the methods, that are associated with it.
我们开始建立类了,我们得到了这些方法,因此实际上你可以说,我怎么知道,哪些方法是关联与这些类的?,为了解决这个问题,我们可以调用,这个方法的作用。
Notice in the class definition here, is there, this is the first thing that's got called, and I just called with the value for x and the value for y, and it went off and did something for me.
就在这里,这是第一个被调用的东西,我用x的值和y的值调用了它,然后这个方法继续进行,并为了做了一些操作。
So another way saying it again is, when I call the class definition, by default I'm going to look to see is there an init method there, and if there is, I'm going to use it.
当我调用类的定义,默认的是语言要去查看,是不是有个init的方法,如果有的话,程序就会调用它,按照惯例。
It's still calling cmp, but it's knowing that cmp is just reversing the order of the arguments.
下一次我会带上正确的眼镜的,这还是会去调用cmp方法的,但是我们知道。
STUDENT: PROFESSOR: Right, cmp other, so how would I call this?
学生:不可闻的声音:,教授:对,cmp另外一个对象,那么我怎么来调用这个方法呢?
应用推荐