Java + object-oriented abstract class and function implementation (calculation of triangles, circular area) __ block chain

Source: Internet
Author: User

The abstract abstract class and function are implemented in this paper, and its function is to compute the area of triangles and circles.


Abstract-oriented programming means that when designing an important class, the class is not oriented to a specific class, but rather to an abstract class, and the important data in the design class is an object declared by an abstract class, rather than an object declared by a specific class. is to use abstract to design the implementation of user needs.


Step one: Define an abstract class geometry, define an abstract Getarea () method in the class, geometry the following class. This abstract class abstracts all methods of calculating the area into one identity: Getarea () does not need to consider algorithmic details.

Public abstract class geometry{public
    abstract Double Getarea ();
}

When a Non-abstract class is a subclass of an abstract class (such as geometry), it must override the abstract method of the parent class (such as Getarea ()) and give the method body.


=============================================================================================================== =================

In the following complete test

We will use the five class 1.Pillar 2.Geometry 3.Lader 4.Circle 5.example5_10

.

.

1.Pillar

.

.

.


2. Geometry (abstract Class)


.

.

.

3.Lader

.

.

.

4.Circle

.

.

.

5.EXAMPLE5_10 Test class

.

.


=============================================================================================================== =================

.

Let's take a look at the code implementation

.

.

Column Body Pillar class

Package com.ash.www;

public class pillar{

	Geometry bottom;	Geometry object to finish member
	double height;		High
	
	pillar (Geometry bottom, double height) {	//define constructor
		this.bottom = bottom;
		this.height = height;
	}
	void Changebottom (Geometry bottom) {
		this.bottom = bottom;
	}
	Public double Getvolume () {return
		Bottom.getarea () * height;
	}
}

.

.

.

Geometry Geometry Class (abstract class)

Package com.ash.www;

Public abstract class Geometry {public
	
	abstract double Getarea ();

}
.

.

.

Trapezoidal Lader Class

Package com.ash.www;

public class Lader extends geometry{	//Inheriting abstract class Geometry
	Double A, B, H;
	
	Lader (double A, double B, double h) {
		this.a = A;
		this.b = b;
		This.h = h;
	}
	Public double Getarea () {return
		(1/2.0) * (A + b) * H;
	}

}

.

.

.

Round Circle Class

Package com.ash.www;

public class Circle extends geometry{	//Inheriting abstract class Geometry
	double R;
	
	Circle (Double R) {
		THIS.R = R;
	}
	Public double Getarea () {return
		(3.14 * r);
	}
	
}
.

.

.

EXAMPLE5_10 Test class

Package com.ash.www;

public class Example5_10 {

	/**
	 * @param args */public
	static void Main (string[] args) {
		//TODO auto- Generated method stub

		pillar pillar;
		Geometry tuxing;
		
		tuxing = new Lader (in);	Tuxing as a reference to the Lader object
		System.out.println ("Lader area:" + Tuxing.getarea ());
		
		Pillar = new Pillar (tuxing,);	
		System.out.println ("ladered pillar Area:" + pillar.getvolume ());
		
		tuxing = new Circle (a);	Tuxing as a reference to the Circle object
		System.out.println ("When radius = ten, the Circle area:" + Tuxing.getarea ());
		
		Pillar.changebottom (tuxing);	
		System.out.println ("Circled Pillar Area:" + pillar.getvolume ());
		
	}


.

.

.

.

After reading this article, do you have a deeper understanding of the abstraction of abstract abstract classes and function implementations?





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.