0:00

Assume that the following declaration appears in a client program Base b = new Derived();. What is the result of the call b.methodOne()?

public class Base
{
   public void methodOne()
   {
      System.out.print("A");
      methodTwo();
   }

   public void methodTwo()
   {
      System.out.print("B");
   }

   public static void main(String[] args)
   {
      Base b = new Derived();
      b.methodOne();
   }
}

class Derived extends Base
{
   public void methodOne()
   {
      super.methodOne();
      System.out.print("C");
   }

   public void methodTwo()
   {
      super.methodTwo();
      System.out.print("D");
   }
}
super.methodOne()中调用的methodTwo()会是Derived中定义的。
1

Given the class definitions of Point2D and Point3D below, which of the constructors that follow (labeled I, II, and III) would be valid in class Point3D?

class Point2D {
   public int x;
   public int y;

   public Point2D() {}

   public Point2D(int x,int y) {
     this.x = x;
     this.y = y;
   }
   // other methods
}

public class Point3D extends Point2D
{
   public int z;

   // other code
}

// possible constructors for Point3D
I.  public Point3D() {}
II. public Point3D(int x, int y, int z)
    {
       super(x,y);
       this.z = z;
    }
III. public Point3D(int x, int y)
     {
        this.x = x;
        this.y = y;
        this.z = 0;
     }
注意xy均是public,都能在child class中被直接访问。
4


陈 欣

AADPS创始人

发表评论