Structure and class
Structure:
In a structure declaration, fields cannot be initialized unless they are declared const or static
Definition: structure modifier struct structure name {}
Inheritance is not supported
Class definition: modifier class name
Modifier for class:
new: only allowed in nested class declaration, indicating that members inherited from the base class with the same name as the base class are hidden in the class
public: unlimited access to this class
protected: can only be accessed from its class and its subclass (derived class)
internal: only its class can access
private: only applications or libraries in. Net can access it
Abstract: abstract classes are not allowed to create instances of classes
Sealed: sealed class, not allowed to be inherited
Be careful:
Methods or fields in a class cannot be called with the class name until the class has been instantiated
Class polymorphism:
Polymorphism enables subclass instances to directly assign variables to base classes, and then directly call subclass methods through this variable
The polymorphism of class in C is tried by overloading the virtual method or function member of base class in subclass
Be careful:
The virtual modifier cannot be used with the private,Static,abstract, or override modifiers
The override modifier cannot be used with the new, static, and virtual modifiers, and the override method can only be used to override the virtual method in the base class
The combination of C ා relay virtual method and rewriting can realize polymorphism
Class supports single inheritance
See the detailed instance code:
/* 2020-3-6 14:15:27 Structure and class Structure: In a structure declaration, fields cannot be initialized unless they are declared const or static Definition: structure modifier struct structure name {} Inheritance is not supported Class definition: modifier class name Modifier for class: new : Only allowed in nested class declaration, indicating that members inherited from the base class with the same name as those in the base class are hidden in the class public: Unlimited access to this class protected: Can only be accessed from its class and its subclass (derived class) internal: Only its class can access private:Only applications or libraries in. Net can access abstract: Abstract classes are not allowed to create instances of classes sealed:Sealed class, inheritance not allowed Be careful: Methods or fields in a class cannot be called with the class name until the class has been instantiated Class polymorphism: Polymorphism enables subclass instances to directly assign variables to base classes, and then directly call subclass methods through this variable C#The polymorphism of the middle class is tested by overloading the virtual methods or function members of the base class in the subclass Be careful: virtual Modifier cannot be used with private,Static,abstract, or override modifiers override The modifier cannot be used with the new, static, and virtual modifiers, and the override method can only be used to override the virtual method in the base class C#The combination of relay virtual method and rewriting can realize polymorphism Class supports single inheritance */ using System; using James_Struct; using James_Class; namespace ConsoleApp8 { class Program { static void Main(string[] args) { //TestStructFunc(); //TestJames_Class(); TestJames_Class2(); Console.ReadKey(); //Console.WriteLine("Hello World!"); } static void TestStructFunc() { Rect rect; rect.width = 5; rect.height = 3; Console.WriteLine("The area of the rectangle is:{0}", rect.Area()); Rect rect2 = new Rect(6, 4); Console.WriteLine("The area of the rectangle is:{0}", rect2.Area()); } static void TestJames_Class() { Cars car = new Cars(); //Here the constructor has been executed Car2 car2 = new Car2(); Console.WriteLine(car2.Add()); } static void TestJames_Class2() //Polymorphism under test { MyClass2 c1 = new MyClass2(); //Instantiate MyClass2 MyClass1 c2 = (MyClass1)c1; //Subclass object instantiation base class object c2.X = 3; c2.Y = 5; Console.WriteLine(c2.Add()); //Because the subclass overrides the Add method of the subclass that is actually called, the result is 14, not 8 Console.WriteLine(c1.Add()); /* When a subclass inherits from a base class, it gets all the methods, fields, properties, and events of the base class. To change the data and behavior of the base class, there are two options 1,Replace base class members with new derived class members 2,Override virtual base member */ } } } namespace James_Struct { public struct Rect { public double width; public double height; public Rect(double x, double y) //Constructor { width = x; height = y; } public double Area() { return width * height; } } } namespace James_Class { public class Cars { public int number = 100; public string color = "blue"; private string brand = "JamesFactory"; public Cars() { Console.WriteLine("Execute constructor"); Console.WriteLine(number); Console.WriteLine(color); Console.WriteLine(brand); } } class Car2 : Cars { private string brand2; public int Add() { return number; } } //The concept of polymorphism in class class MyClass1 { private int x = 0; private int y = 0; public int X { get { return x; } set { x = value; } } public int Y { get { return y; } set { y = value; } } public virtual int Add() { return X + Y; } } class MyClass2 : MyClass1 { //You cannot override a non virtual or static method. Overridden base method must be virtual, abstract, or override /*public override int Add() //The override method can be used to re implement the method in the base class, that is, rewrite the base method { int x = 7; int y = 7; return x + y; }*/ public new int Add() { int x = 7; int y = 7; return x + y; } } }