下面是關於接口和類的一些問題。java中的接口,類和構造函數
我正在嘗試通過名爲IPAddressString的類爲名爲IPAddress的接口執行一個實現。 Ipadress包含四個部分。
我正在寫一個名爲掩碼的方法,它掩蓋了給定的地址。掩碼 操作是對地址的所有四個部分按位和'操作。您通過我寫的名爲getOctet的方法獲得了所有這四個部分。 (你可以在下面看到)。
好的,所以我需要掩蓋我的this.IpAdress,它與我一起寫了一個新的通用IPAddress。
寫封面時我面臨一個問題。我計算了4個與他們相同的整數,我想返回一個新的IPAddress類型。爲了做到這一點,我需要使用我的constructer返回 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);
}
}
問題再次與方法面具。我需要返回一個新的IP地址,但我應該使用我的結構。我錯過了什麼?
謝謝。
'IPAddressString'需要*實現*'IPAddress' – Tedil 2011-04-07 20:49:10
您是否試過實現IPAddress? – RedSoxFan 2011-04-07 20:50:05
@泰迪爾:是的,我寫了相反的嗎? – 2011-04-07 20:50:08