This article illustrates the method of Java interface, polymorphism, inheritance, class calculation triangles and rectangular perimeter and area. Share to everyone for your reference. Specifically as follows:
Defining Interface Specifications:
/** * @author VVV * @date 2013-8-10 a.m. 08:56:48 * * Package com.duotai;
/** * * */public interface Shape {public double area ();
public double longer ();
/** * @author VVV * @date 2013-8-10 a.m. 09:10:06 * * Package com.duotai;
/** * * */public class Triangle implements Shape {double S1;
Double S2;
Double S3; Initializes a triangular object and gives the triangular three-side long public triangle (double s1, double S2, double S3) {if (Istri (S1, S2, S3)) {THIS.S
1 = S1;
THIS.S2 = s2;
THIS.S3 = S3; else {System.out.println ("entered the three-side long" + S1 +, "+ s2 +", "+ s3 +" can not form a triangle, please re-enter the three-side length!)
"); }//Determine if it is a triangle public boolean istri (double s1, double S2, double S3) {if (S1 + s2 < S3) {Retur
n false;
} if (S1 + s3 < S2) {return false;
} if (S2 + s3 < S1) {return false;
return true; }/* * (NON-JAVADOC) * * @see Com.duotai.shape#area ()/@OverRide public double () {double] = (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;
}/** * @author VVV * @date 2013-8-10 a.m. 09:12:06 * * Package com.duotai;
/** * * */public class Director implements Shape {double S1;
Double S2;
Initializes a rectangle, giving the rectangle both sides long public Director (double s1, double s2) {this.s1 = S1;
THIS.S2 = s2; }/* * (NON-JAVADOC) * * @see Com.duotai.shape#area () */@Override public double area () {//
TODO auto-generated Method stub return s1 * S2;
}/* * (NON-JAVADOC) * * @see Com.duotai.shape#longer ()/@Override public double longer () {
TODO auto-generated Method Stub return 2 * (S1 + s2); }/** * @author VVV * @date 2013-8-10 a.m. 09:13:30 * * Package com. Duotai; /** * * */public class Test {/** * @param args/public static void main (string[] args) {Sha
PE triangle = new triangle (3, 4, 8);
Create a triangular Shape tri = new Triangle (3, 4, 5) with a three-side length of 3,4,5;
Shape director = new Director (10, 20);
Create a new rectangular System.out.println with two sides long 10,20 ("The perimeter of the triangle triangle is:" + Triangle.longer ());
System.out.println ("Triangle Triangle area is:" + Triangle.area ());
System.out.println ("The perimeter of the triangle Tri is:" + Tri.longer ());
System.out.println ("Triangle Tri area is:" + Tri.area ());
System.out.println ("The circumference of the rectangle is:" + Director.longer ());
System.out.println ("The area of the rectangle is:" + Director.area ()); }
}
I hope this article will help you with your Java programming.