How to define an abstract attribute example in java and how to define an example in java

Source: Internet
Author: User
Tags define abstract

How to define an abstract attribute example in java and how to define an example in java

Preface

This article mainly introduces the definition of an abstract attribute in java and shares it for your reference. I will not talk about it much below. Let's take a look at the detailed introduction:

Abstract keywords are usually used for classes and methods to delegate the implementation of certain behaviors to subclasses. Java does not support abstract attributes. If you try to mark the class attributes as abstract, a compile-time error will be returned.

In this tutorial, we will introduce two methods to define Abstract attributes. These Abstract attributes can be set by subclass without using Abstract keywords.

Practical cases

Suppose we want to implement a logging module that records transactions and records the information of specific transactions. We hope this module is abstract, so that we can implement different logging methods, such as logging to files or databases.

Our engine uses predefined delimiters to connect information in logs and store it in a String. Which delimiter should be used depends on the logging rules. For example, you can use the character "," to separate the information of different parts of the log.

Therefore, the separator seems abstract to our engine and needs to be clearly defined by each logging rule.

The following two methods are provided to delegate the definition of separators to sub-classes.

Define constructors with parameters in abstract classes

The first way to define dynamic attributes in an abstract class is to define the constructor of a parameter.

So we can implement this engine like this:

// TransactionManager.javapublic abstract class TransactionManager { private String separator;  public TransactionManager(String separator) { this.separator = separator; }  public abstract void writeTransaction(String result);  public Transaction startTransaction() { Transaction transaction = new Transaction(System.currentTimeMillis()); return transaction; }  public void endTransaction(Transaction t) { long processingTime = System.currentTimeMillis() - t.getStartTime();  StringBuilder logBuilder = new StringBuilder(); logBuilder.append(t.getStartTime()); // Notice the use of this.separator logBuilder.append(this.separator); logBuilder.append(processingTime); logBuilder.append(this.separator); logBuilder.append(t.getData());  String result = logBuilder.toString(); writeTransaction(result); }}

When a constructor with parameters is defined in an abstract class, the subclass will be forced to define its own constructor and call it.super(). In this way, we can force the separator attribute to depend on the used logging mechanism.

Note:Our engine implements the static behavior of all log mechanisms:startTransaction(),endTransaction() And Dynamic BehaviorwriteTransaction()It is implemented by the subclass.

Now, if we want to create a Transaction Manager and use it to record the log content to a file, we can define it as follows:

public class TransactionManagerFS extends TransactionManager{  // The IDE forces you to implement constructor. public TransactionManagerFS(String separator) { super(separator); }  @Override public void writeTransaction(String result) { System.out.println("The following transaction has just finished: " ); System.out.println(result); }}

Next, let's perform a test to see how the code works.

public static void main(String[] args) throws InterruptedException { // we pass the separator explicitly in the constructor TransactionManager transactionManager = new TransactionManagerFS(","); Transaction transaction = transactionManager.startTransaction(); transaction.setData("This is a test transaction !!"); Thread.sleep(1500); transactionManager.endTransaction(transaction); }

Output:

The following transaction has just finished: 1502179140689,1501,This is a test transaction !!

Use the getter method to pass the delimiter

Another way to implement dynamic attributes is to define an abstract getter method to retrieve the required separators based on the current logging mechanism. In our engine, you can call this getter method to obtain the delimiter.

Next we will modify the engine to the following:

public abstract class TransactionManager {  public abstract String getSeperator(); public abstract void writeTransaction(String result);  public Transaction startTransaction() { Transaction transaction = new Transaction(System.currentTimeMillis()); return transaction; }  public void endTransaction(Transaction t) { long processingTime = System.currentTimeMillis() - t.getStartTime();  StringBuilder logBuilder = new StringBuilder(); logBuilder.append(t.getStartTime()); // Notice the use of getSeparator() logBuilder.append(getSeperator()); logBuilder.append(processingTime); logBuilder.append(getSeperator()); logBuilder.append(t.getData());  String result = logBuilder.toString(); writeTransaction(result); }}

In addition, modify TransactionManagerFS as follows:

public class TransactionManagerFS extends TransactionManager{  @Override public String getSeperator() { return ","; }  @Override public void writeTransaction(String result) { System.out.println("The following transaction has just finished: " ); System.out.println(result); }}

Then, modify main to use the new implementation and ensure that the correct result is obtained.

public static void main(String[] args) throws InterruptedException { // The separator is defined implicitly using getSeparator() method of the manager TransactionManager transactionManager = new TransactionManagerFS(); Transaction transaction = transactionManager.startTransaction(); transaction.setData("This is a test transaction !!"); Thread.sleep(1500); transactionManager.endTransaction(transaction); }

Output:

The following transaction has just finished: 1502179140689,1501,This is a test transaction !!

Summary

The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.

Translation: Crazy Technology House

Original article: http://programmergate.com/define-abstract-property-java/

Public Account first published in this article: full brain Holes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.