Explain the characteristics and concepts of classes and objects in C phase object programming

  • 2021-09-05 00:41:56
  • OfStack

Class
A "class" is a construct that allows you to create your own custom type by combining variables, methods, and events of other types into one. Class is like a blueprint, which defines the data and behavior of the type. If the class is not declared static, client code can use the class by creating "objects" or "instances" assigned to variables. The variable remains in memory until all references to it are out of scope. When all references are out of scope, CLR marks the variable for garbage collection. If a class is declared static, there is only one copy in memory, and client code can access the class only through the class itself, not "instance variables."
Declare class
Class is declared using the class keyword, as shown in the following example:


public class Customer
{
  //Fields, properties, methods and events go here...
}

The class keyword is preceded by the access level. Because public is used in this example, anyone can create objects based on this class. The name of the class follows the class keyword. The rest of the definition is the body of the class, which is used to define behavior and data. The fields, properties, methods, and events of a class are collectively referred to as "class members."
Create an object
Although sometimes classes and objects are interchangeable, they are different concepts. Class defines the type of an object, but it is not the object itself. Objects are concrete entities based on classes, sometimes called instances of classes.
You can create an object by using the new keyword (followed by the name of the class on which the object will be based), as follows:


Customer object1 = new Customer();

After an instance of the class is created, a reference to the object is passed back to the programmer. In the previous example, object1 is a reference to an Customer-based object. This reference refers to the new object, but does not contain the object data itself. In fact, you can create an object reference without creating an object at all:


Customer object2;

It is not recommended to create an object reference like this that does not refer to the object, because attempts to access the object through such a reference at run time will fail. However, you can create a reference to an object by creating a new object or assigning it to an existing object, as follows:


Customer object3 = new Customer();
Customer object4 = object3;

This code creates two object references that refer to the same object. Therefore, any changes made to the object through object3 will be reflected in the subsequent use of object4. Because class-based objects are referenced by reference, classes are called reference types.
Class inheritance
Inheritance is achieved by using "derivation," which means that a class is declared using a "base class," from which its data and behavior are inherited. You can specify a base class by appending a colon and a base class name to the derived class name, as follows:


public class Manager : Employee
{
  // Employee fields, properties, methods and events are inherited
  // New Manager fields, properties, methods and events go here...
}

When a class declares a base class, it inherits all members of the base class except the constructor.
Unlike C + +, classes in C # can only inherit directly from one base class. However, because the base class itself may inherit from another class, a class can indirectly inherit from multiple base classes. Moreover, one class can directly implement more than one interface.
Classes can be declared as abstract classes. Abstract classes contain abstract methods that have signature definitions but are not implemented. Abstract classes cannot be instantiated. Abstract classes can only be used through derived classes that implement abstract methods. In contrast, sealed classes do not allow other classes to derive from them.
Class definitions can be split between different source files.
Describe
The following example defines a public class that contains a field, a method, and a special method called a constructor. For more information, see Constructor (C # Programming Guide). The class is then instantiated using the new keyword.


public class Person
{
  // Field
  public string name;

  // Constructor that takes no arguments.
  public Person()
  {
    name = "unknown";
  }

  // Constructor that takes one argument.
  public Person(string nm)
  {
    name = nm;
  }

  // Method
  public void SetName(string newName)
  {
    name = newName;
  }
}
class TestPerson
{
  static void Main()
  {
    // Call the constructor that has no parameters.
    Person person1 = new Person();
    Console.WriteLine(person1.name);

    person1.SetName("John Smith");
    Console.WriteLine(person1.name);

    // Call the constructor that has one parameter.
    Person person2 = new Person("Sarah Jones");
    Console.WriteLine(person2.name);

    // Keep the console window open in debug mode.
    Console.WriteLine("Press any key to exit.");
    Console.ReadKey();
  }
}

Output:


unknown
John Smith
Sarah Jones

Object
A class or structure definition acts like a blueprint, specifying what the type can do. Essentially, objects are blocks of memory allocated and configured according to this blueprint. Programs can create multiple objects of the same class. Objects, also known as instances, can be stored in named variables, arrays or collections. The code that uses these variables to call object methods and access object public properties is called client code. In object-oriented languages such as C #, a typical program consists of multiple objects that interact dynamically.

Structure instance. Option class instance
Because a class is a reference type, the variables of a class object refer to the address of the object on the managed heap. If a second object of the same type 1 is assigned to the first object, both variables refer to the object at that address. These 1 points will be discussed in more detail later in this topic.
Class is created using the new operator. In the following example, Person is the type, and person1 and person 2 are instances of the type (that is, objects).


public class Person
{
  public string Name { get; set; }
  public int Age { get; set; }
  public Person(string name, int age)
  {
    Name = name;
    Age = age;
  }
  //Other properties, methods, events...
}

class Program
{
  static void Main()
  {
    Person person1 = new Person("Leopold", 6);
    Console.WriteLine("person1 Name = {0} Age = {1}", person1.Name, person1.Age);

    // Declare new person, assign person1 to it.
    Person person2 = person1;

    //Change the name of person2, and person1 also changes.
    person2.Name = "Molly";
    person2.Age = 16;

    Console.WriteLine("person2 Name = {0} Age = {1}", person2.Name, person2.Age);
    Console.WriteLine("person1 Name = {0} Age = {1}", person1.Name, person1.Age);

    // Keep the console open in debug mode.
    Console.WriteLine("Press any key to exit.");
    Console.ReadKey();

  }
}

Output:


  person1 Name = Leopold Age = 6
  person2 Name = Molly Age = 16
  person1 Name = Molly Age = 16

Because the structure is a value type, the variables of the structure object have a copy of the whole object. Structure can also be created using the new operator, but this is not required, as shown in the following example:


public struct Person
{
  public string Name;
  public int Age;
  public Person(string name, int age)
  {
    Name = name;
    Age = age;
  }
}

public class Application
{
  static void Main()
  {
    // Create struct instance and initialize by using "new".
    // Memory is allocated on thread stack.
    Person p1 = new Person("Alex", 9);
    Console.WriteLine("p1 Name = {0} Age = {1}", p1.Name, p1.Age);

    // Create new struct object. Note that struct can be initialized
    // without using "new".
    Person p2 = p1;

    // Assign values to p2 members.
    p2.Name = "Spencer";
    p2.Age = 7;
    Console.WriteLine("p2 Name = {0} Age = {1}", p2.Name, p2.Age);

    // p1 values remain unchanged because p2 is copy.
    Console.WriteLine("p1 Name = {0} Age = {1}", p1.Name, p1.Age);

    // Keep the console open in debug mode.
    Console.WriteLine("Press any key to exit.");
    Console.ReadKey();
  }
}

Output:


Customer object1 = new Customer();
0

The memory of p1 and p2 is allocated on the thread stack. This memory is reclaimed with the type or method 1 that declares it. This is one reason why the structure is copied when assigning values. In contrast, when all references to class instance objects are out of scope, the memory allocated for the class instance is automatically reclaimed by the common language runtime (garbage collection). Class objects cannot be destroyed as explicitly as in C + +.

Equality between object identity and. value
When comparing two objects for equality, you must first be clear whether you want to know whether two variables represent the same 1 object in memory, or whether the values of one or more fields of the two objects are equal. If you want to compare values, you must consider whether the two objects are instances of a value type (structure) or a reference type (class, delegate, array).
To determine whether two class instances refer to the same 1 location in memory (meaning they have the same identity), use the static Equals method. (System. Object is the implicit base class for all value and reference types, including user-defined constructs and classes.)
To determine whether the instance fields in two struct instances have the same value, use the ValueType. Equals method. Because all structures implicitly inherit from System. ValueType, you can call this method directly on an object, as shown in the following example:


Customer object1 = new Customer();
1

Output:


Customer object1 = new Customer();
2

The System. ValueType implementation of Equals uses reflection because it must be able to determine which fields are in any structure. Overriding the Equals method provides efficient equalization algorithms for your type when creating your own structure.


Related articles: