C # Basic Primer Five

Source: Internet
Author: User

C # Basics Get started five recursion
    • Recursive invocation: A method invokes itself directly or indirectly, called a recursive invocation of a method.
    • Recursive method: The method itself is called in the method body.
    • Recursive example
public long Fib(int n){    if(n==0||n==1)    return n;    return Fib(k-2)+Fib(k-1);}
    • Exercise: Use recursion to calculate the value of a 1+2+3...+100.
static int Fun(int n){    if(n==1)    return n;    return n+Fun(n-1);}
Construction method and construction method of destructor method
    • The constructor is used to initialize the object. For example, to create a pencil object, you can use a constructor to control its initial state, such as length, width, color, and so on.
    • The construction method must have the same name as the class, and the construction method has no return value, but can have multiple overloads.
    • The essence of the construction method is a constructor, which is generated for the instantiation of the class, and the constructor method is a special method that can only be executed at initialization time and cannot be called externally.
    • The constructor method can not be written, and the system will add an parameterless constructor to your class by default.
    • If the constructor is set to private, it cannot be externally instantiated.
 public class Student    {        public int age;        public string name;        public int grade;        public Student(int a,string n, int g){            age = a; name = n; grade = g;    }    }    class MainClass    {                public static void Main(string[] args)        {            Student student = new Student(21, "zxh", 100);            Console.WriteLine(student.age+student.name+student.grade);        }    }
destructor method
    • Destructors are used to instantiate instances of classes to free up memory space.
class Car{    ~Car()    {        //cleanup statements    }}
    • A destructor cannot be defined in a struct, only a destructor can be used on a class, a class can have only one destructor, cannot be inherited or overloaded, cannot call a destructor, can only be called automatically, and the destructor method has no modifiers or parameters.
Object-Oriented Programming
    • Process oriented: process-oriented is to analyze the steps required to solve the problem, and then use the function of the steps to achieve one step at a time, the use of one by one in turn to call it.
    • Object-oriented: To break up a problem transaction into individual objects, the object is not designed to complete a single step, but to describe the behavior of something in the whole process of solving the problem.
    • Object-oriented features: encapsulation, inheritance, polymorphism.
    • OOP (Object-oriented programming) achieves the three main goals of software engineering: reusability, flexibility, and extensibility.
Packaging
    • Encapsulation is the first step in implementing object-oriented programming, in which a package is a collection of data or functions in a unit (called a class).
    • The meaning of encapsulation is to protect or prevent the code from being accidentally destroyed by us.
    • Encapsulation is the re-wrapping of class members.
Inherited
    • Inheritance is used to create new classes that reuse, extend, and modify behaviors defined in other classes, whose members are inherited by classes called "base classes", the classes that inherit these members are called "derived classes," and derived classes can have only one direct base class, but inheritance is transitive, and if CLASSB derives CLASSC, ClassA the CLASSB is derived, CLASSC inherits the members declared in B and a.
    • Base classes have, derived classes have, derived classes have, the base class may not, that is: derived classes can be assigned to the base class, and the base class cannot be assigned to a derived class. public class Student:Person{}student inherit person.
public class Person    {        public void Eat()        {            Console.WriteLine("人都要吃饭");        }    }    public class Student:Person    {        public void Learn()        {            Console.WriteLine("学生都要学习");        }    }    public class Senior : Student    {        public new void Learn(){//方法替换            Console.WriteLine("高中生要学的更多");        }        public void University()        {            Console.WriteLine("高中生要考大学");        }    }    class MainClass    {                public static void Main(string[] args)        {            Senior sen = new Senior();            sen.Learn();            sen.University();            sen.Eat();        }    }> 打印结果:> 高中生要学的更多> 高中生要考大学> 人都要吃饭
Polymorphic
    • When a derived class inherits from a base class, it obtains all the methods, fields, properties, and events of the base class, and the derived class can choose:
      • Inherit the closest base class method without overriding it
      • overriding virtual members in a base class
      • Defines a new non-virtual implementation of a member that hides a base class implementation, which we typically call "method substitution."
    • Virtual function: Virtual method is used to modify the method;
    • Rewrite: Overridden by the method override;
    • Sealed class: Sealed modifier, decorated class is sealed, cannot be inherited cannot be overridden, derived class can stop inheriting through sealed, internal method can prevent being overridden by sealed.
    • Static classes are default to sealed classes, so static classes cannot be inherited.

C # Basic Primer Five

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.