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

What are the advantages of Java polymorphism

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

Share

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

This article introduces the knowledge of "what are the advantages of Java polymorphism". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Upward transformation.

Inheritance has been mentioned before. The parent class reference points to the subclass object. References in this case can only call subclass overridden methods, not subclass-specific methods. Automatic conversion

Public class Main {public static void main (String [] args) {Father f = new Son (); f.money ();}} class Father {void money () {System.out.println ("parent money") }} class Son extends Father {void money () {System.out.println ("money for subclasses");} void SonPlay () {System.out.println ("Dad, can I play games?");}}

What if I want the reference zone to invoke subclass-specific methods (not override overrides)? Will report an error (the following downward transformation is to solve this problem)

Here's an example.

Output "HelloWorld" in different languages

(1) the parent class is normal.

Public class Language {/ / programming language class void HelloWorld () {/ / output HelloWorld}} public class CLanguage extends Language {/ / C language class inherits programming language class void HelloWorld () {/ / output HelloWorld System.out.println ("printf (" HelloWorld! ");") }} public class JavaLanguage extends Language {/ / Java inherits programming language class void HelloWorld () {System.out.println ("System.out.println (" HelloWorld! ")");}}

Provide a function call

Public class Test {public static void main (String [] args) {/ / inputHelloWorld (new CLanguage ()); / / inputHelloWorld (new JavaLanguage ()); / / or Language language; language = new CLanguage (); inputHelloWorld (language) Language = new JavaLanguage (); inputHelloWorld (language);} public static void inputHelloWorld (Language language) {language.HelloWorld ();}}

Without changing the parameter of the function (parent class), the change of the parameter (subclass) will not cause the program to report an error. In other words, all I need to care about is that the parameter of the function is the class (itself or subclass object) associated with the Language class, regardless of which subclass object the argument passed in is.

What if the parent class is an interface or an abstract class? Also OK

(2) the parent class is the interface

If the parent class is really to provide a standard, or if the parent class does not need to implement content, you can consider turning the parent class into an interface, and the interface also supports polymorphism.

Public class Test {public static void main (String [] args) {/ / inputHelloWorld (new CLanguage ()); / / inputHelloWorld (new JavaLanguage ()); / / or Language language; language = new CLanguage (); inputHelloWorld (language); language = new JavaLanguage () InputHelloWorld (language);} public static void inputHelloWorld (Language language) {language.HelloWorld ();}} interface Language {/ / programming language class public void HelloWorld () / / output HelloWorld} class CLanguage implements Language {/ / C language class inherit programming language class public void HelloWorld () {/ / output HelloWorld System.out.println ("printf (" HelloWorld! "););}} class JavaLanguage implements Language {public void HelloWorld () {System.out.println (" System.out.println ("HelloWorld!") ") }}

Of course, an abstract class as a parent class can also implement polymorphism.

two。 Turn down.

The reference of the parent class to the subclass object is strongly converted to a different subclass object. Objects after conversion can call subclass-specific methods

(solve the problem that the unique method of the calling subclass will report an error)

Public class Main {public static void main (String [] args) {Father f = new Son (); / / f.money (); Son s = (Son) f; s.SonPlay () }} class Father {void money () {System.out.println ("money of parent class");}} class Son extends Father {void money () {System.out.println ("money of subclass") } void SonPlay () {System.out.println ("Dad, can I play games?");}}

In the above programming language output HelloWorld example, if I ask the inputLanguage method to judge the subclass objects in the passed arguments, and if the incoming objects are C language classes, output the new unique methods, and the Java classes are the same.

This is a bit like the simple factory pattern (what the argument is, I new what). Here is what the argument is. After I force it, I call the corresponding method.

Public class CLanguage implements Language {/ / C language class inherits programming language class public void HelloWorld () {/ / output HelloWorld System.out.println ("printf (" HelloWorld! "););} public void CReadMe () {/ / add C language subclass specific method System.out.println (" I am C language ") }} public class JavaLanguage implements Language {public void HelloWorld () {System.out.println ("System.out.println (" HelloWorld! ")");} public void JavaReadMe () {/ / add Java subclass specific method System.out.println ("I am Java");}}

Modify inputHelloWorld function

Public class Test {public static void main (String [] args) {/ / inputHelloWorld (new CLanguage ()); / / inputHelloWorld (new JavaLanguage ()); / / or Language language; language = new CLanguage (); inputHelloWorld (language); language = new JavaLanguage () InputHelloWorld (language);} public static void inputHelloWorld (Language language) {if (language instanceof CLanguage) {CLanguage clanguage = (CLanguage) language; clanguage.CReadMe () } else if (language instanceof JavaLanguage) {JavaLanguage javalanguage = (JavaLanguage) language; javalanguage.JavaReadMe ();} language.HelloWorld ();}} "what are the advantages of Java polymorphism"? thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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