Java中的接口,类和构造函数 [英] Interfaces, classes and constructors in java
问题描述
这里有一些东西让我讨厌接口和类。
Here's something that bothers me regarding Interfaces and classes.
我试图做一个名为IPAddressString类的IPAddress接口的实现。
Ipadress包含四个部分。
I'm trying to do an implemataion for an interface called IPAddress by a class named IPAddressString. Ipadress contains four parts.
我正在写一个名为mask的方法,用给定的掩码掩盖当前地址。掩蔽
操作是对地址的所有四个部分的按位和操作。你通过一个我写了名为getOctet的方法得到所有的四个部分。 (你可以在下面看到)。
I'm writing a method named mask which mask the current address with the given one. The masking operation is a bitwise 'and' operation on all four parts of the address. You get all the four parts by a method that I wrote named getOctet. (you can see below).
好吧,所以我需要掩盖我的this.IpAdress它与我写了一个新的一般IPAddress类。
Ok, so I need to mask my this.IpAdress which with it I wrote my class with a new general IPAddress.
在写面具时,我面临一个问题。我计算了4个整数,与他们我想返回一个新的IPAddress类型。为了做到这一点,我需要使用我的构造函数返回
IPAddressString类型,并且猥亵我不能从IPAddressString到IPAddress。
While writing the mask I face a problem. I calculated 4 integers that with them I want to return a new IPAddress type. In order to do that I need to use my constructer which returns IPAddressString type, and obivously I cannot convert from IPAddressString to IPAddress.
我迷路了。我该怎么办?为什么我的结构不是没有好处呢? IPAddressString是IPAddress的子类型吗?
I'm lost. what should I do? why Isn't my constructure is no good for this? Isn't IPAddressString a sub-type of IPAddress?
这里的代码将使其更简单:
Here's the code which will make it simpler:
这是接口:
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,但我应该使用我的结构。
The problem again is with the method mask. I need to return a new IPAddress, but I should use my constructure. what am I missing?
谢谢。
推荐答案
在 IPAddressString
中实现 IPAddress
。虽然您在 IPAddressString
类中实现 IPAddress
接口的所有方法,但您不会告诉编译器[显然不能猜测你的意图]。
You can implement IPAddress
in IPAddressString
. Although you are implementing all the methods of IPAddress
interface in your IPAddressString
class, you are not telling this to the compiler [which clearly cannot guess your intentions].
将你的类的定义改为:
class IPAddressString implements IPAddress
这应该解决转换的问题。
This should solve the problem in conversion.
现在这行:
IPAddress n1= new IPAddressString (n0,n1,n2,n3);
不会给你提出问题,因为层次结构建立。您可以和平地返回 n1
。
will not give you problems since the hierarchy is established. And you can peacefully return n1
.
这篇关于Java中的接口,类和构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!