C # notes - [] object-oriented base (B) Method and method overload

Defining and using (a) Method

Definition 1) Method

The method is characterized in the dynamic objects and represents what the object can do;
Type: instance methods, static methods, (constructor, abstract methods, virtual methods);

The method defined requirements:

Here Insert Picture Description

note:
  • Access modifiers: default is private, by definition needs to be public;
  • The naming method: generally "verb" or "verb phrase moving", Pascal nomenclature, not begin with a number;
  • Parameter list: added as required; no parameter may (not); Requirements parameter list: <type> <parameter name>, separated by commas;
  • Need to return value when using the return return; return later statement can not have other statements; no return value, the use of void;

Call 2) method

Here Insert Picture Description

Note: when a calling method, the order parameter is generally defined to be consistent with the time sequential method, but may be inconsistent, required when calling the method to explicitly write the name of the parameter;

Common errors (two) method definition

1) Forget the return value

return of the role:
  1. Unconditional jump, immediately jump method currently performed;
  2. Returns the result of the method;

2) Return value defines the data types and the return type

Solution:
  1. Editor's return type of the method defined;
  2. Modify the actual type of the actual return;

3) Variable range, in use, beyond the scope

  1. The scope of local variables: a pair of curly braces between;
  2. Local variables priority over the global variable (principle of proximity);
Classification and scope of variables:
  • Internal process variables are called local variables, such variables can only be used within the process;
  • Methods defined within an external, class variables, called "member variables" (also called field); scope class can then use internal or external, but is rarely used outside the class using the property [external]

(C) Classes and Objects of knowledge summary

1) Object Oriented Features

1, the package:
  • Concept: a package refers to the packaged together or a plurality of small objects, and to complete a new object is rendered;
  • Action: a package such that the internal features of an object becomes hidden, so that more secure; hide the internal implementation details;
Software package:
  • Packaging categories: class is the smallest program unit; is encapsulation of attribute method; public interface data type members are provided externally accessible;
  • Module package: a plurality of classes may be encapsulated into a module, consisting of a larger program components;
From the understanding of OOP:

When outside calls, you can not care about the internal implementation details, fully embodies the object-oriented "high cohesion, low coupling";

We understood from reusing row objects:

A package, use everywhere;

2) About variables

Of a variable: refer to memory address, easy to remember
Variable scope:
  1. Local variables: internal a method of exchanging data, method execution is completed, as the return value if no, then quickly clean; scope only inside this method;
  2. Member variables: definitions between classes and methods, multiple internal method of exchanging data; providing data of external (outside the class) to think through the property, clearing uncertain variables;

Overload (d) method

Is substantially similar to the first multi-language meaning; defines a set of methods, a method of displaying only the outside, the method is actually loaded compiler automatically selected according to the type and the number of arguments passed;
Here Insert Picture Description

1) the advantages of method overloading

  • Reducing the class external interface (only one way), to reduce the complexity of the class
  • User-friendly (the same function as the method name) and identified

2) method overload condition

  1. The name of the method must be the same;
  2. The method number of parameters or parameter types are not the same;

3) independent of method overloading

  • Regardless of method overloading and returns the value;

(Five) static method and reload

Role 1) static keyword

May be modified classes, methods, member variables, called after the modification: static class, a static method, a static field;

Call the static method: class name method name; different from the non-static: object name the method name.
Here Insert Picture Description

Note: Static members are not created with objects created; instead loaded into memory when the program starts; it is a static member on the memory usage is very large;
Static method is also suitable for heavy method;
Static member experience:
  • Static members are transferred to the program is running in memory and will not be recovered GC before the system is not closed;
  • When members of the class to use very frequently, consider using static modification, but do not use too much;
  • Static members can not directly call instance members (static methods can not call an instance method directly); instance members exist only when the object is created;
  • Instance members can call a static member, because it has been loaded into memory;
  • Static methods can be overloaded;
Published 34 original articles · won praise 0 · Views 997

Guess you like

Origin blog.csdn.net/forever_008/article/details/104076713
Recommended