Java 扩展示例

发布于 2024-10-02 18:29:09 字数 342 浏览 4 评论 0原文

我有一个java初学者问题: Parent.print() 在控制台中打印“hallo”, 而且 Child.print() 也打印“hallo”。 我认为它必须打印“孩子”。 我该如何解决这个问题?

public class Parent {

    private String output = "hallo";

    public void print() {
        System.out.println(output);
    }

}

public class Child extends Parent {

   private String output = "child";

}

i have a java beginner question:
Parent.print() prints "hallo" in the console,
but also Child.print() prints "hallo".
I thought it has to print "child".
How can i solve this?

public class Parent {

    private String output = "hallo";

    public void print() {
        System.out.println(output);
    }

}

public class Child extends Parent {

   private String output = "child";

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

笑梦风尘 2024-10-09 18:29:09

目前您有两个单独的变量,并且 Parent 中的代码仅了解 Parent.output。您需要将Parent.output的值设置为“child”。例如:

public class Parent {

  private String output = "hallo";

  protected void setOutput(String output) {
    this.output = output;
  }

  public void print() {
    System.out.println(output );
  }
}

public class Child extends Parent {
  public Child() {
    setOutput("child");
  }
}

另一种方法是为父类提供一个构造函数来获取所需的输出:

public class Parent {
  private String output;

  public Parent(String output) {
    this.output = output;
  }

  public Parent() {
    this("hallo");
  }

  public void print() {
    System.out.println(output );
  }
}

public class Child extends Parent {
  public Child() {
    super("child");
  }
}

这实际上取决于您想要做什么。

Currently you've got two separate variables, and the code in Parent only knows about Parent.output. You need to set the value of Parent.output to "child". For example:

public class Parent {

  private String output = "hallo";

  protected void setOutput(String output) {
    this.output = output;
  }

  public void print() {
    System.out.println(output );
  }
}

public class Child extends Parent {
  public Child() {
    setOutput("child");
  }
}

An alternative approach would be to give the Parent class a constructor which took the desired output:

public class Parent {
  private String output;

  public Parent(String output) {
    this.output = output;
  }

  public Parent() {
    this("hallo");
  }

  public void print() {
    System.out.println(output );
  }
}

public class Child extends Parent {
  public Child() {
    super("child");
  }
}

It really depends on what you want to do.

誰認得朕 2024-10-09 18:29:09

Child 无法访问 Parentoutput 实例变量,因为它是私有。您需要做的就是使其受保护,并在Child 的构造函数中将output 设置为“child”

换句话说,两个output变量是不同的。

如果您将 Parent 中的输出更改为受protected,您也可以执行此操作:

public void print(){
    output = "child"
    super.print();
}

Child doesn't have access to Parent's output instance variable because it is private. What you need to do is make it protected and in the constructor of Child set output to "child".

In other words, the two output variables are different.

You could also do this if you change output to be protected in Parent:

public void print(){
    output = "child"
    super.print();
}
黄昏下泛黄的笔记 2024-10-09 18:29:09

child不打印“child”的原因是java中的继承中,只继承方法,而不继承字段。变量output 不会被子级覆盖。

您可以这样做:

public class Parent {
    private String parentOutput = "hallo";

    String getOutput() {
        return output;
    }

    public void print() {
        System.out.println(getOutput());
    }
}

public class Child extends Parent {
    private String childOutput = "child";

    String getOutput() {
        return output;
    }
}

此外,字符串变量不需要具有不同的名称,但为了清楚起见,我在这里这样做了。

另一种更易读的方法是这样做:

public class Parent {
    protected String output;

    public Parent() {
        output = "hallo";
    }

    public void print() {
        System.out.println(output);
    }
}

public class Child extends Parent {
    public Child() {
        output = "child";
    }
}

在本例中,变量是受保护的,这意味着它可以从父级和子级读取。类的构造函数将变量设置为所需的值。这样您只需实现一次打印功能,并且不需要重复的重写方法。

The reason why child is not printing "child" is that in inheritance in java, only methods are inherited, not fields. The variable output is not overridden by the child.

You could do it like this:

public class Parent {
    private String parentOutput = "hallo";

    String getOutput() {
        return output;
    }

    public void print() {
        System.out.println(getOutput());
    }
}

public class Child extends Parent {
    private String childOutput = "child";

    String getOutput() {
        return output;
    }
}

Also, the String variables do not need to be different names, but I did so here for clarity.

Another, more readable way would be to do this:

public class Parent {
    protected String output;

    public Parent() {
        output = "hallo";
    }

    public void print() {
        System.out.println(output);
    }
}

public class Child extends Parent {
    public Child() {
        output = "child";
    }
}

In this example the variable is protected, meaning it can be read from both the parent and child. The constructor of the classes sets the variable to the desired value. This way you only implement the print function once, and do not need a duplicate overridden method.

好久不见√ 2024-10-09 18:29:09

当我试图找出扩展关键字时,我使用了两个类。我希望这也能帮助您理解基本概念。

父类.java

public class Parent {
    private int a1;
    private int b1;


    public Parent(int a, int b){
        this.a1 = a;
        this.b1 = b;
    }

    public void print() {
        System.out.println("a1= " + this.a1 + " b1= " + this.b1);
    }

}

子类.java

public class Child extends Parent {
        public Child(int c1, int d1){
        super(c1,d1);
    }

    public static void main(String[] args) {
        Parent pa = new Parent(1,2);
        pa.print();
        Child ch = new Child(5,6);
        ch.print();
    }

}

When I was trying to figure out the extend keyword, I was using two classes. I'll hope that also will help you to understand the basic idea.

Parent.java

public class Parent {
    private int a1;
    private int b1;


    public Parent(int a, int b){
        this.a1 = a;
        this.b1 = b;
    }

    public void print() {
        System.out.println("a1= " + this.a1 + " b1= " + this.b1);
    }

}

Child.java

public class Child extends Parent {
        public Child(int c1, int d1){
        super(c1,d1);
    }

    public static void main(String[] args) {
        Parent pa = new Parent(1,2);
        pa.print();
        Child ch = new Child(5,6);
        ch.print();
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文