public class Course//使用public公共,说明是公共类,外部类也可以调用 { public int id; public string name; public int age = 10; public string student; public string Test() { student = "大学生"; string name1 = $"{name}的序号是{id},年龄是{age},是{student}"; Console.WriteLine(age+1);//因为在同一个类中,所以可以这么写 return name1; } //Course course= new Course();//在方法体外面只能new一个类,不能引用eg:course.id = 15;而且不能这样写,自己调用自己的话,会导致栈溢出 public void Test2() { Course course = new Course();//在C#中有垃圾回收机制,所以当对象new出来之后就不用close course.id = 15; course.name = "LIN"; Console.WriteLine(course.Test()); Console.WriteLine(course.age);//在外部类中的写法 } }
public static void Test() { Course course = new Course(); course.age = 29; Console.WriteLine(course.Test()); }
当没赋值时,string类型为null(空),int类型为0;
本文来自博客园,作者:阿霖找BUG,转载请注明原文链接:https://www.cnblogs.com/lin-07/p/17346154.html