2023-05-25 17:47:19 +02:00
|
|
|
# Lösung: Vererbung
|
2023-05-23 09:19:31 +02:00
|
|
|
|
2023-05-25 17:47:19 +02:00
|
|
|
<div style="border: 1px solid grey;"><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br></div>```ruby
|
|
|
|
class Mensch
|
|
|
|
attr_reader :name, :alte
|
|
|
|
|
|
|
|
def initialize(name, alter)
|
|
|
|
@name, @alter = name, Integer(alter)
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
"#{@name}, Alter: #{@alter}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Student < Mensch
|
|
|
|
attr_reader :matrikel_nr
|
|
|
|
|
|
|
|
def initialize(name, alter, matrikel_nr)
|
|
|
|
super(name, alter)
|
|
|
|
@matrikel_nr = matrikel_nr
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
super + ", Matrikel-Nr.: #{@matrikel_nr}"
|
|
|
|
end
|
2023-05-23 09:19:31 +02:00
|
|
|
end
|
|
|
|
|
2023-05-25 17:47:19 +02:00
|
|
|
s = Student.new('Emil Meier', 22, '1822233')
|
2023-05-23 09:19:31 +02:00
|
|
|
```
|