我想在ArrayList
中创建一个Student class
,但无法添加student1, student2, student3
来自Main
的值 public class Program
{
static void Main(string[] args)
{
Student student1 = new Student("John", "Grodinskiy", "12/07/1996", "Lesy Ukrainky 88", "Cherkasy", "58000", "Ukraine");
Student student2 = new Student("Niko", "Rekun", "03/02/1995", "Promyslova 86", "Velykiy Bychkiv", "90615","Ukraine");
Student student3 = new Student("Veronica","Kran", "26/03/1991", "Uzhgorodska 1", "Svalyava", "59432", "Ukraine");
........
Console.WriteLine("The {0} program contains the {1} of Sciense degree\nThe {2} of Sciense degree contains the course {3}\n{4} course contains {5} students", program.Program_name1, myDegree.degree_name, myDegree.degree_name, myCourse.Course_name, myCourse.Course_name, Student.Counter);
student1.TakeTest();
student2.TakeTest();
student3.TakeTest();
}
}
。如何在不将其替换为其他课程的情况下使student1 / 2/3可见?
这是我的代码的一部分:
public class Student:Person
{
public Student(string name, string last_name, string birthday, string address, string city, string zip, string country)
{
this.Name = name;
this.Last_name = last_name;
this.Birth_date = birthday;
this.Address = address;
this.City = city;
this.Zip = zip;
this.Country = country;
Counter++
}
public void TakeTest()
{
Take_counter++;
Console.WriteLine("{0} student took the test.",Take_counter);
}
public void add_student()
{
ArrayList students = new ArrayList();
students.Add(student1);
}
}
// Class
//PROPERTIES
/// <summary>
/// Property for "Title"
/// Read & Write property for attribute "Title"
/// set method currently has no protection
/// </summary>
///
public string Title
{
get { return title; }
set { title = value; }
}
public string FirstName
{
get { return firstname; }
set { firstname = value; }
}
public string MiddleName
{
get { return middlename; }
set { middlename = value; }
}
public string LastName
{
get { return lastname; }
set { lastname = value; }
}
答案 0 :(得分:2)
代码的以下部分不属于Student
类。
public void add_student()
{
ArrayList students = new ArrayList();
students.Add(student1);
}
因为学生不是学生的集合,也不包含学生集合。
您可以做的一件事是创建一个代表学生集合的新课程,让我们称之为StudentProgram
:
public class StudentProgram
{
private readonly List<Student> _students = new List<Student>();
public StudentProgram(string programName)
{
Name = programName;
}
public readonly string Name;
public void AddStudent(Student newStudent)
{
_students.Add(newStudent);
}
public IEnumerable<Student> AllStudents
{
get { return _students; }
}
public int NumberOfStudents
{
get { return _students.Count; }
}
}
然后,您可以在Main
中使用此功能:
static void Main(string[] args)
{
var scienceProgram = new StudentProgram("science");
scienceProgram.Add(new Student(
"John", "Grodinskiy", "12/07/1996",
"Lesy Ukrainky 88", "Cherkasy", "58000", "Ukraine"));
scienceProgram.Add(new Student(
"Niko", "Rekun", "03/02/1995",
"Promyslova 86", "Velykiy Bychkiv", "90615","Ukraine"));
scienceProgram.Add(new Student(
"Veronica","Kran", "26/03/1991",
"Uzhgorodska 1", "Svalyava", "59432", "Ukraine"));
........
Console.WriteLine(
"The {0} program has {1} students",
scienceProgram.Name, scienceProgram.NumberOfStudents);
foreach (var student in scienceProgram.AllStudents)
student.TakeTest();
}
答案 1 :(得分:1)
public class Student:Person
{
public static List<Student> Students = new List<Student>();
//....
public static void add_student(Student newStudent)
{
Students.Add(newStudent);
}
}
学生列表是静态的,这意味着你将拥有一个全班学生列表,从你认为你想要实现的构造函数和方法。
答案 2 :(得分:1)
这是一个非常令人困惑的设计,如果你想在add_student方法中添加学生,那么你应该使用:
static void Main(string[] args)
{
...
student1.Add(student1);
}
然后在你的主要方法
from tkinter import *
from functools import partial
canvas_height = 400
canvas_width = 600
canvas_colour = "grey50"
moveBoolean = "True"
def move_it(ctr=0):
if ctr < len(coords):
x, y = coords[ctr]
ctr += 1
print ctr, x, y
canvas.move(square, x, y)
window.after(1000, partial(move_it, ctr))
window = Tk()
canvas = Canvas(bg=canvas_colour, height=canvas_height,
width=canvas_width, highlightthickness=0)
canvas.pack()
square = canvas.create_rectangle(50, 50, 50, 50, width=50, fill="black")
coords = ((90, 90),
(180, 180),
(50, 50))
move_it()
window.mainloop()
将学生添加到一个名为student的课程中虽然没有多大意义(因为你将学生传递给自己,你可以使用'this'关键字访问),如果你进一步解释该程序,也许我可以帮助你改善它。
答案 3 :(得分:1)
也许以下答案太简单了。也许是因为我没有正确地解决你的问题。但如果你的问题出在这里:
public void add_student()
{
ArrayList students = new ArrayList();
students.Add(student1);
}
因此,如果您想将新的students
存储到ArrayList students
,那么您可以执行以下操作:
ArrayList students = new ArrayList();
public void add_student(student1)
{
students.Add(student1);
}
您可以通过以下方式在主要课程中添加students
Student person = new Student(...);
person.add_student("any student to be added");