当前位置: 首页 > 面试题库 >

Java中的接口,类和构造函数

汲利
2023-03-14
问题内容

关于接口和类,这让我感到困扰。

我正在尝试通过名为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,但是我应该使用我的结构。我想念什么?

谢谢。


问题答案:

您可以IPAddress在中实施IPAddressString。尽管您正在实现类中的所有IPAddress接口方法IPAddressString,但您并未将此告知编译器[显然,编译器无法猜测您的意图]。

将类的定义更改为:

class IPAddressString implements IPAddress

这应该解决转换中的问题。

现在这行:

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

建立层次结构不会给您带来问题。您可以和平归来n1



 类似资料:
  • 问题内容: 我知道不可能在接口中定义构造函数。但是我想知道为什么,因为我认为这可能非常有用。 因此,您可以确定为该接口的每种实现定义了类中的某些字段。 例如,考虑以下消息类: 如果为该类定义一个接口,以便我可以有更多实现消息接口的类,则只能定义send方法,而不能定义构造函数。那么,如何确保此类的每个实现都确实有一个接收者集?如果我使用类似的方法,则不能确定是否真的调用了该方法。在构造函数中,我可

  • 我从我正在使用的库中收到以下接口: 如何创建该类的实现?(我需要一个测试模拟)一个自然的实现,构造函数定义为:

  • 问题内容: 在Java中,如果有接口: 然后实现是: 所以我的意思是,如果用户想用构造函数声明实例: 那不可能吗? 问题答案: 那不可能吗? 那就对了。你永远做不到 之后你必须选择一个 类 实现了接口(*) ,如: 为什么? 您可以将接口视为类的属性。比喻是形容词,例如“红色”。例如,创建一个红色的球()或一辆红色的汽车()是很有意义的,但是仅创建“红色”()没有任何意义(“红色代表 什么 ?”)

  • 解析:这里可能会有误区,其实普通的类方法是可以和类名同名的,和构造方法唯一的区分就是,构造方法没有返回值。<

  • 问题内容: 这些来自github上的spring amqp示例,位于https://github.com/SpringSource/spring-amqp- samples.git 这些是 什么类型的Java构造函数?它们是吸气剂和吸气剂的捷径吗? 与此相反 问题答案: 这些构造函数被重载以使用调用另一个构造函数。第一个无参数构造函数使用空参数调用第二个。第二呼叫的第三构造(未示出),其必须采取,

  • 问题内容: 编译该程序时,出现错误- 错误-找不到构造函数Person()。为什么定义Person()是必要的? 问题答案: 创建时,您要同时创建一个。为了确保构造正确,编译器在构造函数中添加了一个隐式调用: 由于没有无参数构造函数,因此失败。 您可以通过以下任一方式解决它 添加对super的显式调用,如下所示: } 或通过将no-arg构造函数添加到: } 通常,编译器还会隐式添加无参数构造函数