Java learning summary: 10

Overwrite

When a subclass defines an attribute or method, it may happen that the defined attribute or method has the same name as the parent class. Such an operation is called overwriting.

Method override

When the subclass defines the method name, return value type, parameter type, and number of methods that are exactly the same as the parent class, it is called method override.

class A1{
    public void fun(){  //在父类定义的方法
        System.out.println("A类中的fun()方法");
    }
}
class B1 extends A1{    //定义子类,此时没有覆写任何方法
    public void fun(){  //此处为覆写
        System.out.println("B类中的fun()方法");
    }
}
class C1 extends A1{
    public void fun(){  //此处为覆写
        System.out.println("C类中的fun()方法");
    }
}
public class Test1_1_3_5 {
    public static void main(String args[]){
        B1 b=new B1();  //实例化子类对象
        b.fun();    //调用fun()方法,此时方法被覆写,所以调用被覆写过的方法
        C1 c=new C1();
        c.fun();
    }
}
//结果:
//B类中的fun()方法
//C类中的fun()方法

Principle: If the subclass overwrites the object and instantiates the subclass object, the overridden method must be called.
In the process of overwriting, permission must also be considered, that is , the method overwritten by the subclass cannot have stricter access permissions than the parent class.

Example:

class A2{
    public void fun(){
        this.print();   //调用print()方法
    }
    private void print(){   //此处为private权限,无法覆写
        System.out.println("666");
    }
}
class B2 extends A2{
    public void print(){    //不能覆写print()方法
        System.out.println("2333");
    }
}
public class Test1_1_3_6 {
    public static void main(String args[]){
        B2 b=new B2();  //实例化子类对象
        b.fun();    //调用父类继承来的fun()方法
    }
}
//结果:
//666

In order to be able to explicitly call the overridden method in the parent class, you can use the super. Method () to access.
Use super. Method () to access the method in the parent class:

class A2{
    public void print(){
        System.out.println("666");
    }
}
class B2 extends A2{
    public void print(){    //覆写的是print()方法
        super.print();      //访问父类中的print()方法
        System.out.println("2333");
    }
}
public class Test1_1_3_6 {
    public static void main(String args[]){
        B2 b=new B2();      //实例化子类对象
        b.print();
    }
}
//结果:
//666
//2333

Coverage of attributes

If the subclass defines exactly the same attribute name as the parent class, it is called an attribute override.

class A3{
    String info="Hello";    //定义属性,暂不封装
}
class B3 extends A3{
    int info=100;   //名称相同,发生属性覆盖
    public void print(){
        System.out.println(super.info);
        System.out.println(this.info);
    }
}
public class Test1_1_3_6 {
    public static void main(String args[]){
        B3 b=new B3();  //实例化子类对象
        b.print();
    }
}
//结果:
//Hello
//100

The difference between this and super

Insert picture description here

Published 49 original articles · praised 25 · visits 1539

Guess you like

Origin blog.csdn.net/weixin_45784666/article/details/104268793