using System;
using System.Collections.Generic;
public class Student{
public string Name { get; set; }
}
class MyClass
{
//用于存储学生对象的集合
private List<Student> test;
//构造方法 示例话调用 类似PHP中的__construct
public MyClass()
{
test = new List<Student>();
}
//添加属性
public void AddStudent(Student student){
test.Add(student);
}
//打印
public void PrintStudent(){
foreach (Student student in test)
{
Console.WriteLine("Name: " + student.Name);
}
}
}
public class Program{
public static void Main(){
//声明一个MyClass类型的变量myClass = 实例化MyClass类
MyClass myClass = new MyClass();
//声明一个Student类型的变量student1 = 实例化并赋值
Student student1 = new Student{ Name = "peter" };
//添加到myClass变量
myClass.AddStudent(student1);
//打印
myClass.PrintStudent();
}
}
标签:Name,C#,代码,学习,Student,myClass,test,MyClass,public
From: https://www.cnblogs.com/douyuanjun/p/17794197.html