Is there a way to store a object immediately to a another classes list?

Rezvan :

Title, I am thinking something like this

Shapes s = new Shapes();
Circle c = new Circle("Our", "Parameters");
Rectangle r = new Rectangle("Our", "Parameters");

Shapes.addToList(c); // I don't want to do this for every object I create
Shapes.addToList(r); 

So I want to add these object to the 'Shapes' list when I initialize a object.

Any ideas?

Joseph Larson :

If Circle and Square both inherit from Shapes, then in Shapes constructor, you could automatically add to your list. In the destructor, you'd want to remove it.import java.util.*;

public class Shapes {
    public static List<Shapes> allMyShapes = new ArrayList<Shapes>();

    public Shapes() {
        addToShapes(this);
    }

    static public void addToShapes(Shapes myShape) {
        allMyShapes.add(myShape);
    }

    static public void dump() {
        System.out.printf("Number of shapes stored: %d\n", allMyShapes.size());
    }
}

public class Foo {
    public static void main(String[] args) {
        Shapes myFirstShape = new Shapes();

        Circle myCircle = new Circle();
        Square mySquare = new Square();

        Shapes.dump();
    }
}

I didn't implement the destructor. This only works, of course, if you always want this behavior.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

What's the best way to store a huge Map object populated at runtime to be reused by another tool?

Is there a way to connect a model object with another object?

is there way to convert object into another class in ruby

C# Sorting a List of an Object which contains another List of an Object

How to store number of object element of array in another array?

How to retrieve the object in jComboBox that is binded in database table and store it to another table

An efficient way of removing objects from an indexedDB object store that are missing a property

Simple way to expose global list of string in several fakes classes in javascript

most pythonic way to use a value from a list as an index to another list

Extract ARIMA object from list and store in dataframe in R

how can i store List<object> in sharedpreferences using provider?

How to build a new list base on another file and sort it in a certain way

Is there a way to modify a matrix value based on a value in another list?

Is there a simple way of handling nested Dictionary<string, object> types where object is either string or another Dictionary<string, object>?

What is the best way to get the percentage for an object in linq list and map it to JSON?

Object A has a List of Object B, Proper Way for Object B to Alter Class A?

Spring JPA: ManyToMany relationship not stored in database when reuse the property to store another object

Accessing classes that are in another DLL?

How to match keys in a dictionary with the elements in a list and store those key values to another dictionary in python?

Efficient way to store data

Find list of items(fruits) from an array in an array of object and calculate its quantity and push into another array as an object

If I edit an object in a list created with ToList in EF 6 will SaveChanges persist edits to data store

Thymeleaf Without Object Classes?

Sharing an object between classes

Python object store with Redis

Any way to assign a variable (not an object property) another variable's value by reference in JavaScript?

Is there a way to set the value of one key as the same as the value of another key in the same object in JS?

Trying to learn generics, is there a way to see if one generic object is bigger or smaller then another?

How to store a List in Flutter?

TOP 一覧

  1. 1

    Unity:未知のスクリプトをGameObject(カスタムエディター)に動的にアタッチする方法

  2. 2

    セレンのモデルダイアログからテキストを抽出するにはどうすればよいですか?

  3. 3

    Ansibleで複数行のシェルスクリプトを実行する方法

  4. 4

    tkinterウィンドウを閉じてもPythonプログラムが終了しない

  5. 5

    Crashlytics:コンパイラー生成とはどういう意味ですか?

  6. 6

    GoDaddyでのCKEditorとKCfinderの画像プレビュー

  7. 7

    Windows 10 Pro 1709を1803、1809、または1903に更新しますか?

  8. 8

    Chromeウェブアプリのウェブビューの高さの問題

  9. 9

    モーダルダイアログを自動的に閉じる-サーバーコードが完了したら、Googleスプレッドシートのダイアログを閉じます

  10. 10

    Windows 10の起動時間:以前は20秒でしたが、現在は6〜8倍になっています

  11. 11

    Reactでclsxを使用する方法

  12. 12

    ファイル内の2つのマーカー間のテキストを、別のファイルのテキストのセクションに置き換えるにはどうすればよいですか?

  13. 13

    MLでのデータ前処理の背後にある直感

  14. 14

    グラフからテーブルに条件付き書式を適用するにはどうすればよいですか?

  15. 15

    Pythonを使用して同じ列の同じ値の間の時差を取得する方法

  16. 16

    mutate_allとifelseを組み合わせるにはどうすればよいですか

  17. 17

    ネットワークグラフで、ネットワークコンポーネントにカーソルを合わせたときに、それらを強調表示するにはどうすればよいですか?

  18. 18

    テキストフィールドの値に基づいて UIslider を移動します

  19. 19

    BLOBストレージからデータを読み取り、Azure関数アプリを使用してデータにアクセスする方法

  20. 20

    PowerShellの分割ファイルへのヘッダーの追加

  21. 21

    ソートされた検索、ターゲット値未満の数をカウント

ホットタグ

アーカイブ