In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "Java abstract definition and corresponding code example analysis", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Java abstract definition and corresponding code example analysis" article.
I. Overview
In the concept of OOP, all objects are described by classes; but not all classes are used to describe objects. A class is called an abstract class if it does not contain enough information to describe a concrete object.
Abstract class: (1) an abstract class cannot instantiate an object, and if an object is instantiated, the compiler cannot pass. Only non-abstract subclasses of an abstract class can create objects (2) that can contain member variables, member methods, constructors, and so on. (3) Constructors and class methods (methods modified with static) cannot be declared as abstract methods.
Abstract methods: (1) if a class contains abstract methods, it must be an abstract class. (2) any subclass must override the abstract method of the parent class, or declare itself as an abstract class.
Definition: abstract class class name
Example: farmers feed animals
Animal: abstract classes-getName (), move (), drink ()
Reptile, Mammal: inherit abstract classes
Snake, tiger, goat, rabbit: inherit Reptile or Mammal respectively
Fammer:bringWater (), feedWater (Animal animal)-responsible for bringing water to a designated place, and then the animal moves to its destination to drink water
The code is as follows:
Package abstractDemo; / * * @ author lpx * @ Description TODO * @ date 2021-04-07 * / public abstract class Animal {abstract String getName (); abstract void move (String destination); abstract void drink ();} abstract class Reptile extends Animal {} abstract class Mammal extends Animal {} class Tiger extends Mammal {private static String name= "tiger"; @ Override String getName () {return this.name } @ Override void move (String destination) {System.out.println ("tiger move to" + destination+ ".");} @ Override void drink () {System.out.println ("tiger lower it is head and drink");}} class Goat extends Mammal {private static String name= "goat"; @ Override String getName () {return this.name } @ Override void move (String destination) {System.out.println ("goat move to" + destination+ ".");} @ Override void drink () {System.out.println ("goat lower it head to drink");}} class Rabbit extends Mammal {private static String name= "rabbit"; @ Override String getName () {return this.name } @ Override void move (String destination) {System.out.println ("rabbit move to" + destination+ ".");} @ Override void drink () {System.out.println ("rabbit put out it is tongue and drink");}} class Snake extends Reptile {private static String name= "snake"; @ Override String getName () {return this.name } @ Override void move (String destination) {System.out.println ("snake move to" + destination+ ".");} @ Override void drink () {System.out.println ("snake dived into and drink");}} class Farmer {public void bringWater (String destination) {System.out.println ("Farmer bring water to" + destination+ ".") } public void feedWater (Animal a) {/ / polymorphism this.bringWater ("Feeding Room"); a.move ("Feeding Room"); a.drink ();}} class Test {public static void main (String [] args) {Farmer fm=new Farmer (); Snake snake=new Snake (); Goat goat=new Goat (); Tiger tiger=new Tiger (); Rabbit rabbit=new Rabbit () Fm.feedWater (snake); fm.feedWater (goat); fm.feedWater (tiger); fm.feedWater (rabbit);}}
Summary:
If each animal does not abstract drink and move methods, it will not be possible to achieve polymorphism. The farmer class needs to overload multiple feedWater depending on the parameters. If you continue to add animals, the number of overloads will increase. In order to facilitate the comparison and understanding, I also wrote it according to the conventional thinking, the code is as follows:
Package abstractDemo; / * * @ author lpx * @ Description TODO * @ date 2021-04-07 * / class Tiger1 {private static String name= "tiger"; String getName () {return this.name;} void move (String destination) {System.out.println ("tiger move to" + destination+ ".");} void drink () {System.out.println ("tiger lower it is head and drink") } class Goat1 {private static String name= "goat"; String getName () {return this.name;} void move (String destination) {System.out.println ("goat move to" + destination+ ".");} void drink () {System.out.println ("goat lower it head to drink");}} class Rabbit1 {private static String name= "rabbit" String getName () {return this.name;} void move (String destination) {System.out.println ("rabbit move to" + destination+ ".");} void drink () {System.out.println ("rabbit put out it is tongue and drink");}} class Farmer1 {public void bringWater (String destination) {System.out.println ("Farmer bring water to" + destination+ ".") } public void feedWater (Goat1 goat) {/ / polymorphism this.bringWater ("Feeding Room"); goat.move ("Feeding Room"); goat.drink ();} public void feedWater (Tiger1 tiger) {/ / polymorphism this.bringWater ("Feeding Room"); tiger.move ("Feeding Room"); tiger.drink () } public void feedWater (Rabbit1 rabbit1) {this.bringWater ("Feeding Room"); rabbit1.move ("Feeding Room"); rabbit1.drink ();}} public class Test1 {public static void main (String [] args) {Farmer1 farmer1=new Farmer1 (); Tiger1 tiger1=new Tiger1 (); Goat1 goat1=new Goat1 (); Rabbit1 rabbit1=new Rabbit1 (); farmer1.feedWater (tiger1) Farmer1.feedWater (goat1); farmer1.feedWater (rabbit1);}}
Hand knock on this example, found that the foundation is very important ah, the previous study is not meticulous, resulting in many places confused do not know why to write that.
(1) multiple class classes can be defined in a .java file, but only one can be defined as public, and the class name must be the same as the file name.
(2) the abstract class inherits the abstract class and does not need to cover its abstract methods (the reason is also very simple, it is an abstract unimplementable method body). While non-abstract classes inherit abstract classes, abstract methods must be overridden and non-abstract methods are not necessary.
(3) the difference between rewriting and reloading (high frequency interview questions)
Overload: define the same method name with different parameters. Belongs to compile-time polymorphism
Override: the subclass overrides the parent class's method @ Override. Belong to run-time polymorphism
The above is the content of this article on "definition of Java abstraction and corresponding code example analysis". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, please pay attention to the industry information channel.
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.