The example of this article tells the method of Java interface, polymorphism, inheritance, and class computing triangle and rectangular length and area. Share it for everyone for your reference. The specifics are as follows:
Define interface specifications:
/ ** * @AUTHOR VVV * @Date 2013-8-10 AM 08:56:48 */ Package com.duotai;/ ** * * * * */ Public Interface Shape {Public Double Area (); Public Double Longer () ; } /** * @author vvv * @date 2013-8-10 上午09:10:06 */ package com.duotai; /** * * */ public class Triangle implements Shape { double s1; double s2; double S3; // Initialize a triangle object, and give the triangle three -sided Public Triangle (double s1, double s2, double s3) {if (IStri (S1, S2, S3)) {this.s1 = s1; this.s2 = s2; this.s3 = s3;} else {system.out.println ("The input three -sided" + s1 + "," + s2 + "," + s3 + "cannot form a triangle, please enter the three re -enter three Border length! ");}} // Judging whether it is a triangle Public Boolean Istri (Double S1, Double S2, Double S3) {if (S1 + S2 <s3) {Return false;} if (S1 + S3 <s2) { Return false;} if (s2 + s3 <s1) {return false;} Return true;} / * * (non-javadoc) * * @see com.duotai.Shape#Area () * / @Override C double area ( ) {double p = (s1 + s2 + s3) / 2; Return math.sqrt (p * (p -s1) * (p -s2) * (p -s3));} / * * (non -javadoc) * * @see com.duotai.Shape#Longer () * / @Override Public Double Longer () {Return S1 + S2 + S3;} / ** * @Authouse vvv * @date 2013-8-10 AM 09: 12:06 */ package com.duotai;/ ** * * * */ Public Class Director Implements Shape {Double S1; Double S2; // Initialize a rectangular Public Director (Dou BLE S1, Double S2) {this.s1 = s1; this.s2 = s2;} / * * (non-javadoc) * * * @see com.duotai.shape #Area () * / @Override dough are () {// Todo Auto- Generated METHOD Stub Return S1 * S2;} / * * (Non-Javadoc) * * @See com.duotai.Shape#Longer () * / @Override Public Double Longer () {// Todo Auto-G ENERATED METHOD Stub Return 2 * (s1 + s2);}} /** * @AutHor vvv * @date 2013-8-10 AM 09:13:30 * /Package com.duotai; /** * * * * /public class test { / * * * @Param ARGS */ Public Static Void Main (String [] ARGS) {Shape Triangle = New Triangle (3, 4, 8); // Create a triangular Shape Tri = New Triangle (3, 4, 5); Shape Director = New Director (10, 20); // Create a rectangular System.out.println ("Triangle Triangle." + Triangle. loger ()); System.out.println ("The area of triangle triangle is:" + triangle.area ()); system.out.println ("The circumference of triangular Tri is: + tri.longer ()); System.out.println ("The area of the triangle tri is:" + tri.area ())); System.out.println ("The perimeter of the rectangle is: + Director.longer ()); ("The area of the rectangle is:" + Director.area ());}}It is hoped that this article is helpful to everyone's Java program design.