Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example usage of Java parameter overloading

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains the "example usage of Java parameter overloading". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the example usage of Java parameter overloading".

Package someTest;class SSSuperClass {} class SSSubClass extends SSSuperClass {} public class TestDuplicate {public void function (Object o) {/ / method 1 System.out.print ("Object\ n");} public void function (int [] array) {/ / method 2 System.out.print ("int [] array\ n") } public void function (SSSuperClass array) {/ / method 3 System.out.print ("SSSuperClass\ n");} public void function (SSSubClass array) {/ / method 4 System.out.print ("SSSubClass\ n") } / * * @ param args * / public static void main (String [] args) {new TestDuplicate () .function (new Object ()); / / call 1 new TestDuplicate () .function (new int [3]) / / call 2 new TestDuplicate () .function (new SSSubClass ()); / / call 3 new TestDuplicate () .function (new SSSuperClass ()); / / call 4 / * * everything seems to be in harmony. Parameter overloading allows the existence of a parent-subclass relationship. * when called, it will find the most accurate matching function to execute based on the actual parameters. * if we remove method 4 and call 4, we will find that method 3 is its best match, so call it. * if method 3 is removed, calling 4 will find that method 1 is the function that best matches its argument, so call it. * * / * but what if we pass in a null instead of an array or any object? * null will also find a function that matches exactly, but there will be a compilation error here. Because there are two inheritance * paths here, one is Object- > array [], and the other is Object- > SSSuperClass- > SSSubClass. * so I don't know which one is the most accurate match between method 2 and method 4. * so our method 3 and method 4 are not compatible and ambiguous under the following call. * * / new TestDuplicate () .function (null); / / compile error / / you can see that although null is an object, it is nothing, not even Object, but he will still look for the most matching parameter String s = null; String ss = "sss". If (s instanceof String) System.out.print ("s is String"); if (ss instanceof String) System.out.print ("ss is String"); if (null instanceof Object) System.out.print ("null is an Object");}}

Thank you for your reading. The above is the content of "instance usage of Java parameter overloading". After the study of this article, I believe you have a deeper understanding of the instance usage of Java parameter overloading, and the specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report