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

How to implement Polymorphism in Java

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you how to achieve polymorphism in Java, I hope you will learn something after reading this article, let's discuss it together!

/ * topic: people can (children, students, teachers) eat fruit (apples, grapes, mangoes, pineapples) idea: 1. Abstract classes: Person {Baby, Student}, Fruit {apple (Apple), grape (Grape), mango (Mango) 2. Find out the class relationship: fruit-> people (children, students) are classified as people who only declare that they can eat fruit, but the specific executors of eating fruit are (children, students) (apples, grapes). Mango (mango) is a classification of fruits. Find out the attributes: child, student (name, fruit quote) 4. Find a way: kid. Students all eat (eat) fruit subclass-> parent subclass-> parent class reference invokes the method function of subclass overwriting: decoupling is to reduce the coupling between classes to provide extensibility * / public class Polymorphism5 {/ / print human eating fruit information public void output (Person p) 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"); p.eat (apple); System.out.println (p.getName () + "eat" + p.getFruit (). GetName ()); p.eat (grape) System.out.println (p.getName () + "eat" + p.getFruit (). GetName ()); p.eat (mango); System.out.println (p.getName () + "eat" + p.getFruit (). GetName ()); p.eat (pineApple) System.out.println (p.getName () + "eat" + p.getFruit (). GetName ());} public static void main (String [] args) {Polymorphism5 test = new Polymorphism5 (); / / Li Ming eats fruit test.output (new Baby ("Li Ming")) / / Zhang Tao eats fruit test.output (new Student ("Zhang Tao")); / / teacher Guo eats fruit test.output (new Teacher ("teacher Guo"));}} abstract class Person {protected Fruit fruit; protected String name; public Person (String name) {this.name = name } public String getName () {return this.name;} public Fruit getFruit () {return this.fruit;} / / declares that a person is a public abstract void eat (Fruit fruit) who can eat fruit;} class Teacher extends Person {public Teacher (String name) {super (name) } / / eat fruit public void eat (Fruit fruit) {this.fruit = fruit;}} class Baby extends Person {public Baby (String name) {super (name);} / / eat fruit public void eat (Fruit fruit) {this.fruit = fruit }} class Student extends Person {public Student (String name) {super (name);} / / 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) }} after reading this article, I believe you have some understanding of "how to achieve polymorphism in Java". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

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