在以下代码的第二行:studySpell(Confundo()
函数studySpell
通过将Confundo
类分配给spell
来创建spell.incantation
类的新实例。我的问题是,在执行倒数第二行之后,为什么'Accio'
会返回'Confundo'
?它不应该返回class Spell(object):
def __init__(self, incantation, name):
self.name = name
self.incantation = incantation
def __str__(self):
return self.name + ' ' + self.incantation + '\n' + self.getDescription()
def getDescription(self):
return 'No description'
def execute(self):
print(self.incantation)
class Accio(Spell):
def __init__(self):
Spell.__init__(self, 'Accio', 'Summoning Charm')
class Confundo(Spell):
def __init__(self):
Spell.__init__(self, 'Confundo', 'Confundus Charm')
def getDescription(self):
return 'Causes the victim to become confused and befuddled.'
def studySpell(spell):
print(spell)
>>> spell = Accio()
>>> spell.execute()
Accio
>>> studySpell(spell)
Summoning Charm Accio
No description
>>> studySpell(Confundo())
Confundus Charm Confundo
Causes the victim to become confused and befuddled.
>>> print(spell.incantation)
Accio
吗?
var html = html + "<tr>\n" + "<td style='text-align: center; background-color: #FFCE43;
'> </br>" + $(n).find("Julio").text() + " <img style = 'cursor: pointer;' onclick='myFunction(\""+a+"\", \""+b+"\")' src='add.png'></td> </tr>";
如果你不理解我的观点,请告诉我,我会尝试传播更多内容。
答案 0 :(得分:0)
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by Rituraj_Jain on 7/14/2017.
*/
public class Main {
private static final Pattern pattern = Pattern.compile("(\\S)-(\\S)");
private static final String input = "Test-TestBad-ManRituRaj-Jain";
public static void main(String[] args) {
Matcher matcher = pattern.matcher(input);
String s = matcher.replaceAll("$1 $2");
System.out.println(s);
System.out.println(input.replaceAll(pattern.pattern(), "$1 $2"));
}
public static void main1(String[] args) {
Matcher matcher = pattern.matcher(input);
StringBuffer finalString = new StringBuffer();
while(matcher.find()){
String replace = matcher.group().replace("-", " ");
matcher.appendReplacement(finalString, replace);
}
matcher.appendTail(finalString);
System.out.println(input);
System.out.println(finalString.toString());
}
}
函数不会“分配”studySpell
变量(全局变量)。它创建一个新的本地变量(也称为spell
),它隐藏全局spell
变量,并在函数执行完毕后消失。
答案 1 :(得分:0)
要实现您想要的功能,您必须重新分配变量,然后执行方法:
spell = Accio()
spell.execute()
studySpell(spell)
studySpell(Confundo())
spell = Confundo()
print(spell.incantation)
Accio
Summoning Charm Accio
No description
Confundus Charm Confundo
Causes the victim to become confused and befuddled.
Confundo
您发布的代码正在按原样运行。
答案 2 :(得分:-2)
class Spell:
def __init__(self, incantation, name):
self.name = name
self.incantation = incantation
def __str__(self):
return self.name + ' ' + self.incantation + '\n' +
self.get_description()
def get_description(self):
return 'No description'
def execute(self):
print(self.incantation)
class Accio(Spell):
def __init__(self):
Spell.__init__(self, 'Accio', 'Summoning Charm')
class Confundo(Spell):
def __init__(self):
Spell.__init__(self, 'Confundo', 'Confundus Charm')
def get_description(self):
return 'Causes the victim to become confused and befuddled.'
def study_spell(spell):
print(spell)
spell = Accio()
spell.execute()
study_spell(spell)
study_spell(Confundo())
print(spell.incantation)