簡體   English   中英

創建對象時如何覆蓋類的功能

[英]how to override function of class while creating its object

我搜索了stackoverflow和Google,但聽不懂。 這是我的代碼。 Class Test和Class Tester在不同的程序包中,我無法擴展Test類

我想在Tester類的get()中重寫class test的token()

public class Test{
    private String d;
    private String f;

    Test(String d, String f) {
        this. d = d;
        this.f = f;
    }

    public String token(String a, String b) {
        String c; 
        // token() logic
        return c;
    }

}

public class Tester{

    public void get() {
        Test t = new Test (“sample1”, “sample2”);
       // get() logic
    }

}

你這樣子做

Test t = new Test("abc","xyz"){
  @Override
  public String token(String a, String b) {
    // New logic here
  }      
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM