C# 属性

C# 抽象

C# 字符串

C# 泛型

C# 杂项

C# 新特性

C# 对象和类

由于 C# 是一种面向对象的语言,因此程序是使用 C# 中的对象和类设计的。

C# 对象

在 C# 中,Object 是一个真实世界的实体,例如椅子、汽车、笔、手机、笔记本电脑等。

换句话说,对象是具有状态和行为的实体。在这里,状态意味着数据,行为意味着功能。

对象是一个运行时实体,它是在运行时创建的。

对象是类的一个实例。类的所有成员都可以通过对象访问。

让我们看一个使用 new 关键字创建对象的示例。

句法 (Syntax)

Student s1 = new Student();//创建一个Student对象

在此示例中,Student 是类型,s1 是引用变量,它引用 Student 类的实例。new 关键字在运行时分配内存。

C# 类

在 C# 中,类是一组相似的对象。它是创建对象的模板。它可以有字段、方法、构造函数等。

让我们看一个只有两个字段的 C# 类示例。

例子 (Example)

public class Student  
 {  
     int id;//字段或数据成员   
     String name;//字段或数据成员 
 }

C# 对象和类示例

让我们看一个具有两个字段的类示例:id 和 name。它创建类的实例,初始化对象并打印对象值。

例子 (Example)

using System;  
   public class Student  
    {  
        int id;//数据成员(也是实例变量)   
        String name;//数据成员(也是实例变量)    
         
    public static void Main(string[] args)  
        {  
            Student s1 = new Student();//创建一个Student对象    
            s1.id = 101;  
            s1.name = "Sonoo Jaiswal";  
            Console.WriteLine(s1.id);  
            Console.WriteLine(s1.name);  
  
        }  
    }

输出:

101Sonoo Jaiswal

C# 类示例 2:在另一个类中有 Main()

让我们看看另一个类的例子,我们在另一个类中有 Main() 方法。在这种情况下,类必须是公开的。

例子 (Example)

using System;  
   public class Student  
    {  
        public int id;   
        public String name;  
   }  
   class TestStudent{  
       public static void Main(string[] args)  
        {  
            Student s1 = new Student();    
            s1.id = 101;  
            s1.name = "Sonoo Jaiswal";  
            Console.WriteLine(s1.id);  
            Console.WriteLine(s1.name);  
  
        }  
    }

输出:

101Sonoo Jaiswal

C#类示例3:通过方法初始化和显示数据

让我们看另一个 C# 类的例子,我们通过方法初始化和显示对象。

例子 (Example)

using System;  
   public class Student  
    {  
        public int id;   
        public String name;  
        public void insert(int i, String n)  
        {  
            id = i;  
            name = n;  
        }  
        public void display()  
        {  
            Console.WriteLine(id + " " + name);  
        }  
   }  
   class TestStudent{  
       public static void Main(string[] args)  
        {  
            Student s1 = new Student();  
            Student s2 = new Student();  
            s1.insert(101, "Ajeet");  
            s2.insert(102, "Tom");  
            s1.display();  
            s2.display();  
  
        }  
    }

输出:

101Ajeet
102 Tom

C# 类示例 4:存储和显示员工信息

例子 (Example)

using System;  
   public class Employee  
    {  
        public int id;   
        public String name;  
        public float salary;  
        public void insert(int i, String n,float s)  
        {  
            id = i;  
            name = n;  
            salary = s;  
        }  
        public void display()  
        {  
            Console.WriteLine(id + " " + name+" "+salary);  
        }  
   }  
   class TestEmployee{  
       public static void Main(string[] args)  
        {  
            Employee e1 = new Employee();  
            Employee e2 = new Employee();  
            e1.insert(101, "Sonoo",890000f);  
            e2.insert(102, "Mahesh", 490000f);  
            e1.display();  
            e2.display();  
  
        }  
    }

输出:

101 Sonoo 890000
102 Mahesh 490000


上一主题 没有了 下一主题 C# 构造函数
  • 使用社交账号登录,本站支持
全部评论(0)