当前位置:网站首页>The difference between overloading and overriding

The difference between overloading and overriding

2022-06-22 00:16:00 Silent tubule

heavy load

The same method name , The number of formal parameters is different perhaps Parameter order is different perhaps Different parameter types are called method overloading

class animal{
    
    function cat(move string){
    //A 
        console.log(" Cats like "+move);
    }
    function cat(age int){
    //A 
        console.log(" Cat "+age+" year ");
    }
    function cat(move string,age int){
    //A 
        console.log(" Cats like "+move+", meanwhile "+age+" year ");
    }
}

rewrite

Rewrite the premise method : There must be inheritance
The method name is the same as the formal parameter list

class animal{
    
    function cat(move string){
     
        console.log(" Cats like "+move+", meanwhile 13 year ");
    }
}
class cat extends animal{
    
	function cat(move string){
     
		console.log(" Cats like "+move+", meanwhile 16 year ");
	}
}

 Insert picture description here

Polymorphism is the same interface , Use different instances , Perform different operations

summary :

(1) Method overloading is : Multiple methods with the same name are defined in a class , The number of parameters is different or the number is the same, but the type and order are different , It's called method overloading (Overloading).
(2) Method override is : The name of the method that exists in the subclass is the same as that of the parent class , And the number of parameters is the same as the type , The return value is the same way , It's called rewriting (Overriding).
(3) Method overloading is a polymorphic representation of a class , Method rewriting is a kind of polymorphism between subclass and parent class .

Refer to the website :https://www.runoob.com/java/java-override-overload.html

Welcome to join the front-end full stack development exchange circle to blow water, chat, learn and exchange qq Group :837051545

Personal website : Silent tubule
If there is a mistake , Your advice are most welcome .
If it helps you , Give me a compliment .

原网站

版权声明
本文为[Silent tubule]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206212226029428.html