modifer
modifer的一些限制,很好记的是他们是triangle
and then he talked about the information hiding
1:Information hiding
2.Abstraction
method:
Accessor(getter):Public String get_name()
Mutator(setter):Public String set_name(String name)
static
Static method:
你可以直接用static的method,而如果没有static的method你可以用instance。或者是完全没有instance的你可以随便乱用。
你的static\要用class里存在的method | 这个class里有static method也有instance method | class里只有instance method | class里只有static method |
建立Instance | 都能用 | 都能用 | 不能建立instance,所以你错啦~ |
不建立instance | 只能用static | 都不能用 | 都能用 |
而不用先声明 Math math = new math();你可以直接用math.max(a,b);但是如果是static class比如说math:Class c = new Class();要用有instance的class记得要
Static variable:
就比如说
也就是说static方法访问static变量不需要new 出来,可以直接用class.a搞出来。
package
你用用scanner就懂了
inheritance
怎么操作
用subclass extends class
method相关
所有的method都能被调用
variable相关
private的variable不能被调用,其他都行
爹的consructor不能用儿子的方法
constructors相关
1.一旦调用自己的constructors祖宗的无参constructors全部被调出来
也就是你建立了一个狗,就等于建立了一个动物,等于建立了一个生命
2.如果你自己没有constructor,爹必须要有一个constructor不然会跑不出来
3.儿子的constructor,不管有参无参,必须要对爹的constructor进行一个显性调用。如果爹有一个无参constructor,那么显性调用可以被省略。
overiding
叫做名字相同并且参数相同的method,爹和儿子都有,但是儿子在用这个function的时候,accessibility要不然相同,要不然变大。这里p2是爹,p1是儿子。
把儿子的overiding function 变成private之后就报错啦~
而且你会发现你调用s.getInfo(调用的是儿子的constructor)会出现上图最后一条的结果,是student儿子这个class里的结果
那如何去调用s的时候出现的是person里的getInfo里的结果呢?请用super
super和this
这样我们在儿子的function里用了一条super,这样就可以调用儿子的constructor而输出爹的overriding的结果了!