Java: Adding fields and methods to existing Class?

≯℡__Kan透↙ 提交于 2019-11-29 16:02:41

问题


Is there, in Java, a way to add some fields and methods to an existing class? What I want is that I have a class imported to my code, and I need to add some fields, derived from the existing fields, and their returning methods. Is there any way to do this?


回答1:


You can create a class that extends the one you wish to add functionality to:

public class sub extends Original{
 ...
}

To access any of the private variables in the superclass, if there aren't getter methods, you can change them from "private" to "protected" and be able to reference them normally.

Hope that helps!




回答2:


You can extend classes in Java. For Example:

public class A {

  private String name;

  public A(String name){
    this.name = name;
  }

  public String getName(){
    return this.name;
  }

  public void setName(String name) {
    this.name = name;
  }

}

public class B extends A {
  private String title;

  public B(String name, String title){
    super(name); //calls the constructor in the parent class to initialize the name
    this.title= title;
  }      

  public String getTitle(){
    return this.title;
  }

  public void setTitle(String title) {
    this.title= title;
  }
}

Now instances of B can access the public fields in A:

B b = new B("Test");
String name = b.getName();
String title = b.getTitle();

For more detailed tutorial take a look at Inheritance (The Java Tutorials > Learning the Java Language > Interfaces and Inheritance).

Edit: If class A has a constructor like:

public A (String name, String name2){
  this.name = name;
  this.name2 = name2;
}

then in class B you have:

public B(String name, String name2, String title){
  super(name, name2); //calls the constructor in the A 
  this.title= title;
}



回答3:


The examples only really apply if the class you're extending isn't final. For example, you cannot extend java.lang.String using this method. There are however other ways, such as using byte code injection using CGLIB, ASM or AOP.




回答4:


Assuming this question is asking about the equivalent of C# extension methods or JavaScript prototypes then technically it is possible as this one thing that Groovy does a lot. Groovy compiles Java and can extend any Java class, even final ones. Groovy has metaClass to add properties and methods (prototypes) such as:

// Define new extension method
String.metaClass.goForIt = { return "hello ${delegate}" }

// Call it on a String
"Paul".goForIt() // returns "hello Paul"

// Create new property
String.metaClass.num = 123

// Use it - clever even on constants
"Paul".num // returns 123
"Paul".num = 999 // sets to 999
"fred".num // returns 123

I could explain how to do the same way as Groovy does, but maybe that would be too much for the poster. If they like, I can research and explain.



来源:https://stackoverflow.com/questions/13730924/java-adding-fields-and-methods-to-existing-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!