当前位置:网站首页>Overloading and overriding

Overloading and overriding

2022-06-26 05:43:00 Wang Xiaoya

Method overloading

It is simply a function or method Have the same name , however The parameter list is not the same The circumstances of , In this way Different parameters with the same name Between functions or methods of , Call each other overloaded function perhaps Overloading methods


Method overloading refers to the relationship between multiple methods defined in the same class , Multiple methods that satisfy the following conditions constitute overloading each other .
1) Multiple methods in the same class
2) Multiple methods have the same method name
3) Multiple methods have different parameters , Different in type or quantity

Which of the following programs is related to show() Method overloading ()?**A、C、D**

class Demo{

  void show(int a,int b,float c){}

}

A.void show(int a,float c,int b){}  //√  Parameter order is different 
B.void show(int a,int b,float c){}  //×  Exactly the same 
C.int show(int a,float c,int b){return a;}  //√  The same name , Parameter order is different 
D.int show(int a,float c){return a    //√  Different parameters 

Method overload features :

* Overloading only corresponds to the definition of the method , It has nothing to do with the method call , The call mode refers to the standard format
* Overloading only identifies the names and parameters of methods in the same class , Independent of the return value , In other words, the return value cannot be used to determine whether two methods constitute overloads

Method rewriting


* 1、 Method rewriting concept

  * As like as two peas, the subclass has the same method declaration as the parent class ( The method name is the same , The parameter list must be the same )

* 2、 Application scenarios of method rewriting

  * When a child class needs the function of a parent class , When the function subject subclass has its own content , You can override methods in the parent class , such , That is, following the function of the parent class , It also defines the specific content of the subclass

Specific examples :  Cats and animals

Cats are animals , Inherited the animal class , Have the same method “eat”, And rewritten eat, The final output is also rewritten “ Cats eat fish ”

Can combine http://t.csdn.cn/fqWv6 understand

package com.object_02;

public class Animal9 {
        public int age = 40;

        public void eat() {
            System.out.println(" Animals eat ");
        }
    }


package com.object_02;

import com.object_02.Animal9;

public class Cat9 extends Animal9 {
    public int age = 20;
    public int weight = 10;


    @Override
    public void eat() {
        System.out.println(" Cats eat fish ");
    }

    public void playGame() {
        System.out.println(" Cat hide and seek ");
    }
}

package com.object_02;

public class AnimalDemo9 {
    public static void main(String[] args) {
        // There are parent class references to child class objects 
        Animal9 a = new Cat9();

        System.out.println(a.age);
//        System.out.println(a.weight);
//  Because the member variable ​ Compile and run to see the parent class , There is no , therefore weight Report errors 
//  Run out age Is also a parent class 40

        a.eat();
//        a.playGame();
// Because the member method compilation looks at the parent class , Parent class Animal There are only eat Method , therefore playGame Method error reporting 
// Because the subclass can be seen during operation , therefore eat The result of method running is the subclass overridden " Cats eat fish "
    }
}

3、Override annotation

 

* To detect the current method , Whether it's an overriding method , Play a 【 check 】 The role of 2.7 Method rewriting considerations ( master )

* Method rewriting considerations

1. Private methods cannot be overridden ( Private members of a parent class cannot be inherited from a child class )
2. Subclass method access cannot be lower (public > Default > private )
 

The difference between overloading and overriding is as follows ( expand )

1. Different definitions --- Overloading is to define the same method name , Different parameters ; Overriding is a method that a subclass overrides a parent class .

2. The range is different --- Overloading is in a class , Overriding is between a subclass and its parent .

3. Polymorphic difference --- Overloading is compile time polymorphism , Run time polymorphism is overridden .

4. Back to different --- Overload does not require a return type , Rewriting requires a return type , There are compatible return types .

5. Different parameters --- Number of overloaded parameters 、 Parameter type 、 The order of parameters can be different , The parameters of overriding parent-child methods must be the same .

6. Embellishment is different --- Overloading has no special requirements for access decoration , The restriction of overriding the access modifier must be greater than that of the overridden method .
 

原网站

版权声明
本文为[Wang Xiaoya]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260540482387.html