C # object-oriented Basics

Source: Internet
Author: User

C # object-oriented Basics

1. Why is object-oriented?

(1) IncreaseCodeReuse.

(2) reduce the maintenance burden, encapsulate and modify unique codeProgramAnd does not affect each other.

2. Data encapsulation is used to solve the problem that global variables are not easy to maintain.

3. Polymorphism: in order to enhance the elasticity of inheritance, it is often necessary for the subclass to override the method of the parent class (the function name remains unchanged ).

4. abstraction is used to reduce the maintenance burden after the program version is updated. Feature providers and users are separated and independent from each other.

5. interfaces are provided to establish specifications between function providers and users in order to achieve imaging. Function providers and users provide and use these functions in accordance with this specification. The specification of this function is interface. The interface defines constants, function names, and function parameters. If the interface is not changed, the function provider can rewrite the code in the function without affecting the user's use of these functions.

 

Advantages of Abstraction:

. You can compile a class for function processing with the same interface.

For example, the employee salary is paid according to the position and length of service. You only need to define an interface for calculating the salary and then implement it separately.

. Used for future function expansion.

. Independent feature providers and users.

6. Generally, applications are inherited from the company's internal development to reuse the Code Compiled by other designers. interfaces are mostly used by two departments or more than two companies, it is used to develop public specifications for your own applications respectively.

7. Class is the basic unit of Data encapsulation. Is the minimum unit for repeated use.

Class Members: variables, methods, attributes, events, constants

An object is an entity of a class in the memory. A class is used to describe the data of an object in the memory. That is to say, the class is used to define the object's appearance.

8. Static methods can be called directly without the creation of class objects, that is, without the new instantiation: Class Name. Method Name ()

Static variables are usually used to configure global shared blocks.

9. After the new object is instantiated, the object will be configured in the heap, and the reference pointer to the location of the object in the heap stack is saved in the stack. The instantiated object is used to store pointers. The instantiated object is placed in the stack, for example:

 

9. Differences between structures and classes: structures are mainly composed of simple numeric types and configured in the stack. Class is a reference type and is configured in accumulation. Structure does not support inheritance.

10. Data encapsulation: use classes for encapsulation.

Encapsulation purpose: encapsulate data and methods to access data through methods. You can control the data access mode.

11. access modifier:

Public: All

PRIVATE: The methods and members of a class can only be used in this class, but cannot be used externally.

Protected: the class itself can be used, and the subclass can also be used. Protected is not accessible by a derived class at will. Access is conditional: When the access must be of a subclass type, the protecte Member of the parent class can be accessed.

Internal: can be used in the same. Net assembly

Protected internal: can be used only for current projects or members inherited from this class.

Note: The declaration method is private by default.

A good object-oriented design usually defines all data members as private, and then provides methods to access the data.

12. Data encapsulation: easy to control data and modify.

13. Static members:

The data in the object is private. Only the object can operate on the Data. Other Objects cannot. Sometimes it is not good to store the data shared by all objects in each object. For example, all employees of a company belong to the same company, if you define a member variable that stores the company name in the employee class, you must use the object name after instantiating the object. the method name () is used to access the company name information. If the company name changes, maintenance is troublesome, and the computer needs to allocate memory and hard disk space. To solve this problem, this type of data is stored in the global memory block during implementation. That is to say, the data is not operated by the object, but by the class level and cannot be operated by the object. However, global data is not stored in the class. Therefore, global variables cannot be used instead of static data. Static classes are used to describe the common information of classes.

Static data is declared in the class, so it can reflect the characteristics of encapsulation. Even if the class that defines static data is not instantiated, the static data still exists. Therefore, you can operate static data without creating object entities.

The compiler automatically initializes static variables when loading classes. That is to say, if static member variables are not assigned values, the compiler automatically assigns values to the variables during compilation. The string type is a null string, the int type is 0, and the bool type is false. The life cycle is limited to the application life cycle.

14. Static Methods:

Static methods are used to perform operations on static data. Static Methods belong to Classes and are class-level, not objects that can be operated. Static methods cannot use this reserved word.

Generally, the compiler treats static data and static methods as global variables or global functions during compilation. The default static data is private.

Static methods cannot access non-static data and non-static methods.

In Windows applications, the main () method is defined as public static. You can directly run main () without instantiating runtime. Static methods can only be called using classes, and non-static methods can be called using instances.

15. This operand

This operand is a reference pointer to this object. That is, after instantiation, you can use this to access this object. It can also be used to solve the problem of the same name: This. Name = Name (parameter ). This can also be used to return the reference of the current object: This. Name = Name; return this.

16. The default class access modifier is private.

17. The internal modifier is the access modifier for type and type members. Only files of the same assembly can be accessed by internal types or members. Assembly: A local. EXE or. dll file is an assembly.

18. Reference Type:

Real Value Type: contains real data and cannot be null. Stack.

Reference Type: A Reference pointer pointing to actual data. Accumulation.

 

Encapsulation, inheritance, and Polymorphism

Inheritance:

1. the C # language provides two inheritance Methods: Implement inheritance (only one inheritance is allowed) and interface inheritance (multiple inheritance is allowed)

UML

Class Name

Employee

Member variables

-CompanyName: String

-Employeeid: int

-Employeename: String

-Employeesalary: String

Method

+ Setid (in ID: string): void

+ GETID (): String

+ Setcompanyname (in compantname: string): void

 

 

 

Member variable:-companyName: String string type. "-" indicates the private access modifier. The bottom line of the member variable indicates static.

Method: + setid (in ID: string): void in indicates that the input parameter void indicates that no return value is returned, and an underscore indicates static

Subclass cannot inherit the constructor and destructor of the parent class.

The default inheritance is public.

2. Base: indicates that the constructor of the parent class is called in the base class. Note that the number and type of parameters of this constructor must be the same as that of a constructor in the base class. Another usage of base is to call the member (member method) of the base class, such:

Parent class:

Namespace classtest

{

Public class employ

{

Public employ (string employeenum)

{

}

Private string employeenum; // employee ID

Public void setemployeeid (string employeenum) // you can specify the employee ID.

{

Employeenum = employeenum;

}

}

}

Subclass:

Namespace classtest

{

Public class u_base: employ

{

Public u_base (string employeenum)

: Base (employeenum) // base indicates the constructor in the base class. The parameter type and number must be the same.

{

Base. setemployeeid (employeenum); // The base here is used to call the member methods in the base class.

}

}

}

3. Override the override parent class method of the base class, which is a manifestation of polymorphism and is a redefinition of the methods in the base class. The method name and parameters are the same as those in the base class, but the internal code of the method is different. (Overloading is a manifestation of polymorphism in a class, with different parameters)

4. Virtual Methods

Polymorphism. The default methods in a class are non-Virtual. Different functions cannot be implemented using the same method in the same class.

The virtual method cannot be static. Because static methods are class-level, polymorphism can only be reflected at the object level.

Virtual cannot be modified using private; otherwise, it cannot be rewritten in the subclass.

The virtual method can have specific implementations, but the abstract method cannot have specific implementations. The abstract method must be implemented using override in the subclass.

Override rewriting in the virtual method quilt class in the base class is that the modifier of the method (the permission size of the modifier does not exist and must be the same), name, parameter, and return value must be the same.

Virtual and override cannot be used at the same time, because the override method implies virtual.

Override cannot be used with private or static methods, because static methods are class-level, while polymorphism can only be reflected at the object level. The override method must be rewritten with the virtual method, if it is set to private, it cannot be changed.

Virtual is used to modify the method or attribute definition of the base class. Virtual members or virtual functions can rewrite their functions in subclass. Virtual is used in the base class. The virtual method can be called by instantiated members.

The specific usage is to define the virtual method in the base class, and then rewrite the override function in the subclass as needed. Override cannot be used to override the method without the virtual declaration; otherwise, an error occurs.

If the base class and subclass have the same method, the base class's method is overwritten, and the base class can call its own method. The subclass calls this method as its own method.

Note: Both the virtual method in the base class and the override method in the subclass class can be called by their respective classes.

5. Final class cannot be inherited, and override is not allowed for the methods in it.

6. New keywords

The method defined in the subclass is used to replace the method in the base class. However, the method in the base class can still be called by the base class instance, but cannot be used by the quilt class instance. The new method in the subclass can only be called by the subclass instance. The effects of using new and directly defining a method in the subclass and using the same method in the base class are the same, but there is a warning.

7. Sealed class (such as Enum and string) and sealed Method

The sealed class prevents the class from being inherited, that is, the sealed class cannot be used as the base class. The sealed method cannot be overwritten by override.

8. The type pointer of the base class can be pointed to the subclass B: A {…} in C {...} A A = new A (); B = new B (); a c = B; Here C is an instance of A. If C. method Name (), the method in a is called. Of course, if a has a virtual method, a C = B will call a first, and then find that the method in a is modified by virtual, and the method in B will be further checked, if the methods in Class B are modified by virtual, the methods in Class A and Class B are irrelevant. If the methods in Class B are modified by new, this indicates that this is a new method ,, is the method in a finally called. If the method in B is modified with override, it indicates that this is a new method and the method in B is finally called.

9. Interface

In order to achieve abstraction, a public specification must be provided between the definition and use of the function, so that both the provider and the user can operate according to the specification. This specification is the interface. The interface only defines the specification and has no specific implementation (the abstract class can have specific implementation). The interface supports multi-inheritance and the class supports single inheritance. The method defined in the interface cannot contain block content, that is, there should be no braces: public interface job {double C () {}}; this is incorrect, the methods defined in the interface are implicitly public. If public is added during the definition, an error is returned. Once the class inherits the interface, it should implement all the methods in the interface, and the implementation should be exactly the same as the method in the interface (modifier, return value, method name, parameter ), methods that inherit from interfaces can be modified with the virtual keyword for self-class inheritance. However, methods in the interface cannot use virtual or modifiers.

10. abstract classes and abstract methods

Abstract classes cannot be instantiated like interfaces. Abstract methods are implicitly virtual, so they cannot be used with virtual. Abstract METHODS can only be defined in abstract classes. Abstract METHODS can only be defined and cannot contain braces. abstract methods must be implemented in subclasses. Similar to interfaces, the difference between abstract methods and virtual methods is that virtual can have an implementation part, that is, braces. Abstract classes can inherit from non-abstract classes and abstract classes.

Abstract classes and interfaces cannot be modified using sealed. Interfaces cannot contain abstract methods. abstract methods can use ovreride and new modifiers.

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.