C#基础-类

using System;

namespace ConsoleApp4
{
    class Program
    {
        static void Main(string[] args)
        {
            // 类的实例化:对象
            Student stu = new Student();
            // 给对象赋值
            stu.name = "Joe";
            stu.stuNo = 1001;
            stu.age = 21;
            Console.WriteLine("学生的姓名:" + stu.name);
            Console.WriteLine("学生的学号:" + stu.stuNo);
            Console.WriteLine("学生的年龄:" + stu.age);
        }


    }

    // 定义类的两种方法,
    //1/在源文件基础上添加
    //2/单独在文件添加

    public class Student
    {
        // 定义变量
        public string name;
        public int stuNo;
        public int age;

    }
}

定义类的两种方法,

1.在源文件基础上添加

public class Student
{
    // 定义变量
    public string name;
    public int stuNo;
    public int age;

}

2.单独在文件添加

类的实例化

 static void Main(string[] args)
        {
            // 类的实例化:对象
            Student stu = new Student();
            // 给对象赋值
            stu.name = "Joe";
            stu.stuNo = 1001;
            stu.age = 21;
            Console.WriteLine("学生的姓名:" + stu.name);
            Console.WriteLine("学生的学号:" + stu.stuNo);
            Console.WriteLine("学生的年龄:" + stu.age);
        }

猜你喜欢

转载自www.cnblogs.com/carious/p/10662539.html