Meetod
Python |
Java |
---|---|
Meetod ilma parameetriteta |
|
Pythonis on meetodi esimene parameeter alati self. class Student:
def get_name(self) -> str:
return self.name
|
Javas märgitakse meetodi nime ette nähtavus class Student {
private String name = "Name";
public String getName() {
return this.name
}
}
|
Parameetriga meetod |
|
class Student:
def set_name(self, name: str):
self.name = name
|
Kui meetod midagi ei tagasta, märgitakse class Student {
private String name = "Name";
public void setName(String name) {
this.name = name;
}
}
|
Vaikeväärtusega parameetrid |
|
class Student:
def get_grades(self, only_passed_grades=True):
if not only_passed_grades:
return self.grades
return list(filter(lambda grade: grade > 0,
self.grades))
|
Javas vaikeväärtust ei saa parameetrile anda. class Student {
public List<Integer> getGrades(boolean onlyPassedGrades) {
if (!onlyPassedGrades) {
return grades;
}
return grades.stream().filter(grade -> grade > 0).toList();
}
}
|