我正在尝试扩展二进制数类,但是当我这样做时,我无法访问某些基类函数,例如我试图在 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;
}
}
类 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;
}
}
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();
}
}