Object-oriented is not a substitute for process-oriented
Class and object. "Person" is a class, and "Zhang San" is an object of the class "person.
Classes are abstract and objects are specific. A button is a class, and a button is an object. An object can be called a class instance. A class is like an int. An object is like a 10. Field (a variable related to an object). A field is the state of the class. This category includes the name, age, height, and other fields. Classes do not occupy memory, and objects only occupy memory.
Method: actions that can be performed, such as greetings and meals.
Class inheritance, the class can have an inheritance relationship between, for example, the computer class can be from"
Electrical appliances "class inheritance, the advantage is that the" computer "class only needs to define its own unique fields and methods, that is, as long as the memory size is defined, you can use the fields such as the CPU model or the optical drive pop-up. Parent class. Base class. Computer is a subclass of electrical devices. Reuse.
Object-oriented features: encapsulation, inheritance, and polymorphism.
There are no problems in the world of symptom objects.
Member access level
Fields, methods, and attributes can all be called class members. They all need to define the access level. The purpose of the access level is to control where members can be accessed, so as to "encapsulate" the object.
Several access levels: Public (accessible anywhere) and privae (default level. Can only be accessed by members of this class ). There are two levels of internal/protected, which will be discussed later.
Class person {
Private string name;
Public void givename (string name ){
Name = Name; // reject unpleasant names
}
Public void sayhello (){
Console. writeline ("Hello, I am {0}", name );
}
}
Person Tom = new pewson ();
Tom. givename ("Tom ");
// Tom. Name = "Tom ";
Tom. sayhello ();
Never make the field public
Attribute
Usage: uppercase letters at the beginning of a property and lowercase letters at the beginning of a field
Class person
{
Private int age;
Public int age
{
Get {return age ;}
Set {age = value ;}
}
Public void sayhello ()
{
Console. writeline ("My age is {0}", age );
}
}
Only set or get can be used to define read-only or write-only attributes (write-only is not common)
You can set the access level for set and get.
For example, restrict the setting of invalid values
What is the difference between a field and a property? The attribute seems to be a field, not a field, and can be used for illegal value control and can be set to read-only.
The Set/get block is actually the GET _ ***/SET _ *** method.
Object-oriented 04
Object-oriented chatbot
The robot has different names and maintains its own fulllevel. It can be sayhello (my name is ***), eat (INT foodcount), or speak ), to handle abnormal situations (the wrong feeding number is too much to support), there are two robots to choose from. At the beginning, the chatbot is selected through numbers 1 and 2.
Why can't the reflector anti-compiler be found in?
Object-Oriented Programming basics 8 Constructor
The constructor is used to create objects and can initialize objects in the constructor.
The constructor is used to create special functions of an object. The function name is the same as the class name, and there is no return value, so no void is required.
The constructor can have parameters. When a new object is added, the function parameters can be passed.
Constructors can be overloaded, that is, there are multiple constructors with different parameters.
If no constructor is specified, the class has a default no-argument constructor.
If a constructor is specified, no default constructor is available. If no constructor is required, you must write the constructor yourself.
Object-Oriented Programming basics 10 inheritance
Public class Chinese person
{
Public String hukou {Get; set ;}
Public void Kongfu (){......}
}
Chinese p1 = new Chinese ();
P1.name = "Li Lei ";
P1.hukou = "Chaoyang District, Beijing ";
P1.sayhello ();
P1.kongfu ();
If no parent class is specified during class definition, the parent class is the object class, and the object class is any class.
Directly or indirectly.
Exception and Exception Handling
Traditional error representation: error code. For example. You need to know the meaning of different error codes. If you do not handle the error codes, the program may fall into unpredictable errors.
Disadvantages of the error code: it is difficult to find the error code if it is not processed, and it is troublesome to process the error code each time;
It is difficult to see the cause of the error; it is easy to make the program into an uncertain state.
Try catch. Exception ex exception is also an object.
Main attribute of exception class: Message/stacktrace
When an exception occurs, the program exits by default and subsequent code will not be executed.
Code after catch will continue to be executed.
Do not eat exceptions
Throw your own exceptions
Constants and static members
Const constant. The constant name must be capitalized. It will not be changed to a constant.
Global variable. Static variables.
Methods that can be used without new: static method. Static Method is actually a common function.
You can call other static methods, fields, and attributes in the static method. You can call static methods and fields in non-static methods.
Static class. The new class cannot be a static class. Static classes are generally used to implement
Function library. * ** Helper, sqlhelper, pagehelper
Namespace
Namespace (namespace), used to solve the problem of duplicate class names, can be seen as "class folder"
When using other classes in the Code, the namespace of the using class is required.
System. Collections. arraylist, quick introduction method, right-click ---> resolve
.
Why do I not need to write using for classes such as convert/console?
If the code and the used class are in a namespace, no using is required.
You can modify the default namespace. Therefore, do not think that you do not need to use using in the same folder.
Indexer
C # provides the access method based on the indexer.
How to define the indexer: String This [int Index] {get {return "";}
Set {}}, string is the index type, and [] is the parameter list. The index writer calls the set code block and uses the value inside the set to obtain the value set by the user. The get code block is executed when the read operation is performed.
There can be more than one index parameter, and the type is not limited to int. It can be almost any type.