![](/img/trans.png)
[英]How to declare a constructor in a subclass when the superClass does not have a constructor
[英]How to declare subclass constructor in java
我有一個抽象類Entity.java。 我還有一個擴展Entity類的TransportUnit.java類。 實體將使用String名稱和int lifePoints構造。 運輸單元應使用相同的結構,另外還有一個空套。 我的問題是如何編寫TransportUnit類的構造函數,為什么? 提前致謝。
您可以使用與普通構造函數相同的方式編寫它,只需調用super()
Set s;
public TransportUnit(String name, int lifePoints){
super(name, lifePoints);
s = new HashSet(); //or other type of set
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.