English 中文(简体)
多形态呼吁取决于变量
原标题:polymorphic call depends on variable

我有以下问题。 试图发出多变呼吁和结果将取决于根据基本类别变化的变量。 然而,它搁置了不同的事情。 让我知道应该改变什么。 问题在于,虽然C.w读到当地变数,但缺到0,从适当类别看,它总是违约到0。 该守则是:

class Cycle{
    private int w = 0;
    public void move(){
        System.out.println("Cycle moving");
    }
    public int wheels(Cycle c){
        switch (c.w){

            case 1:     return 1; 
            case 2:     return 2;
            case 3:     return 3;
            default:    return 0;
        }
    }
}
class Unicycle extends Cycle{
    public int w = 1;
    public void go(){
        System.out.println("Unicycle go");
    }
}
class Bicycle extends Cycle{
    public int w = 2;
    public void go(){
        System.out.println("Bicycle go");
    }
}
class Tricycle extends Cycle{
    public int w = 3;
    public void go(){
        System.out.println("Tricycle go");
    }
}

public class TestCycle {

    public static void ride(Cycle c){
        c.move();
        int now = c.wheels(c);
        System.out.println(now);

    }

    public static void main(String[] args){
        Bicycle b = new Bicycle();
        ride(b);
        Unicycle u = new Unicycle();
        ride(u);
        Tricycle t = new Tricycle();
        ride(t);
    }

}
最佳回答

您的问题(其中之一)是,你正在重新界定你各子类别中的类别变量。 界定其为循环成员,每个子级在构造中正确确定。

class Cycle{
    protected int w;
    public void move(){
        System.out.println("Cycle moving");
    }
    public int wheels(){
        return w;
    }
}

class Unicycle extends Cycle{
    public Unicycle() {
        w = 1;
    }
     public void go(){
        System.out.println("Unicycle go");
    }
}

或者,你可以确定一种称为“轮船”的抽象方法,在子类中压倒。 它是一件事。

问题回答

轮椅法应当更像

public int getWheelCount(){
   return this.w;
}

你自己援引这一论点,不需要通过论据。 如果目前的情况是三管齐下,这种方法将回归三轮。

由于<代码>Cycle.w是私人的,从继承人看不出。 这意味着,例如,“三轮”不会产生“相同”的变数,也不见周期(因此,你总是有零)。 您必须使<代码>Cycle.w至少受到保护,然后从所有子流中除去,并将其每层层构造的价值确定到你想要的东西。

这或许不是你寻找的答案,而是以下工作。 请提供更多详情,说明你试图做些什么。

public abstract class Cycle {
   protected int nWheels;
   protected String goText;

   // no constructor.

   public void go() {
     System.out.println(goText);
   }

   public int wheels() {
     return nWheels;
  }
}
...

public class Unicycle extends Cycle {

   public Unicycle() {
      nWheels = 1;
      goText = "Unicycle go";
   }
}

请注意:I madeCycleabstract 因为我不想立即这样做。

www.un.org/Depts/DGACM/index_spanish.htm

public static int getNumberOfWheels(Cycle cycle) {
   return cycle.wheels();
}

这一点显然并不有用,因为简单地要求<代码>.wheels()与要求这一功能相同。

我不敢肯定,为什么你想要避免建筑商。 或许应该写一下你试图回答的准确问题。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签