【问题标题】:Java extends exampleJava 扩展示例
【发布时间】:2011-05-10 10:34:32
【问题描述】:

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

public class Parent {

    private String output = "hallo";

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

}

public class Child extends Parent {

   private String output = "child";

}

【问题讨论】:

    标签: java extends superclass


    【解决方案1】:

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

    父类.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();
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      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;
          }
      }
      

      此外,String 变量不需要是不同的名称,但为了清楚起见,我在这里这样做了。

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

      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";
          }
      }
      

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

      【讨论】:

        【解决方案3】:

        目前你有两个独立的变量,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");
          }
        }
        

        这真的取决于你想做什么。

        【讨论】:

        • @Martin:因为您创建了一个完全独立的变量,并在Child 中设置了 that 的值。 Parent 类不知道 Child 中的变量。
        【解决方案4】:

        Child 无权访问Parentoutput 实例变量,因为它是private。您需要做的是使其成为protected,并在Child 的构造函数中将output 设置为"child"

        也就是说,这两个output变量是不同的。

        如果您在Parent 中将输出更改为protected,您也可以这样做:

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

        【讨论】:

          猜你喜欢
          • 2016-02-29
          • 2012-10-22
          • 2015-07-22
          • 1970-01-01
          • 1970-01-01
          • 2020-08-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多