-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSon.java
More file actions
60 lines (47 loc) · 1.24 KB
/
Son.java
File metadata and controls
60 lines (47 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package shixunTest;
/*
* 方法重写(运行时多态)
* 在子类继承父类中,方法名相同,返回类型相同,参数列表相同
* 子类重写方法异常的声明要<=父类
* 子类重写方法修饰权限>=父类
*
* 方法重载(编译时多态)
* 在同一个类中,方法名相同,参数列表不同
*
* 静态对象(类方法)
*
*
* 当子类和父类有同名变量时,父类的同名变量将不会给子类继承(实际开发,不要有同名变量)
* 所以:子类继承了父类的所有变量及方法是不准确的
* */
public class Son extends Base{
public Son(){
System.out.println("son constract");
a=2;
b=2;
}
public void addA(){
System.out.println("son addA");
a+=3;
}
public static void addB(){
System.out.println("son addB");
b+=4;
}
public void addB(int a){}
public static void main(String[] args) {
Son son=new Son();
Base base=son;
//调用子类的addA方法
//运行时多态
//base.addA();
//调用父类的addB方法,因为addB方法是静态的
//base.addB();
son.addA();
son.addB();
//System.out.println(base.a);
//System.out.println(base.b);
System.out.println(son.a);
System.out.println(son.b);
}
}