Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

C# 类成员


类成员

类内部的字段和方法通常被称为 "类成员"

例子

创建一个 Car 类,包含三个类成员:两个字段一个方法

// The class
class MyClass
{
  // Class members
  string color = "red";        // field
  int maxSpeed = 200;          // field
  public void fullThrottle()   // method
  {
    Console.WriteLine("The car is going as fast as it can!");
  }
}

字段

在上一章中,您了解到类内部的变量被称为字段,您可以通过创建类的对象并使用点语法 (.) 来访问它们。

以下示例将创建一个名为 myObjCar 类对象。然后我们打印字段 colormaxSpeed 的值

例子

class Car 
{
  string color = "red";
  int maxSpeed = 200;

  static void Main(string[] args)
  {
    Car myObj = new Car();
    Console.WriteLine(myObj.color);
    Console.WriteLine(myObj.maxSpeed);
  }
}

自己尝试 »

您也可以将字段留空,并在创建对象时修改它们

例子

class Car 
{
  string color;
  int maxSpeed;

  static void Main(string[] args)
  {
    Car myObj = new Car();
    myObj.color = "red";
    myObj.maxSpeed = 200;
    Console.WriteLine(myObj.color);
    Console.WriteLine(myObj.maxSpeed);
  }
}

自己尝试 »

这在创建同一个类的多个对象时特别有用

例子

class Car 
{
  string model;
  string color;
  int year;

  static void Main(string[] args)
  {
    Car Ford = new Car();
    Ford.model = "Mustang";
    Ford.color = "red";
    Ford.year = 1969;

    Car Opel = new Car();
    Opel.model = "Astra";
    Opel.color = "white";
    Opel.year = 2005;

    Console.WriteLine(Ford.model);
    Console.WriteLine(Opel.model);
  }
}

自己尝试 »



对象方法

您从 C# 方法 一章中了解到,方法用于执行特定操作。

方法通常属于一个类,它们定义了类对象的行为。

就像字段一样,您可以使用点语法访问方法。但是,请注意该方法必须是 public。并且记住,我们使用方法名后跟两个括号 () 和一个分号 ; 来调用(执行)该方法

例子

class Car 
{
  string color;                 // field
  int maxSpeed;                 // field
  public void fullThrottle()    // method
  {
    Console.WriteLine("The car is going as fast as it can!"); 
  }

  static void Main(string[] args)
  {
    Car myObj = new Car();
    myObj.fullThrottle();  // Call the method
  }
}

自己尝试 »

为什么我们将该方法声明为 public,而不是像 C# 方法章节 中的示例那样声明为 static 呢?

原因很简单:static 方法可以在不创建类对象的情况下访问,而 public 方法只能由对象访问。


使用多个类

记得上一章中我们说过,为了更好的组织(一个用于字段和方法,另一个用于执行),我们可以使用多个类。这是推荐的做法

prog2.cs

class Car 
{
  public string model;
  public string color;
  public int year;
  public void fullThrottle()
  {
    Console.WriteLine("The car is going as fast as it can!"); 
  }
}

prog.cs

class Program
{
  static void Main(string[] args)
  {
    Car Ford = new Car();
    Ford.model = "Mustang";
    Ford.color = "red";
    Ford.year = 1969;

    Car Opel = new Car();
    Opel.model = "Astra";
    Opel.color = "white";
    Opel.year = 2005;

    Console.WriteLine(Ford.model);
    Console.WriteLine(Opel.model);
  }
}

自己尝试 »

关键字 public 被称为访问修饰符,它指定 Car 的字段也可以被其他类访问,例如 Program

您将在后面的章节中了解有关 访问修饰符 的更多内容。

提示: 随着您的继续学习,您还将了解其他类成员,例如 构造函数属性


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.