What Is reflection

reflection ’s human ’s , has ’s .

Digestcalculate, among them, has as follows:code:

1
2
3
4
5
6
if selected_algorithm == "SHA-512":
    algorithm = hashlib.sha512()
elif selected_algorithm == "SHA-256":
    algorithm = hashlib.sha256()
else:
    algorithm = hashlib.md5()

code is userchoose ’s Digestcalculateway,Creating ’s object,very ’s method.

but is has ,cannotmore?reflection:

1
2
3
4
if hasattr(hashlib, selected_algorithm):
    algorithm = getattr(hashlib, selected_algorithm)()
else:
    algorithm = hashlib.md5()

a kind of Digestalgorithm,program,;Usingreflection, can runtime,choose ’s string ’s methodcall.

,cost is ?cost is someefficiency. Java has Usingreflection, for example, ’s Spring.

has somereflection ’s materialreflection can encapsulation,accessclass and object ’s member, is a . if need access,necessaryprivateprotectedmember; if must ,mostthink is is has Problem,do not more.

AboutC++ and reflection

C++ 20standard has , is has keywordtypeid has reflection ’s , ’s standard.

, is must ’s ,applicationscenariocodecomplexity,Using.

Programming,first step is ,Usage、;second step is ,do not ;third step is ,,also,,.