In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
In this article, the editor introduces in detail "how to use the keyword abstract of Java". The content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to use the keyword abstract of Java" can help you solve your doubts.
1. Understand
Abstract: abstract
two。 Action
Abstract can be used to modify classes and methods.
Variables, code blocks, and constructors cannot be modified with abstract.
You cannot use abstract to modify private methods, static methods, final methods, or final classes.
3. Modifier class-abstract class
There must be a constructor in the abstract class so that it can be called when the subclass is instantiated (involving the whole process of subclass object instantiation).
In development, subclasses of abstract classes are provided to instantiate subclass objects and complete related operations.
Abstract classes cannot be instantiated. Abstract classes are used to be inherited, and subclasses of abstract classes must override the abstract methods of the parent class and provide the method body. If you do not override all abstract methods, it is still an abstract class.
4. Modification method-Abstract method
Abstract methods have only the declaration of the method, not the implementation of the method. End with a semicolon.
Public abstract void talk ()
A class containing abstract methods must be declared as an abstract class. Conversely, there can be no abstract methods in an abstract class.
If the subclass overrides all abstract methods in the parent class, the subclass can be instantiated.
If the subclass does not override all the abstract methods in the parent class, the subclass is also an abstract class and needs to be decorated with abstract.
5. The code demonstrates that public class AbstractTest {public static void main (String [] args) {/ / once the Person class is abstract, it cannot be instantiated / / Person p1 = new Person (); / / p1.eat ();}} abstract class Creature {public abstract void breath ();} abstract class Person extends Creature {String name Int age; public Person () {} public Person (String name,int age) {this.name = name; this.age = age } / / is not an abstract method: / / public void eat () {/} / / abstract method public abstract void eat (); public void walk () {System.out.println ("people walk") } class Student extends Person {public Student (String name,int age) {super (name,age);} public Student () {} public void eat () {System.out.println ("students eat more nutritious food") } @ Override public void breath () {System.out.println ("students should breathe fresh air without haze");}} 6. Classic title public class Test1 {public static void main (String args []) {An a = new B (); a.m1 (); / / M1 method defined in Class B a.m2 (); / / m2 method defined in Class A} abstract class A {abstract void M1 (); public void m2 () {System.out.println ("m2 method defined in Class A") }} class B extends A {void M1 () {System.out.println ("M1 methods defined in Class B");}} 7. Anonymous subclass public class PersonTest {public static void main (String [] args) {/ / anonymous object method (new Student ()); / / non-anonymous class non-anonymous object Worker worker = new Worker (); method1 (worker) / / non-anonymous class anonymous object method1 (new Worker ()) / / create an object of an anonymous subclass: p Person p = new Person () {@ Override public void eat () {System.out.println ("eat") } @ Override public void breath () {System.out.println ("breathe");}}; method1 (p) / / create an anonymous object method1 (new Person () {@ Override public void eat () {System.out.println ("eat well") of the anonymous subclass } @ Override public void breath () {System.out.println ("get some fresh air");}}) } public static void method1 (Person p) {p.eat (); p.breath () } public static void method (Student s) {}} class Worker extends Person {@ Override public void eat () {} @ Override public void breath () {}} 8. Application-template Design pattern (TemplateMethod)
Abstract class embodies the design of a template pattern, abstract class as a general template of multiple subclasses, subclasses expand and transform on the basis of abstract classes, but subclasses generally retain the behavior of abstract classes.
When part of the implementation of the function is determined, part of the implementation is uncertain. At this point, you can expose the uncertain parts and let the subclasses implement them.
In other words, when implementing an algorithm in software development, the overall steps are fixed and generic, and these steps are already written in the parent class. But some parts are changeable, and the volatile parts can be abstracted for different subclasses to implement. This is a template mode.
Template method design pattern is a pattern that is often used in programming. He can be seen in various frameworks and class libraries, such as the common ones:
Encapsulation of database access
Junit unit test
About doGet/doPost method calls in JavaWeb's Servlet
Template program in Hibernate
JDBCTemlate, HibernateTemplate and so on in Spring
Abstract class Template {public final void getTime () {long start = System.currentTimeMillis (); code (); long end = System.currentTimeMillis (); System.out.println ("execution time is:" (end-start));} public abstract void code ();} class SubTemplate extends Template {public void code () {for (int I = 0; I < 10000; iTunes +) {System.out.println (I) } after reading this, the article "how to use the keyword abstract of Java" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow 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.