Examples of the usage of Java object-oriented Polymorphism
Editor to share with you an example of the usage of Java object-oriented polymorphism, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
/ * topic: (children, students) eat fruits (apples, grapes, mangoes, pineapples) ideas: 1. Abstract classes: {Baby, Student}, Fruit {apple (Apple), grape (Grape), mango (Mango)} 2. Find out the class relationship: fruit-> (children, students) (apples, grapes, mangoes) is the classification of fruits 3. Find out the attributes: child, student (name, fruit quote) 4. Find a way: children, students all eat (eat) fruit subclass-> parent class conversion: decoupling is to reduce the coupling between classes to provide extensibility * / public class Polymorphism4 {public static void main (String [] args) {/ / instantiate a Baby Baby baby = new Baby ("Li Ming") / / instantiate a student Student stu = new Student ("Zhang Tao"); / / instantiate an apple and a grape Apple apple = new Apple ("Red Fuji"); Grape grape = new Grape ("Black Grape"); Mango mango = new Mango ("Big Mango") PineApple pineApple = new PineApple ("pineapple"); / / Li Ming eats fruit baby.eat (apple); System.out.println (baby.getName () + "ate" + baby.getFruit (). GetName ()); baby.eat (grape) System.out.println (baby.getName () + "eat" + baby.getFruit (). GetName ()); baby.eat (mango); System.out.println (baby.getName () + "eat" + baby.getFruit (). GetName ()); baby.eat (pineApple) System.out.println (baby.getName () + "ate" + baby.getFruit (). GetName ()); / Zhang Tao eats fruit stu.eat (apple); System.out.println (stu.getName () + "ate" + stu.getFruit (). GetName ()); stu.eat (grape) System.out.println (stu.getName () + "eat" + stu.getFruit (). GetName ()); stu.eat (mango); System.out.println (stu.getName () + "eat" + stu.getFruit (). GetName ()); stu.eat (pineApple) System.out.println (stu.getName () + "eat" + stu.getFruit (). GetName ());} class Baby {private Fruit fruit; private String name; public Baby (String name) {this.name = name;} public String getName () {return this.name } public Fruit getFruit () {return this.fruit;} / / eat fruit public void eat (Fruit fruit) {this.fruit = fruit;}} class Student {private Fruit fruit; private String name; public Student (String name) {this.name = name } public String getName () {return this.name;} public Fruit getFruit () {return this.fruit;} / / eat fruit public void eat (Fruit fruit) {this.fruit = fruit;}} class Fruit {private String name Public Fruit (String name) {this.name = name;} public String getName () {return this.name;}} class PineApple extends Fruit {public PineApple (String name) {super (name);}} class Apple extends Fruit {public Apple (String name) {super (name) }} class Grape extends Fruit {public Grape (String name) {super (name);}} class Mango extends Fruit {public Mango (String name) {super (name);}} these are all the contents of the article "examples of the use of Java object-oriented Polymorphism". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!