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

这是关于接口和类的困扰我的事情。

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

我正在编写一个名为mask的方法,该方法使用给定的掩码屏蔽当前地址。 屏蔽操作是对地址的所有四个部分的按位’和’操作。 你通过我写的名为getOc​​tet的方法获得了所有这四个部分。 (你可以在下面看到)。

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

在写面具时我遇到了问题。 我计算了4个整数,我想要返回一个新的IPAddress类型。 为了做到这一点,我需要使用我的constructer返回IPAddressString类型,并且通常我不能从IPAddressString转换为IPAddress。

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

这是使代码更简单的代码:

这是界面:

public interface IPAddress { /** * Returns a string representation of the IP address, eg "192.168.0.1" */ public String toString(); /** * Compares this IPAddress to the specified object * * @param other * the IPAddress to compare this string against * @return true if both IPAddress objects represent the same IP * address, false 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 true if this address is in one of the private * network address ranges, false otherwise. * @see Private Networks */ 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); } 

这是我的class级:

 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 = 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实现IPAddressString 。 虽然您正在IPAddressString类中实现IPAddress接口的所有方法,但您并没有告诉编译器[显然无法猜测您的意图]。

将类的定义更改为:

 class IPAddressString implements IPAddress 

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

现在这一行:

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

建立层次结构后,不会给您带来任何问题。 你可以安静地返回n1

一个简单的公共类IPAddressString实现IPAddress将解决问题。

另一个跳到我眼前的建议:

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

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