簡體   English   中英

如何在擴展 class 中使用方法並在主 class 中使用它們

[英]How to use methods in extended class and use them in main class

我正在嘗試創建一個擴展形狀 class 的方法並使用它,但它不起作用。 這是我正在嘗試做的代碼:

public class Try extends Application {
    @Override
    public void start(Stage arg0) throws Exception {
        Rectangle rectangle = new customRectangles(5);
        int i = rectangle.getObject();   //This doesn't work
        System.out.println(i);
    }
    
    class customRectangles extends Rectangle {
        int object = 0;

        customRectangles(int object){
            this.object = object;
        }

        public int getObject(){
            return object;
        }
    }
}

這不是完整的代碼,其中大部分已被刪除。 我已經搜索了 web 並找不到任何東西所以來問這個問題。 請任何幫助。

Rectangle rectangle = new customRectangles(5);

意味着變量矩形將只是一個矩形。 因此只會看到 Rectangle 方法。 你想完成什么:

customRectangles rectangle = new customRectangles(5);

(類名應以大寫字母開頭)

暫無
暫無

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

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