这段Java代码是什么意思?

发布于 2024-10-02 11:09:35 字数 1847 浏览 1 评论 0原文

首先,我不是要求任何人做我的作业。我只是对这个 Java 语法感到困惑。

下面是作业描述和我必须实现的两个类。第一个仅供参考。 第二个是我遇到问题的。

能够使用(理论上)没有最大值的数字进行编程是计算机科学的许多应用中的必要条件。您将编写一系列类来开始执行此任务。您的最终课程将允许您表示并至少添加任意长度的二进制数。

// You are to write (implement) this class exactly as dictated by the following list of class members.
public abstract AbstractBit: 

  private boolean bit;

  public abstract AbstractBit clone(); 
  public abstract AbstractBit addBits(AbstractBit guest);
  public abstract AbstractBit addBits(AbstractBit guest1, AbstractBit guest2);
  public abstract AbstractBit carryBit(AbstractBit guest);
  public abstract AbstractBit carryBit(AbstractBit guest1, AbstractBit guest2);

  protected void setBit(boolean value)

  public boolean getBit()

  public AbstractBit()

  public AbstractBit(boolean value)

  public AbstractBit(AbstractBit guest)

  public boolean equals(AbstractBit guest)

  public String toString()

这些到底是什么?

为什么要添加 BinaryBit 类型的静态类变量?这不是会以某种方式递归吗? (参见标有 ?????? 的行)

// You are to write (implement) this class exactly as dictated by the following list of class members.
public BinaryBit extends AbstractBit: 

  public static final BinaryBit zero = new BinaryBit(false);  // ????
  public static final BinaryBit one = new BinaryBit(true);    // ????

  public BinaryBit()

  public BinaryBit(boolean bit)

  public BinaryBit(int bit)

  public BinaryBit(BinaryBit guest)

  public BinaryBit clone()

  public boolean equals(BinaryBit guest)

  public String toString()

  public AbstractBit addBits(AbstractBit guest)

  public AbstractBit addBits(AbstractBit guest1, AbstractBit guest2)

  public AbstractBit carryBit(AbstractBit guest)

  public AbstractBit carryBit(AbstractBit guest1, AbstractBit guest2)

First off, I'm not asking for anyone to do my homework. I'm simply confused with this Java Syntax.

Below is the assignment description and two classes I have to implement. The first one is here for reference only. The second one is the one I'm having trouble with.

Being able to program with numbers that do not (in theory) have a maximum value is a necessity in many applications of computer science. You are going to write a series of classes to get started on this task. Your final class will allow you to represent and at least add Binary numbers of arbitrary length.

// You are to write (implement) this class exactly as dictated by the following list of class members.
public abstract AbstractBit: 

  private boolean bit;

  public abstract AbstractBit clone(); 
  public abstract AbstractBit addBits(AbstractBit guest);
  public abstract AbstractBit addBits(AbstractBit guest1, AbstractBit guest2);
  public abstract AbstractBit carryBit(AbstractBit guest);
  public abstract AbstractBit carryBit(AbstractBit guest1, AbstractBit guest2);

  protected void setBit(boolean value)

  public boolean getBit()

  public AbstractBit()

  public AbstractBit(boolean value)

  public AbstractBit(AbstractBit guest)

  public boolean equals(AbstractBit guest)

  public String toString()

What the heck are these?

Why am I adding static class variables of type BinaryBit? Isn't this going to be recursive in some way? (See the lines marked with ????)

// You are to write (implement) this class exactly as dictated by the following list of class members.
public BinaryBit extends AbstractBit: 

  public static final BinaryBit zero = new BinaryBit(false);  // ????
  public static final BinaryBit one = new BinaryBit(true);    // ????

  public BinaryBit()

  public BinaryBit(boolean bit)

  public BinaryBit(int bit)

  public BinaryBit(BinaryBit guest)

  public BinaryBit clone()

  public boolean equals(BinaryBit guest)

  public String toString()

  public AbstractBit addBits(AbstractBit guest)

  public AbstractBit addBits(AbstractBit guest1, AbstractBit guest2)

  public AbstractBit carryBit(AbstractBit guest)

  public AbstractBit carryBit(AbstractBit guest1, AbstractBit guest2)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我纯我任性 2024-10-09 11:09:35

这是仅存在有限数量的实例时使用的技术。它们都被声明并被称为常量。传统上将它们称为“零”和“一”,以使其更明显地表明它们是常数。它实际上不是递归的,因为静态变量只创建一次。

That's a technique used when only a limited number of instances exist. They all get declared and then referred to as constants. It's traditional to call them ZERO and ONE to make it more obvious they are constants. It's actually not recursive because static variables are only created once.

暖树树初阳… 2024-10-09 11:09:35

一位只有两个可能的值——零和一。任何给定的零或一都与其他任何零或一一样好。因此,为什么不创建一个“固定的”、不可更改的零和一值,并仅传递对这些值的引用呢?它节省内存和处理器时间。您可能会看到这种方法称为Flyweight 模式

您可以在 Java 自己的 中看到一些这种模式BigInteger 类,具有常见数字的预定义值,例如 ZEROONETEN

A bit has only two possible values -- zero and one. Any given zero or one is just as good as any other. Therefore, why not create a "canned", unchangeable zero and one value, and just pass references to those values around? It saves memory and processor time. You might see this approach called the Flyweight pattern.

You can see some of that pattern in Java's own BigInteger class, with predefined values for common numbers like ZERO, ONE and TEN.

情话已封尘 2024-10-09 11:09:35

由于位只能有两个值:1 和 0,因此您要创建对表示这些值的对象的静态引用。这比每次需要表示 1 或 0 时创建一个新对象更有意义。

since bits can only have two values, 1 and 0, you are creating static references to objects representing those values. It makes more sense than creating a new object every time you need to represent a 1 or 0.

撑一把青伞 2024-10-09 11:09:35

零和一是预定义可在代码中使用的模板的一种方法。另一个例子是 String.empty,它是一个空字符串的实例,这样您就可以执行诸如 if ("" == String.empty) 之类的操作。它只会让你的代码更具可读性。

zero and one are a way of predefining templates that can be used in your code. Another example would be to have String.empty, which is an instantiation of an empty string so that you could do things like if ("" == String.empty). It just makes your code more readable.

旧竹 2024-10-09 11:09:35

不,这不是递归的。它只是意味着类的静态初始化将创建其自身的实例。在创建实例之前,字段为 null,但这应该是一个有争议的问题,除非您在构造函数中使用 zeroone

No, it's not recursive. It just means the static initialization of the class will create instances of itself. Before the instances are created, the fields are null, but that should be a moot point unless you use zero or one in the constructor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文