【问题标题】:Interfaces, classes and constructors in javajava中的接口、类和构造函数
【发布时间】:2011-04-07 20:45:44
【问题描述】:

关于接口和类,我有一些困扰。

我正在尝试通过一个名为 IPAddressString 的类为一个名为 IPAddress 的接口执行一个实现。 Ipadress 包含四个部分。

我正在编写一个名为 mask 的方法,它用给定的地址屏蔽当前地址。掩蔽 操作是对地址的所有四个部分的按位“与”操作。您可以通过我编写的名为 getOc​​tet 的方法获得所有四个部分。 (您可以在下面看到)。

好的,所以我需要屏蔽我的 this.IpAdress,我用它用新的通用 IPAddress 编写了我的课程。

在写面具时,我遇到了一个问题。我计算了 4 个整数,我想用它们返回一个新的 IPAddress 类型。为此,我需要使用返回的构造函数 IPAddressString 类型,显然我无法从 IPAddressString 转换为 IPAddress。

我迷路了。我该怎么办?为什么我的结构不适合这个? IPAddressString 不是 IPAddress 的子类型吗?

下面的代码会让它变得更简单:

界面如下:

public interface IPAddress {

    /**
     * Returns a string representation of the IP address, e.g. "192.168.0.1"
     */
    public String toString();

    /**
     * Compares this IPAddress to the specified object
     * 
     * @param other
     *            the IPAddress to compare this string against
     * @return <code>true</code> if both IPAddress objects represent the same IP
     *         address, <code>false</code> otherwise.
     */
    public boolean equals(IPAddress other);

    /**
     * Returns one of the four parts of the IP address. The parts are indexed
     * from left to right. For example, in the IP address 192.168.0.1 part 0 is
     * 192, part 1 is 168, part 2 is 0 and part 3 is 1.
     * 
     * @param index
     *            The index of the IP address part (0, 1, 2 or 3)
     * @return The value of the specified part.
     */
    public int getOctet(int index);

    /**
     * Returns whether this address is a private network address. There are
     * three ranges of addresses reserved for 'private networks' 10.0.0.0 -
     * 10.255.255.255, 172.16.0.0 - 172.31.255.255 and 192.168.0.0 -
     * 192.168.255.255
     * 
     * @return <code>true</code> if this address is in one of the private
     *         network address ranges, <code>false</code> otherwise.
     * @see <a href="http://en.wikipedia.org/wiki/IPv4#Private_networks">Private Networks</a>
     */
    public boolean isPrivateNetwork();

    /**
     * Mask the current address with the given one. The masking operation is a
     * bitwise 'and' operation on all four parts of the address.
     * 
     * @param mask
     *            the IP address with which to mask
     * @return A new IP address representing the result of the mask operation.
     *         The current address is not modified.
     */
    public IPAddress mask(IPAddress mask);
}

这是我的课:

public class IPAddressString {

    private String IpAdress;

    public IPAddressString(int num1, int num2, int num3, int num4) {
        this.IpAdress = num1 + "." + num2 + "." + num3 + "." + num4;

    }


    public String toString() {
        return this.IpAdress;

    }

    public boolean equals(IPAddress other) {
        return ((other.toString()).equals(IpAdress));
    }

    public int getOctet(int index) {

        StringBuffer buf = new StringBuffer();
        int point = index;
        int countPoints = 0;

        for (int i = 0; i <= IpAdress.length() - 1; i++) {
            if ((IpAdress.charAt(i)) == '.') {
                countPoints++;

            }
            if ((countPoints == point) && IpAdress.charAt(i) != '.') {
                buf.append(IpAdress.charAt(i));
            }

        }
        String result = buf.toString();
        return Integer.parseInt(result);
    }

    public boolean isPrivateNetwork() {

        if (getOctet(0) == 10) {
            return true;
        }

        if (getOctet(0) == 172) {
            if (getOctet(1) >= 16 && getOctet(1) <= 31) {
                return true;
            }
        }

        if (getOctet(0) == 192) {
            if (getOctet(1) == 168) {
                return true;
            }
        }

        return false;

    }


    public IPAddress mask(IPAddress mask){
        int n0= mask.getOctet(0) & getOctet(0);
        int n1= mask.getOctet(1) & getOctet(1);
        int n2=mask.getOctet(2) & getOctet(2);
        int n3=mask.getOctet(3) & getOctet(3);



         IPAddress n1= new IPAddressString (n0,n1,n2,n3);
    }

}

问题又出在方法掩码上。我需要返回一个新的 IPAddress,但我应该使用我的结构。我错过了什么?

谢谢。

【问题讨论】:

  • IPAddressString 需要实现 IPAddress
  • 你尝试过实现 IPAddress 吗?
  • @Tedil:是的,我是不是写反了?
  • 你根本没有实现。
  • @RedSoxFan:我不明白。我用 IPAddressString 实现 IPAddress 方法。

标签: java class interface constructor


【解决方案1】:

您可以在IPAddressString 中实现IPAddress。尽管您在IPAddressString 类中实现了IPAddress 接口的所有方法,但您并没有将这些告诉编译器[它显然无法猜测您的意图]。

将类的定义更改为:

class IPAddressString implements IPAddress

这应该可以解决转换问题。

现在这一行:

IPAddress n1= new IPAddressString (n0,n1,n2,n3);

由于层次结构已经建立,不会给您带来问题。并且可以安心返回n1

【讨论】:

    【解决方案2】:

    一个简单的公共类IPAddressString实现IPAddress就可以解决这个问题。

    另一个让我眼前一亮的建议:

    在接口中声明 equals 和 toString() 方法没有任何意义,因为每个对象都已经有了它们。不实现就不会出现编译错误。

    此外,equals 方法必须始终具有签名 boolean equals(Object other),因为只有这样它才会覆盖 Object 的方法并始终正确调用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-13
      • 2010-11-11
      • 2017-04-10
      • 2012-02-17
      • 2011-12-20
      • 2012-10-27
      相关资源
      最近更新 更多