请教Java~~~1、定义一个接口AreaInterface,接口中定义了抽象方法area().类Rectangle(长

1个回答

  • 1.

    TestArea.java

    ----------------------------------------

    interface AreaInterface

    {

    public abstract float area();

    }

    class Rectangle implements AreaInterface

    {

    private float x, y;

    public Rectangle(float X, float Y)

    {

    x = X;

    y = Y;

    }

    @Override

    public float area() {

    return x * y;

    }

    public String toString()

    {

    return String.format("长方形的长为%1$.2f,宽为%2$.2f,面积为%3$.2f", x, y, area()) ;

    }

    }

    public class TestArea {

    public static void main(String[] args) {

    Rectangle rectangle = new Rectangle(10.0f, 20.0f);

    System.out.println(rectangle.toString());

    }

    }

    ----------------------------------------------

    2.

    TestInner.java

    ----------------------

    class Outer{

    private String s = "I am Outer.";

    class Inner{

    private String s = "I am Inner.";

    public void say()

    {

    System.out.println(Outer.this.s);

    System.out.println(this.s);

    }

    }

    }

    class TestInner {

    public static void main(String[] args) {

    Outer outer = new Outer();

    Outer.Inner inner = outer.new Inner();

    inner.say();

    }

    }

    -------------------------------

    3.

    ClassA.java

    -------------------

    package package1;

    public class ClassA {

    public void methodA(){

    System.out.println("I am methodA() in ClassA.");

    }

    }

    ---------------

    ClassB.java

    --------------------

    package package2;

    public class ClassB {

    public void methodB(){

    package1.ClassA classA = new package1.ClassA();

    classA.methodA();

    System.out.println("I am methodB() in ClassB.");

    }

    public static void main(String[] args) {

    ClassB classB = new ClassB();

    classB.methodB();

    }

    }