我正在尝试扩展二进制数类,但是当我无法访问某些基类函数时,例如我试图在main中运行的最后一个函数
import java.util.*;
class BinaryNumber
{
private boolean b[];
public void setBit(int index, boolean value)
{
b[index] = value;
}
public boolean getBit(int index)
{
return b[index];
}
public void clear()
{
for (int i = 0; i < 8; i++)
b[i] = false;
}
public BinaryNumber()
{
b = new boolean[8];
for (int i = 0; i < 8; i++)
{
b[i] = false;
}
}
public void inputNumber(int a)
{
if (a > 11111111)
{
System.out.println(" Exception Number to Large Nothing Entered ");
}
else
{
int number = a;
int c = 7;
int digit = 0;
for (int i = 0; i < 8; i++)
{
digit = number % 10;
if (digit == 1)
{
b[c] = true;
}
else if (digit == 0)
{
b[c] = false;
}
else
{
System.out.println(" Only Binary Number Accepted ");
clear();
i = 8; // break
}
number = number / 10;
c--;
}
}
}
public void outputNumber()
{
for (int i = 0; i < 8; i++)
{
if (b[i] == true)
{
System.out.print("1");
}
else
{
System.out.print("0");
}
}
System.out.println("");
}
public BinaryNumber ANDNumbers(BinaryNumber second)
{
BinaryNumber result = new BinaryNumber();
boolean temp = false;
for (int i = 0; i < 8; i++)
{
result.setBit(i, (b[i] && second.b[i]));
}
return result;
}
}
class ExtendedBinaryNumber
class ExtendedBinaryNumber extends BinaryNumber
{
public ExtendedBinaryNumber binaryAdd(ExtendedBinaryNumber A)
{
ExtendedBinaryNumber result = new ExtendedBinaryNumber();
boolean carry = false;
for (int i = 7; i >= 0; i--)
{
if (getBit(i) == true)
{
if (A.getBit(i) == false)
{
if (carry == false)
{
carry = false;
result.setBit(i, true);
}
else
{
carry = true;
result.setBit(i, false);
}
}
else
{
if (carry == false)
{
carry = true;
result.setBit(i, false);
}
else
{
carry = true;
result.setBit(i, true);
}
}
}
else
{
if (A.getBit(i) == false)
{
if (carry == false)
{
carry = false;
result.setBit(i, false);
}
else
{
carry = true;
result.setBit(i, true);
carry = false;
}
}
else
{
if (carry == false)
{
carry = false;
result.setBit(i, true);
}
else
{
carry = true;
result.setBit(i, false);
}
}
}
}
return result;
}
}
class Q6
class Q6
{
public static void main(String args[])
{
ExtendedBinaryNumber numA = new ExtendedBinaryNumber();
int a = Integer.parseInt("10010111");
numA.inputNumber(a);
numA.outputNumber();
ExtendedBinaryNumber numB = new ExtendedBinaryNumber();
int b = Integer.parseInt("10010101");
numB.inputNumber(b);
numB.outputNumber();
System.out.println("");
// BinaryNumber c=numA.ANDNumbers(numB);
// c.outputNumber();
// ExtendedBinaryNumber dd=numA.binaryAdd(numB);
// dd.outputNumber();
ExtendedBinaryNumber dde = numA.ANDNumbers(numB);
dde.outputNumber();
}
}
答案 0 :(得分:0)
numA.ANDNumbers
返回BinaryNumber
,您将其分配给ExtendedBinaryNumber
变量。默认情况下,您应该进行向下转换以允许此操作,但请注意,您在执行该行时确实返回了ExtendedBinaryNumber
实例,否则您将拥有ClassCastException
。
解决此问题的最佳方法是:
BinaryNumber dde=numA.ANDNumbers(numB);
dde.outputNumber();
如果你真的想在Java中使用覆盖的权限,你可以覆盖ANDNumbers
类中的ExtendedBinaryNumber
方法并返回一个协变类型,在这种情况下将是{{1} }:
ExtendedBinaryNumber
通过这样做,现在你的线可以在
之后编译并快乐地运行class ExtendedBinaryNumber {
public ExtendedBinaryNumber binaryAdd(ExtendedBinaryNumber A) {
//here goes your actual code...
}
@Override
public ExtendedBinaryNumber ANDNumbers(BinaryNumber second) {
//fancy override implemententation that returns ExtendedBinaryNumber instance
}
}
答案 1 :(得分:0)
ANDNumbers
方法返回BinaryNumber
,因此行ExtendedBinaryNumber dde=numA.ANDNumbers(numB);
答案 2 :(得分:0)
没有覆盖问题。除了方法的返回类型之外你没事。
变化
ExtendedBinaryNumber dde = numA.ANDNumbers(numB);
到
BinaryNumber dde = numA.ANDNumbers(numB);
ANDNumbers
方法返回BinaryNumber
。
答案 3 :(得分:0)
@override
public void outputNumber() {
super.outputNumber();
}
@override
public BinaryNumber ANDNumbers(BinaryNumber second) {
return super.BinaryNumber();
}
类Q6
BinaryNumber c=numA.ANDNumbers(numB);
// c.outputNumber();
ExtendedBinaryNumber dd=numA.binaryAdd(numB);
dd.outputNumber();