In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the Java object-oriented how to achieve polymorphism, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Reproduced at: http://www.verejava.com/?id=16992843565554
/ * topic: children eat (apples, grapes, mangoes) ideas: 1. Abstract classes: Baby, Apple, Grape 2. Find out the class relationship: Apple, grape-> child 3. Find out the attributes: child (name, apple quote, grape reference) apple (name) grape (name) 4. Find out how: children eat (eat) apples or grapes * / public class Polymorphism2 {public static void main (String [] args) {/ / instantiate an Baby Baby baby = new Baby ("Li Ming"); / / instantiate an apple and a grape Apple apple = new Apple ("Red Fuji") Grape grape = new Grape ("black grape"); Mango mango = new Mango ("big mango"); / / Li Ming eats apples and grapes baby.eat (apple); System.out.println (baby.getName ()) + "happy to say I ate" + baby.getApple (). GetName (); baby.eat (grape) System.out.println (baby.getName () + "happily say I ate" + baby.getGrape (). GetName ()); baby.eat (mango); System.out.println (baby.getName () + "happily say I ate" + baby.getMango (). GetName ());}} class Baby {private Apple apple; private Grape grape; private Mango mango Private String name; public Baby (String name) {this.name = name;} public String getName () {return this.name;} public Apple getApple () {return this.apple;} public Grape getGrape () {return this.grape } public Mango getMango () {return this.mango;} / / eat Apple public void eat (Apple apple) {this.apple = apple;} / / eat Grape public void eat (Grape grape) {this.grape = grape } / / eat public void eat (Mango mango) {this.mango = mango;}} class Apple {private String name; public Apple (String name) {this.name = name;} public String getName () {return this.name;}} class Grape {private String name Public Grape (String name) {this.name = name;} public String getName () {return this.name;}} class Mango {private String name; public Mango (String name) {this.name = name;} public String getName () {return this.name }} Thank you for reading this article carefully. I hope the article "how to achieve Polymorphism in Java object orientation" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.