C # class usage specification,

Source: Internet
Author: User

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();        }    }}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.