C#基础代码学习

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();

    }
}


posted @ 2023-10-28 15:58  窦戈  阅读(11)  评论(0编辑  收藏  举报