Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:继承和重写方法问题_Java_Inheritance_Methods_Overriding - Fatal编程技术网

Java:继承和重写方法问题

Java:继承和重写方法问题,java,inheritance,methods,overriding,Java,Inheritance,Methods,Overriding,我目前正在研究java教科书中的一个问题,该问题围绕着创建一个电路超类和一个电阻器、串行和并行子类展开。串行和并行类具有ArrayList,这些ArrayList应该由Circuit类的对象填充。每个子类都包含一个getResistance()方法,该方法应该覆盖超类中的getResistance()方法。我的问题是,不管输入如何,只有更改并行类中的实例变量“sum”才能更新getResistance()的结果 另外,我知道这是学校作业,我不希望任何人“为我做家庭作业”,但我仍在学习遗传知识,我

我目前正在研究java教科书中的一个问题,该问题围绕着创建一个电路超类和一个电阻器、串行和并行子类展开。串行和并行类具有ArrayList,这些ArrayList应该由Circuit类的对象填充。每个子类都包含一个getResistance()方法,该方法应该覆盖超类中的getResistance()方法。我的问题是,不管输入如何,只有更改并行类中的实例变量“sum”才能更新getResistance()的结果

另外,我知道这是学校作业,我不希望任何人“为我做家庭作业”,但我仍在学习遗传知识,我想知道我做错了什么,以备将来参考

这是我的主要课程:

public class Circuit
{ 
//this is the superclass and acts just as a container class
 public double getResistance() 
{ 
//I want this method to take an arbitrary value and return it.
 return 0;
}
public void add(Circuit input)
{
//this method is supposed to add a value to an object of the Circuit class
}
public void addAll(ArrayList<Circuit>circuits)
{
//this method is for combining ArrayList and takes in ArrayList
}   
}
公共级电路
{ 
//这是一个超类,充当一个容器类
公众双重阻力()
{ 
//我希望此方法获取任意值并返回它。
返回0;
}
公共无效添加(电路输入)
{
//此方法应向Circuit类的对象添加值
}
public void addAll(ArrayListcircuits)
{
//此方法用于组合ArrayList和接收ArrayList
}   
}
平行子类:

//subclass of Circuit superclass
public class Parallel extends Circuit
{

//this instance variable is of the Circuit class
private ArrayList<Circuit> parallel = new ArrayList<>();
private double resistance; 
private double sum; 

public void add(Circuit input)
{
//int count = 0; 
//adding values to populate ArrayList 
for(Circuit x: parallel)
{
    //setting the xth index value to the value to the input
   x.add(input); 
   //count++;  
}
}

public double getResistance()
{
  //this method overrides the one from the superclass
  //we are the sum of the input resistances of the ArrayList
  if(parallel.size()> 0)
   {
   for(Circuit x: parallel)
   {
    resistance = x.getResistance();
    sum=+ 1/resistance; 
   }
   }
     return sum; 
   }

 public void addAll(Serial series)
 {
 //this method is supposed to add the ArrayList of the Circuit type together
   series.addAll(parallel);  

  }
  }
//电路超类的子类
公共类并行扩展电路
{
//此实例变量属于电路类
private ArrayList parallel=新的ArrayList();
私人双重抵抗;
私人双和;
公共无效添加(电路输入)
{
//整数计数=0;
//添加值以填充ArrayList
用于(电路x:并联)
{
//将第X个索引值设置为输入的值
x、 添加(输入);
//计数++;
}
}
公众双重阻力()
{
//此方法覆盖超类中的方法
//我们是ArrayList的输入电阻之和
if(parallel.size()>0)
{
用于(电路x:并联)
{
电阻=x.getResistance();
总和=+1/电阻;
}
}
回报金额;
}
公共无效添加全部(系列)
{
//此方法应将电路类型的ArrayList添加到一起
串联。addAll(并联);
}
}

与继承或重写无关(它是在更改
sum
变量时没有效果),而是

使用
添加
方法应将输入电路添加到危害该并联电路的电路列表中

现在,它将
并行
列表中的所有电路(全部为0个)添加到另一个
输入
电路中。哎呀,走错路了

由于在当前代码中,没有子电路添加到
并行
列表中,因此getResistence中的循环将永远不会实际运行。。它返回未更改的
sum
变量中的任何值

对于
addAll
,也应进行类似的更改


sum
不应是成员变量;将其保留为局部变量将“修复”在修复上一个问题后遇到的另一个问题。

感谢您的回复!我将add方法更改为您的建议,并将sum变量设置为局部变量,但是我仍然没有得到更新的返回值。@Code.girl更新的add方法是否仍有循环?(它不应该。)哈哈,是的,我从add方法中删除了循环后,就让它运行了。非常感谢你!所以,现在我的问题是,当我尝试对Series或Parralle类使用add()方法时,它只会向第一个索引值添加一个值。当我再次调用add()方法时,它会用新值替换第一个值。@Code.girl从不替换值。因此,一定是其他原因导致了观察到的行为。记住,每个新创建的
电路最初都有0个子电路。第一组代码应该是我的超类,而不是主类!