使用super的java类和子类
我有3个班级:班级项目:使用super的java类和子类,java,Java,我有3个班级:班级项目: public class Item { public String name,constructor ; public int year; public double price ; public Item(String name,int year,double price,String constructor){ this.name = name ; this.year = year ;
public class Item {
public String name,constructor ;
public int year;
public double price ;
public Item(String name,int year,double price,String constructor){
this.name = name ;
this.year = year ;
this.price = price ;
this.constructor = constructor ;
}
public String getName(){
return name ;
}
public int getYear(){
return year ;
}
public double getPrice(){
return price ;
}
public String getConstructor(){
return constructor ;
}
public void setName(String name){
this.name = name ;
}
public void setYear(int year ){
this.year = year ;
}
public void setPrice(double price){
this.price = price ;
}
public void setConstructor(String constructor){
this.constructor = constructor ;
}
}
类硬件:
public class Hardware extends Item {
private String proccesor;
private String motherboard;
private String RamMemory;
private String drive;
public Hardware(String name, int year,double price, String constructor, String proccesor,String motherboard, String RamMemory,
String drive) {
super(name, year, price, constructor);
this.proccesor= proccesor;
this.motherboard= motherboard;
this.RamMemory= RamMemory;
this.drive= drive;
}
public void setProccesor (String proccesor) {
this.proccesor= proccesor;
}
public void setMotherboard(String motherboard){
this.motherboard= motherboard;
}
public void setRam(String RamMemory){
this.RamMemory= RamMemory;
}
public void setDrive(String drive){
this.drive= drive;
}
public String getProccesor() {
return proccesor;
}
public String getMotherboard() {
return motherboard;
}
public String getRamMemory() {
return RamMemory;
}
public String getDrive() {
return drive;
}
}
和班级主任:
public class Proccesor extends Hardware {
private double ghz;
private int cores;
public Proccesor (String name, int year,double price, String constructor, double ghz, int cores) {
super(super(name,year, price, constructor));
this.ghz= ghz;
this.cores= cores;
}
public void setGhz(double ghz){
this.ghz= ghz;
}
public void setCores(int cores) {
this.cores = cores;
}
public double getGHz() {
return ghz;
}
public int getCores() {
return cores;
}
}
我需要将Item类的构造函数调用到Proccesor类中,但我不知道如何做。命令super(super(名称、年份、价格、建造商))给了我一个错误:
Proccesor.java:7: error: call to super must be first statement in constructor
super(super(name,year, price, constructor));
^
1 error
只要调用super()
您应该只从类处理器调用您的超类的构造函数,然后它将负责调用项
类的构造函数
如果需要绕过硬件的构造函数,那么硬件应该有一个受保护的构造函数,该构造函数被设计为调用项构造函数,并且可以由子类调用
public Hardware(String name, int year,double price, String constructor, String proccesor,String motherboard, String RamMemory, String drive) {
this(name, year, price, constructor);
this.proccesor= proccesor;
this.motherboard= motherboard;
this.RamMemory= RamMemory;
this.drive= drive;
}
protected Hardware(String name, int year,double price, String constructor) {
super(name, year, price, constructor);
}
如你所见:
- 一个类可以有多个构造函数
- 可以将公共代码放在其中一个(通常是最简单的)中,然后从其他人那里调用它,以避免重复代码消除
这就是所说的(名称、年份、价格、构造函数)代码>语句的意思
更好地缩进代码,这很难阅读。不要使用super(super(名称、年份、价格、构造函数));
只需使用super(name、year、price、constructor),它将依次调用其他所有具有super的构造函数。如上所述,super(super(…
)不是有效的java语法。现在,考虑一下:
查看您的硬件构造函数
只有此构造函数,您才能通过提供所有构造函数参数(包括字符串处理器
和主板
)显式允许创建硬件
对象
现在,由于您的处理器
也是一个硬件
,因此只能通过向其超级(…)
构造函数提供所有必要的信息来构造它
您可以做什么,例如:
在硬件
中创建另一个构造函数,这将允许您使用较少的参数构造对象,并从处理器
的构造函数调用它
或
调用super(名称、年份、价格、构造函数、处理器、主板、内存、驱动器)
(使用完整参数列表)在构造处理器
时,可能会为处理器
中未使用的参数传递一些默认值,例如驱动器
你认为super(super(…)
会做什么?