C # class usage specification,
Notes:
Private Data Type. Most methods are public;
If there is inheritance or mutual reference, pay attention to the public or private data to ensure the read-only nature of the data;
C # constructor cannot be called when data is declared like C ++. The constructor must be called using Myclass temp = new Myclass;
C # multi-inheritance relationships are not supported. That is to say, a derived class cannot have multiple base classes. Simply put, a father may have many sons (the parent class can be derived from many subclasses), but a child can only have one father (the Child class cannot have multiple inheritance relationships );
C # Like other object-oriented features, such as overload, polymorphism, virtual functions, and abstract classes;
There is no difference between heavy loads and heavy loads in C ++. Only one problem is that the comparison operators (<,>=, >,<=, == ,! =), Must be reloaded in pairs;
Virtual functions must be added with a virtual declaration. The Derived classes must be rewritten with an override declaration added;
Abstract declarations must be added to abstract functions. If no code is executed in the base class, they must be added to the inheritance class;
If you need to output multiple values, you need to use the ref statement or out statement;
Objects declared by ref must be initialized in advance;
The out object does not need to be initialized in advance;
If the class contains static objects, you can use static constructors to assign values to static objects. Example:
ElemType. csusing System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace Test_Class {class ElementType {private int value; public ElementType () {value = 0;} public ElementType (int _ v) {this. value = _ v;} public void disp () {Console. writeLine ("value is {0}", value);} public void edit (int _ v) {this. value = _ v;} public int reff () {return this. value ;}} class package {private ElementType x; private ElementType y; public package () {this. x = new ElementType (); this. y = new ElementType ();}////// Constructor //////X ///Y public package (int _ x, int _ y) {this. x = new ElementType (_ x); this. y = new ElementType (_ y);} public void disp () {Console. writeLine ("package display"); Console. writeLine ("\ tx vaule is {0}", this. x. reff (); Console. writeLine ("\ ty vaule is {0}", this. y. reff (); Console. writeLine ();}////// Modify the value //////X ///Y public void modify (int _ x, int _ y) {this. x. edit (_ x); this. y. edit (_ y);} public void copyto (ref package p) {p. modify (this. x. reff (), this. y. reff ());}}}
Program. cs
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Test_Class{ class Program { static void Main(string[] args) { package p1 = new package(5, 5); package p0 = new package(); p0.disp(); p1.disp(); p1.copyto(ref p0); p0.disp(); p0.modify(10, 50); p0.disp(); Console.ReadLine(); } }}