有什么方法可以从接口中删除totalPermutationCount(),因为该方法的唯一目的是从具体类中获取permutationCombination值,如果存在更多具体类或更多实例变量,则接口将变得团团而庞大。
除了构造函数以外,还有没有其他最佳方法可以将值分配给整个类所依赖的实例变量。
请参阅我的使用方式,请帮助我变得更好。
接口
interface Word {
void performPermutation();
int totalPermutationCount();
}
接口实现
class WordImpl implements Word{
// "word" is to store the word from the user
private String word;
// "convert" is to convert the word into a char array to perform the logic
private char[] convert;
// "permutationCombination" is to find the total number of combination that is done
private int permutationCombination = 0;
// Constructor
public WordImpl(String wordCom){
setWord(wordCom);
}
//getter setter of word instance variable
public void setWord(String wordCom){
if(!wordCom.isEmpty() && wordCom.trim().length() > 0){
word = wordCom;
}else{
word = "Default";
}
convertToChar();
}
// convert the given word to char array
private void convertToChar(){
convert = new char[word.length()];
for(int i = 0 ; i < word.length() ; i++){
convert[i] = word.charAt(i);
}
}
public int totalPermutationCount(){
return permutationCombination;
}
// -------------------------------- Below is the Temporary Logic Ignore it ---------------------------------------------
private void performSwap(char[] list , int from, int to){
char temp;
temp = list[from];
list[from] = list[to];
list[to] = temp;
}
public void performPermutation(){
char[] list = convert;
Set<String> listData = new HashSet<>();
System.out.println(convert);
for (int i = 0 ; i < word.length() ; i++){
for (int j = i + 1 , reverse = i - 1 ; j < word.length() || reverse >= 0 ; j++ , reverse--){
if(j < word.length()){
performSwap(list,i,j);
System.out.println(convertToString(list));
list = convert;
permutationCombination++;
}
if(reverse >= 0 && i != 0){
performSwap(list,i,reverse);
System.out.println(convertToString(list));
list = convert;
permutationCombination++;
}
}
}
}
// ----------------------------------------------------------------------------------------
private String convertToString(char[] list){
String value = "";
for(int i = 0 ; i < word.length() ; i++){
value = value + list[i];
}
return value;
}}
主班
public class MyClass {
public static void main(String args[]) {
Word wordImplReference = new WordImpl("home");
wordImplReference.performPermutation();
System.out.println(wordImplReference.totalPermutationCount());
}
}
3.如何解决突变问题
char [] list = convert;
public void performPermutation(){
char[] list = convert;
Set<String> listData = new HashSet<>();
System.out.println(convert);
for (int i = 0 ; i < word.length() ; i++){
for (int j = i + 1 , reverse = i - 1 ; j < word.length() || reverse >= 0 ; j++ , reverse--){
if(j < word.length()){
performSwap(list,i,j);
System.out.println(convertToString(list));
list = convert;
permutationCombination++;
}
if(reverse >= 0 && i != 0){
performSwap(list,i,reverse);
System.out.println(convertToString(list));
list = convert;
permutationCombination++;
}
}
}
}
最佳答案
以下可以帮助您进行查询:
如果从接口中删除totalPermutationCount方法,则会失去从接口类获得的抽象好处。如果仍然需要这样做,则需要使用WordImpl wordImplReference = new WordImpl(“ home”);在您的主要方法中。
在这种情况下,通过构造函数进行分配会更好。可以使用WordImpl类中的Set方法进行设置,但需要在Interface中添加setWord方法。