You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
# 面向对象进阶
|
|
|
|
|
|
|
|
|
|
### this关键字
|
|
|
|
|
|
|
|
|
|
使用this修饰的变量,表示的是属性。没有的表示是形参
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
package com.thi;
|
|
|
|
|
|
|
|
|
|
public class per {
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
Person p1 = new Person();
|
|
|
|
|
p1.setAge(20);
|
|
|
|
|
System.out.println(p1.age);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Person{
|
|
|
|
|
String name;
|
|
|
|
|
int age;
|
|
|
|
|
|
|
|
|
|
public void setAge(int age){
|
|
|
|
|
this.age=age;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
命名一致是为了见文知意,加上this防止错误。如无this则age=age;全部指属性,没有形参了
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- this可以调用成员变量、方法、构造器
|
|
|
|
|
- 理解为当前对象(方法中)或者当前正在创建的对象(构造器中)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## 继承性
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
class Student extends Person{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
如此, Person中含有的属性和方法 Student 不定义也可以使用
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
java支持多层继承
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|