Java Object-oriented note 6

Source: Internet
Author: User

When we write a class, we often need to define methods for it, which are used primarily to describe how the class behaves, so these methods have specific


The method body. In some cases, however, a parent class simply knows how its subclasses should be, but does not know exactly how these subclasses are implemented


These methods. What is the solution to the above problems? These requirements can be met using abstract methods: The abstract method is only a method signature, no method


The method of implementation.


Abstract methods and abstract classes must be defined using the abstract modifier, which can only be defined as abstract classes, and abstract classes are not abstract


Methods, the rules for abstract methods and abstract classes are as follows:

1. Abstract classes must be decorated with the abstract modifier class, and the abstract method must be decorated with an abstract modifier, which cannot have a method body.


2, abstract class cannot be instantiated, cannot use the new keyword to invoke the constructor of an abstract class to create an instance of an abstract class, even if the abstract class does not draw


Like method, you cannot create an instance.


3. Abstract class can contain field, common method and abstract method, constructor, initialization block, inner class, enumeration class 6 kinds of components. Constructors for abstract classes do not


Can be used to create an instance, primarily for invocation by its subclasses.


4. A class containing abstract methods can only be defined as abstract classes.


The abstract method is defined by simply adding an abstract modifier to the normal method and removing the curly braces from the method body of the normal method. And after the method is increased


Add a semicolon.


The following defines an abstract class:

Public abstract class Shape {public shape () {}public abstract double calperimeter ();p ublic abstract String getType ();}

In the above abstract class there is a parameterless constructor and two abstract methods, this parameterless constructor is used to initialize the subclass, Calperimeter


The abstract method is used to calculate the perimeter, and the GetType abstract method is to get the type of the shape.


We then define a class of triangles that inherit this abstract class and implement all the abstract methods in the abstract class:

public class Triangle extends Shape {private double a;private double b;private double c;public Triangle (double a,double B, Double c) {This.setsides (A, B, c);} @Overridepublic Double Calperimeter () {return a+b+c;} @Overridepublic String GetType () {return "triangle";} private void Setsides (double a,double b,double c) {if (a>=b+c| | b>=a+c| | C>=A+B) {System.out.println ("Both sides must be greater than the third side"); return;} This.a=a;this.b=b;this.c=c;}}
The above classes inherit the shape abstract class and implement the two abstract methods in the abstract class. Then define our execution Portal:

public class Maintest {public static void main (string[] args) {Shape shape=new Triangle (2, 2, 3); System.out.println (Shape.calperimeter ()); System.out.println (Shape.gettype ());}}

Output Result:

7.0
Triangle


As you can see from the program above, an abstract class cannot create an instance and is inherited only as a parent class. From a semantic point of view, abstract classes are from multiple


The abstract parent class in the body class, which has a higher level of abstraction. Abstract an abstract class from multiple classes with the same characteristics, with this abstract class as


As the template of its subclass, avoids the arbitrariness of the design of sub-class.


If you write an abstract parent class, the parent class provides a common method for multiple subclasses, and leaves one or more methods in the subclass implementation, which is a template model


, template mode is one of the most common design patterns, and the shape program above has already used template mode.


The following is an example of another template pattern:

Public abstract class Shape {public shape () {}public abstract double calperimeter ();p ublic double Getcalperimeter () {retur n Calperimeter ();}}
The above defines an abstract class in which a common method is defined for calculating the perimeter, but this common method relies on a pumping


And this abstract method is deferred to the subclass.

public class Triangle extends Shape {private double a;private double b;private double c;private void Setsides (double A, do Uble B, double c) {if (a >= B + c | | b >= A + c | | c >= A + b) {System.out.println ("The sum of the two sides must be greater than the third side"); THIS.A = a;this.b = B;THIS.C = C;} Public Triangle (double A, double b, double c) {This.setsides (A, B, c);} @Overridepublic Double Calperimeter () {return a + B + C;}}

public class Maintest {public static void main (string[] args) {Shape shape=new Triangle (2, 2, 3); System.out.println (Shape.calperimeter ());}}

Input Result:

7.0

The rules for using template mode are as follows:


1. Abstract parent class can only define some methods that need to be used, abstract the parts that cannot be implemented into abstract methods, and leave the subclasses to implement.


2. The parent class may contain methods that need to invoke other series methods, which can be implemented either by the parent class or by its subclasses. Parent class


The method provided simply defines a generic algorithm whose implementation may not be entirely self-fulfilling, but must depend on the helper of its parent class.



Reprint Please specify source: http://blog.csdn.net/hai_qing_xu_kong/article/details/43877437 Emotional Control _  






Java Object-oriented note 6

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.