12..23

The definition of the
class and the instantiation of the object The definition of the class: the file name is the class name,
insert the picture description here

Add attributes to the class

Private attribute: String color;
Public attribute: public String color;
Insert picture description here

Instantiation of objects

Class name variable name = new class name();
insert picture description here

● Obtain and modify the attribute value of the object

Object. Property name = value
Insert picture description here

.Define method and use
Define method format:

Access modifier return value type method name (type parameter name of the formal parameter) {method execution content}
return value type

Default: void
Basic type: such as int
array: array name []
string: String
custom class: class name
call method

Object call method: object. method name ();
example: a.method();
method call method: method name ();
example: method();
call method with return value

Use a variable of the same type as the return value
to receive the return value. Return value type variable name = object. Method name ();
Example: String js = a.method( );
Call a method with parameters

Object. Method name (actual parameter 1, actual parameter n);
Example: a.method (eee: "success", i: 5);


The difference between object array and ordinary array

Object arrays can store custom objects.
Ordinary arrays store basic data types
. Definition of object arrays

Class name [] variable name = new class name [length];
method call in the object array

Traverse the method of calling each array member
for(int i=0; i<object array.length; i++){ object array[ i]. Method name(); } 1 2 3




Modifier
1. Attribute Modifier
Serializable: Marshal the object to the remote server by value. When marshaling an object by value, a copy of the object is created and serialized to the server. Any method calls to this object are made on the server.

STAThread: Single-threaded suite, a threading model

MTAThread: Multi-threaded suite, a threading model

2. Access modifier
Type code role
Public access public without any restrictions.
Protected access. Protected instances cannot be accessed.
Private access to private subclasses. Instances cannot be accessed.
Internal access is limited to access within this project. Others cannot access
internal protection. Access protected internal only Limited to access to this project or subclasses, others cannot be accessed.
Third, the class modifier
public: unrestricted

internal: only accessible within this project

Partial: Partial class. You can divide a class into several parts and write them in different files, and they will be merged into one file during final compilation, and each part cannot be scattered in different assemblies. Common scenarios are the front-end units and logic units that are automatically generated when a window or control is defined in Winform.

Abstract: Modified class, which means that an instance of this class cannot be created. Modification method means that the method needs to be implemented by the subclass. If the subclass does not implement the method, the subclass is also an abstract class; and the class containing the abstract method must be an abstract class.

Sealed: Modified class, which means that the class cannot be inherited. Modification method means that the method cannot be overwritten.

Static: Modified class, which means that the object of this class cannot be instantiated, nor can it contain object members; when a class member is modified, the member is a class member and can only be accessed through [class. member name].

Four, member modifier
Public modifier: public

Private modifier: private

Protected modifier: protected

Internal modifier: internal

Virtual: Modified method members, representing virtual methods. The parent class can contain the implementation of this class, and the subclass can override this function.

Override: Indicates that this method is a method that overrides the parent class.

Readonly: Modified field, which means that the field is read-only.

Const: Modified field, indicating that the field is read-only. And you must be able to clearly know the value of this field when compiling, and its value is hard-coded into the program. After modifying the type member, you need to recompile to make the modification take effect.
Readonly cannot modify local variables, const can.

Abstract is used to modify an abstract class, which means that the class can only be used for inheritance as a parent class and cannot be instantiated. Abstract classes can contain abstract members, but this is not required. Abstract cannot be used with new at the same time.

sealed is used to modify the class as a sealed class to prevent the class from being inherited. At the same time, it is meaningless to modify a class with abstract and sealed, and it is also forbidden.

Note: When a class or method is not modified by any modifier, the default is internal.
6. Preprocessor: #region, collapsible code block
Usage:

#region Folded description text

Guess you like

Origin blog.csdn.net/zzxin1216/article/details/111688745