In-depth class methods, in-depth class methods
I. C # keyword extension explanation:
1. new:
1) Open up space
2) Call Construction
3) instantiate an object
2. this:
Instance of the current class, used to distinguish between input parameters and member variables
3. void
The void modifier indicates that the return value type is null, and does not indicate that no return value is returned.
Ii. constructor:
1. Constructor
Features:
The method name is the same as the class name.
No Return Value Type
Initializes the object.
Note: constructor cannot be defined as void.
According to the general experience, during development, we generally do not perform anything other than class initialization in the constructor, and do not try to call the constructor explicitly.
2. No parameter construction
Syntax:
Access modifier Class Name () {// method body}
Sample Code:
public Book() { }
3. constructor with Parameters
Syntax:
Access modifier Class Name (parameter list) {// method body}
The parameter list is generally used to assign values to attributes of a class.
Advantages:
Better flexibility
Dynamically control object features through Parameters
Sample Code:
Public Book (int price, string author, string name) {// instance of the current class, used to distinguish between input parameters and member variables. This. price = price; this. author = author; this. name = name ;}
4. Privacy Constructor
When no manual constructor is written, the system automatically generates a constructor.
Why does the system automatically generate a construction without parameters?
In order to give an advanced framework to the future, the advanced framework can create an object for our class through the no-argument structure at the underlying layer.
3. Method Overloading
1. constructor overload
Public class Player {// The constructor can also constitute an overloaded public Player () {} public Player (int age ){}}
2. Method Overloading
Features:
Same method name
Different method parameter types or different number of parameters
In the same class
Note that methods with the same method name and parameter list have different return value types, and cannot be called method overloading.
public void Say() { } public string Say(int age) { return "1"; } public void Eat(int age,string name) { } public void Eat(string name, int age) { }
4. input variable parameters that mimic the WriteLine method of the Console system class
Public class MagicClass {// param parameter params parameter set Variable Parameter public void Say (params object [] arg ){}}
: Method overloading not only avoids naming troubles, but also enables callers to directly call a method without having to judge the method name.
4. Interaction between objects
1. Overview:
In the object-oriented world, everything is an object.
Objects and objects are independent of each other and do not interfere with each other. However, with some external force, objects start to work together.
Each class has its own features and functions. We encapsulate them as attributes and methods.
Interaction between objects through attributes and Methods
It can be considered that the parameters and return values of methods are messages transmitted by objects.
2. Instance
// Remote control public class Contol {public void Open (TV TV) {TV. open ();} public void Close (TV TV) {TV. close ();} public void ChangeChannel (TV TV, string channelName) {TV. changeChannel (channelName) ;}/// <summary >/// TV //</summary> public class TV {// maintain a state bool state = false; // close public void Open () {if (state = false) // close {state = true; // switch the status to the on-premises Console. writeLine ("the TV is on !!!!! ~~~~~ ") ;}} Public void Close () {if (state = true) // open {state = false; // switch the status to the shut-off status Console. writeLine ("the TV is off !!!!! ~~~~~ ") ;}} Public void ChangeChannel (string channelName) {if (state = true) {Console. writeLine ("You have successfully switched to" + channelName) ;}} static void Main (string [] args) {// 01. buy a TV TV = new TV (); // 02. buy a remote control Contol control = new Contol (); // 03. operation control. open (TV); control. changeChannel (TV, "Phoenix TV"); control. close (TV );}
Experience:
Once the program reports an error "the object is not referenced to the instance of the object", we can determine that an object is not instantiated. In this case, you can use the error code to find the object that is not instantiated and modify it accordingly.