#!/usr/bin/ruby -w
# -*- coding: UTF-8 -*-
# 学生类
class Student
attr_accessor :age, :name
def initialize(name, age)
@name, @age = name, age
end
end
# 班级类
class Class
attr_accessor :students, :class_name
def initialize(class_name)
@class_name=class_name
@students=[]
end
end
# 学校类
class School
attr_accessor :classes, :school_name
def initialize(school_name)
@school_name=school_name
@classes=[]
end
end
school=School.new("XXX大学")
for i in 0..10
school.classes.push(Class.new("班级"+i.to_s))
for j in 0..30
school.classes[i].students.push(Student.new("学生"+j.to_s,10+j))
end
end
puts school.school_name
for i in 0..10
puts school.classes[i].class_name
for j in 0..30
puts "学生:"+school.classes[i].students[j].name+" age :"+school.classes[i].students[j].age.to_s
end
end
Ruby 学校,班级,学生类的三级关系
最新推荐文章于 2024-09-29 21:40:02 发布