/Files/Wangs/June 17 test. Doc
1
ClassFushu
{
IntA; // Real-Time
IntB; // virtual part
PublicFushu (IntX,IntY)
{
This. A = X;
This. B = y;
}
PublicFushu add (fushu Add1)
{
IntX = add1.a +This.;
IntY = add1. B +This. B;
Return NewFushu (x, y );
}
PublicFushu plus (fushu sub1)
{
IntX =This. A-sub1.a;
IntY =This. B-sub1. B;
Return NewFushu (x, y );
}
PublicString tostring (){
String RT = "";
If(B> 0)
RT = "(" + A + "+" + B + "I" + ")";
If(B = 0)
RT = "(" + A + ")";
If(B <0)
RT = "(" + A + B + "I" + ")";
ReturnRT;
}
Public Static VoidMain (string [] ARGs)
{
Fushu fushu1 =NewFushu (3, 4 );
Fushu fushu2 =NewFushu (2,-13 );
Fushu result = fushu1.add (fushu2 );
System.Out. Println (result );
// System. Out.Println("Result =" + result. A + "+" + result. B + "I ");
}
}
2. // interfacetest. Java
// Main class interfacetest
Public class interfacetest {
Public static void main (string ARGs []) {
Myline line = new myline (15 );
Mycircle Circle = new mycircle (10 );
Myrect rect = new myrect (10, 20 );
Shape shapes [] = new shape [3];
Shapes [0] = line;
Shapes [1] = circle;
Shapes [2] = rect;
String output =
Line. tostring () + "\ n"
+ Circle. tostring () + "\ n" + rect. tostring ();
For (INT I = 0; I <shapes. length; I ++ ){
Output + = "\ n" +
"" + Shapes [I]. tostring () +
"\ Narea =" +
Shapes [I]. Length ();
}
System. Out. println ();
System. Out. println (output );
}
}
// Define the Interface Shape
Interface Shape {
// Calculate the length
Public abstract double length ();
}
// Define a Class Point
Class myline
Implements shape {
Private int length;
Public myline (INT length ){
This. Length = length;
}
Public String tostring (){
Return "this is myline ";
}
Public double length (){
Return length;
}
}
Class mycircle
Implements shape {
Protected double radius;
Public mycircle (double circleradius ){
This. radius = circleradius;
}
Public double length (){
Return 2 * Math. Pi * radius;
}
Public String tostring (){
Return "this is a circle ";
}
}
Class myrect
Implements shape {
Protected int height;
Protected int width;
Public myrect (INT height, int width ){
This. Height = height;
This. width = width;
}
Public double length (){
Return 2 * (height + width );
}
Public String tostring (){
Return "this is a rectangle ";
}
}