我只是在学习接口及其在java中的意义,是否有必要为内部类创建接口,例如我在下面创建的程序?此类的目的只是成为政府的模糊原型以及它如何以最简单的形式运作。我创建了几种添加法律、参议员和国会成员的方法。这段代码是否适用于我想要完成的工作?
public class usaGov
{
private int sizeOfSenate = 105;
private int sizeOfCongress = 308;
private int amtOfLaws = 0;
private int law = 0;
void addSenateMember(int numberOfSenators)
{
sizeOfSenate = sizeOfSenate + numberOfSenators;
}
void addCongressMember(int numberOfCongressMembers)
{
sizeOfCongress = sizeOfCongress + numberOfCongressMembers;
}
void removeSenateMember(int numberOfSenators)
{
sizeOfSenate = sizeOfSenate - numberOfSenators;
}
void removeCongressMember (int numberOfCongressMembers)
{
sizeOfCongress = sizeOfCongress - numberOfCongressMembers;
}
void passLaw()
{
if (law == 3)
{
amtOfLaws++;
}
}
private class Senate
{
void senateYes()
{
int yes = 1;
law = law + yes;
}
void senateNo()
{
int no = -1;
law = law + no;
}
}
private class Congress
{
void congressYes()
{
int yes = 1;
law = law + yes;
}
void congressNo()
{
int no = -1;
law = law + no;
}
}
private class President
{
void presidentYes()
{
int yes = 1;
law = law + yes;
}
void presidentNo()
{
int no = -1;
law = law + no;
}
}
}
这是我的界面:
public interface americanGov
{
void addSenateMember(int numberOfSenators);
void addCongressMember(int numberOfCongressMembers);
void removeSenateMember(int numberOfSenators);
void removeCongressMember (int numberOfCongressMembers);
}