- class Fu {
- public int x = 10;
- public Fu(){
- }
- public Fu(int x) {
- System.out.println("Fu……" + x);
- }
- public void show() {
- System.out.println("Fu……show");
- }
- }
- class Zi extends Fu {
- public int x = 20;
- public Zi() {
- super(10); // Fu……20,创建对象的时候,会先去对父类进行初始化。
- System.out.println("Zi……x" +x); // 20
- System.out.println("Zi……x" + this.x + " -- Fu……x" + super.x);
- this.show(); // Zi……show,如果子类没有同名的show,就会去调用父类的show()
- super.show(); // Fu……show
- System.out.println(this);// Zi@5a20d10a
- //System.our.println(super);// 报错
- }
- public void show() {
- System.out.println("Zi……show");
- }
- }
- class Demo {
- public static void main(String[] args) {
- Zi zi = new Zi();
- }
- }
复制代码
|