是否可以在@Override Java的其他类中使用其他类变量

时间:2018-11-29 19:07:14

标签: java

我想知道@Override中是否可以使用其他类变量? 这是我的第一堂课。我尝试使用setters和getters,但仍然收到错误:

at exercise4.BinaryTree$Node.access$000(BinaryTree.java:14)
    at exercise4.BinaryTree$Itr.next(BinaryTree.java:205)
    at exercise4.BinaryTree$Itr.next(BinaryTree.java:184)
    at exercise4.Exercise4.main(Exercise4.java:34)

这是我的另一堂课:

public class Darbuotojas {
String dVardas;
String dPavarde;
int dAmzius;
int dPatirtis;

Programuotojas programuotojas = new Programuotojas();
String check = programuotojas.getDarboKalba();

Darbuotojas() { }
public Darbuotojas(String dVardas, String dPavarde, int dAmzius, int dPatirtis) {
    this.dVardas = dVardas;
    this.dPavarde = dPavarde;
    this.dAmzius = dAmzius;
    this.dPatirtis = dPatirtis;
}

@Override
public String toString() {
    return dVardas + " " + dPavarde + " " + dAmzius + " " + dPatirtis + " " + check;
}}

正如我所见,我为你们提出了一个不好的,无法理解的问题。我会尽力解释。我的父亲类Darbuotojas具有4个参数,在子类Programuotojas中,我添加了另一个参数-darboKalba。因此,我想从Override中返回Darbuotojas的这4个参数,再加上Programuotojas的一个。

public class Programuotojas extends Darbuotojas {
Programuotojas(){}

public String getDarboKalba() {
    return darboKalba;
}

public void setDarboKalba(String darboKalba) {
    this.darboKalba = darboKalba;
}

public String darboKalba;
public Programuotojas(String dVardas, String dPavarde, int dAmzius, int dPatirtis, String darboKalba) {
    super(dVardas, dPavarde, dAmzius, dPatirtis);
    this.darboKalba = darboKalba;
}

1 个答案:

答案 0 :(得分:0)

反射可能很棘手,我不能保证它会在所有情况下都起作用。这对我来说足够好。

package quicktest;

import java.lang.reflect.Field;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Brenden
 */
public class FieldDumpTest {

   public static void main( String[] args ) {
      System.out.println( new FieldDump3() );
   }

   public final String topTestString = "This is a test.";
   protected int topInt = 11;
   float topFloat = 123.456f;
   private short topShort = Short.MAX_VALUE;

   @Override
   public String toString() {
      StringBuilder str = new StringBuilder();
      Class<?> type = this.getClass();
      do {
         str.append( type.getName() );
         str.append( " {" );
         Field[] fields = type.getDeclaredFields();
         for( int i = 0; i < fields.length; i++ ) {
            Field field = fields[i];
            str.append( field.getName() );
            str.append( ":" );
            try {
               str.append( field.get( this ) );
            } catch( IllegalAccessException | IllegalArgumentException ex ) {
               str.append( "<NO ACCESS>" );
            }
            str.append( ", " );
         }
         str.append(  "}  " );
         type = type.getSuperclass();
      } while( type != null );
      return str.toString();
   }

}

class FieldDump2 extends FieldDumpTest {
   private String test  = "testtest";
}

class FieldDump3 extends FieldDump2 {
   int ten = 10;
   protected double fortytwo = 42.0;
}

输出:

run:
quicktest.FieldDump3 {ten:10, fortytwo:42.0, }  quicktest.FieldDump2 {test:<NO ACCESS>, }  quicktest.FieldDumpTest {topTestString:This is a test., topInt:11, topFloat:123.456, topShort:32767, }  java.lang.Object {}  
BUILD SUCCESSFUL (total time: 0 seconds)