Java:扩展内部类 [英] Java: Extending inner classes

查看:117
本文介绍了Java:扩展内部类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解在Java中扩展内部类。我读过,但没有什么,我发现很好地回答了我的问题。所以这里...

I am trying to understand extending inner classes in Java. I have read around but nothing I found quite answers my question. So here goes...

我有...

public class Pie{
    protected Slice[] slices;

    // Pie constructor
    public Pie(int n){
         sliceGenerator(n)
    }

    private void sliceGenerator(int n){
         slices = new Slice[n];
         final float sweepAngle = 360.0f/(float)n;
         float startAngle = 0;
         for (int i=0;i<n;i++){ 
             slices[i] = new Slice(startAngle);
             startAngle += sweepAngle;
         }
    }

    @Override
    public String toString(){
         for (Slice s:slices){  
             s.toString();
         }
    }

    // Inner class...
    public class Slice{
        public Slice(float startAngle){
             //set some private fields based on startAngle and generic pie 
        }

        @Override
        public String toString(){
             return **string based on private fields**
        }
    }
}

public class ApplePie extends Pie{
    protected Slice[] slices;

    // Apple Pie constructor
    public ApplePie(int n){
         super(n);
    }

    // Inner class...
    public class Slice extends Pie.Slice{
        public Slice(float startAngle){
            super(startAngle);
            //set some **additional** private fields based on startAngle **specific to apple pie** appleness or something
        }

        @Override
        public String toString(){
             return **string based on apple pie specific private fields**
        }
    }
}

现在,当我做一个苹果派,并调用其toString方法,像这样...

Now, when I make an Apple pie and call its toString method, like so...

ApplePie ap = new ApplePie(8);
System.out.println(ap.toString());

我没有得到关于苹果派切片的信息,它忽略我的 toString 覆盖,或更可能忽略我的苹果派 Slice 。我如何安排它,使苹果饼切片参考 ApplePie

I do not get information about the apple pie slices, but information about the pie slices. It ignores my toString override, or more likely ignores my apple pie Slice. How can I arrange it such that apple pie slices refer to ApplePie?

任何帮助非常感谢!对于饼图参考 - 这是我正在使用的实际类...

Any help much appreciated! Sorry for pie references - it is the actual class I am working with...

推荐答案

您的需求。

您的超类 Pie 即将创建一个新实例 Slice ,但子类ApplePie的Slice不会覆盖其超类的 Slice 方法。

Your super class Pie is about to create a new instance of Slice, but the child class ApplePie's Slice do not override the Slice method of its super class'.

我在下面添加了函数,以便让子类创建自己的 Slice

I added functions below in order to enable the child class to create its own Slice.

protected void newSliceArray(int n) {
    slices = new Slice[n];
}

protected Slice newSlice(float startAngle) {
    return new Slice(startAngle);
}

Pie.java:

public class Pie {
  private int a = 1;
  protected Slice[] slices;

  // Pie constructor
  public Pie(int n) {
    sliceGenerator(n);
  }

  private void sliceGenerator(int n) {
    newSliceArray(n);
    final float sweepAngle = 360.0f / n;
    float startAngle = 0;
    for (int i = 0; i < n; i++) {
      slices[i] = newSlice(startAngle);
      startAngle += sweepAngle;
    }
  }

  protected void newSliceArray(int n) {
    slices = new Slice[n];
  }


  protected Slice newSlice(float startAngle) {
    return new Slice(startAngle);
  }

  @Override
  public String toString() {
    String t = "";
    for (Slice s : slices) {
      t += s.toString();
    }
    return t;
  }

  // Inner class...
  public class Slice {
    public Slice(float startAngle) {
      // set some private fields based on startAngle and generic pie
    }

    @Override
    public String toString() {
      return "" + a;
    }
  }
}



ApplePie.java:

ApplePie.java:

public class ApplePie extends Pie {
  private int b = 2;

  // protected Slice[] slices;

  // Apple Pie constructor
  public ApplePie(int n) {
    super(n);
  }

  protected void newSliceArray(int n) {
    slices = new Slice[n];
  }

  protected Slice newSlice(float startAngle) {
    return new Slice(startAngle);
  }

  // Inner class...
  public class Slice extends Pie.Slice {
    public Slice(float startAngle) {
      super(startAngle);
      // set some **additional** private fields based on startAngle **specific to apple pie**
      // appleness or something
    }

    @Override
    public String toString() {
      return b + "";
    }
  }
}

测试:

public static void main(String[] args) {
    ApplePie ap = new ApplePie(8);
    System.out.println(ap.toString());
}

代码将打印 22222222

这篇关于Java:扩展内部类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆