c#中怎样创建一个类然后调用这个类?

2025-02-25 16:54:17
推荐回答(4个)
回答1:

创建类
例如:
public class Student
{
public int Id{get;set;} //为类定义字段
public string StuName{get;set;}

//可以写一些方法
例:
public string Get()
{
//方法体
}
}

调用类
Student s1=new Student();
string str=s1.Get();//调用类的方法

回答2:

声明一个类 类内包含字段和函数(所谓的方法)
class 类名{ 字段; 方法}
调用的时候先实例化 然后调用其中的方法来调用类,空间有限无法详细描述

回答3:

对这个类添加引用就可以了,(在当前程序中using被调用类的命名空间.被调用类名)。

回答4:

调用类New就行了。例如News ns = new News();这样