C # sharp experience seventh lecture and attribute

xiaoxiao2021-03-06  99

Nanjing University of Posts and Telecommunications Li Jianzhong (Cornyfield@263.net)

area

Domain (Field), also known as member variable, which represents a storage location, which is an indispensable part of C #. The type of domain can be any data type in C #. However, for other reference types that remove string types, the operation of some classes of constructor is involved in initialization, we will not mention that we will not mention this part of the content as a "class" interface inheritance and polymorphism "I explained in one."

The domain is divided into instance domains and static domains. The instance domain belongs to the specific object, which is proprietary for a particular object. The static domain belongs to the class and is shared for all objects. C # Strictly specify the instance field can only be obtained by objects, and the static domain can only be obtained by class. For example, we have an object of Myclass object MyObject, instance fieldfield in MyClass, can only get this: MyObject. Instancefield. And myclass static domain staticfield (access limit is public) can only get this: Myclass.staticField. Note that the static domain cannot be obtained by objects like traditional C , that is, myObject.staticField is wrong, cannot be compiled by the compiler.

The domain access restrictions reflect the principle of encapsulation of object-oriented programming. As mentioned earlier, the access restriction modifier in C # has five, which is applicable to these five pairs of domains. C # just extension C original Friend modifiers with INTERNAL. When there is a need to make some of the two classes, we declare these types of domains as INTERNAL and then put them in a combination. If you need to be inherited to their inheritance, it is possible to declare it as protected internal. In fact, this is also the original meaning of the composition - encapsulates the logical class combination.

C # introduced the ReadOnly modifier to represent a read-only domain, constly indicates that the usual is not constant. As the name suggests that the only way to write is not written, the constant constant cannot be modified, what is the difference between these two? The read-only domain can only assign a value during initialization-declaration initialization or constructor, and other places cannot perform assignment operations for read-only domains, otherwise the compiler will report an error. A read-only domain can be an instance domain or a static domain. The type of read-only domain can be any type of C # language. However, constant constants must be assigned to the declaration, and the compiler can calculate this determined value during compilation period. Const modified constants are static variables that cannot be obtained for objects. The type of Const modified value is also limited, it can only be one of the following types (or can be converted to the following type): Sbyte, Byte, Short, USHORT, INT, UINT, Long, Ulong, Char, Float, Double, Dou, DECIMAL , BOOL, STRING, ENUM type, or reference type. It is worth noting that the reference type here is, because all types of the string type are removed from the NULL value, they cannot calculate their exact values ​​during compiler, so we can declare that the reference type of Const can only be String or other reference type of NULL. Obviously when we declare a NULL constant, we have lost the meaning of the statement - this can also be said to be the embarrassment of C # design!

That is to say, when we need a constant constant, but its type limits its value that it cannot be calculated in the compile time, we can take it to Static Readonly to resolve. But there is still a little different between the two. Look at the two different files below:

//file1.cs

// CSC / T: library file1.cs

Using system;

Namespace mynamespace1 {

Public class myclass1

{

Public static readonly int myfield = 10;

}

}

//file2.cs

// csc /r:file1.dll file2.cs

Using system;

Namespace mynamespace2

{

Public class myclass1

{

Public static void main ()

{

Console.writeline (MyNameSpace1.myclass1.myfield);

}

}

}

Our two classes belong to two files file1.cs and file2.cs, and compile separately. When the domain myfield in the file file1.cs declares that when we need to change my File1.cs, we only need to recompile file file1.cs for file1.dll, when performing file2.exe Will get 20. But if we change Static Readonly to const, we must recompile all files that reference to file1.dll, otherwise we reference myNamespace1.myclass1.myfield will not change as we wish . This is especially notes in the big system development process. In fact, if we can understand that constant constants is calculated when compiling, and substitution to each place referenced to the constant, and readonly is determined at runtime - just initialization After we don't want its value to change again, we can understand the goodness of C # designers, we can completely grasp the behavior of consT and readonly!

Initialization of the domain is a problem that needs special attention in object-oriented programming. The C # compiler defaults to initialize each domain as its default value. Simply put, the default value of the numeric type (enumeration type) is 0 or 0.0. The default value of the character type is '/ x0000'. The default value of the Boolean type is false. The default value of the reference type is NULL. The default value of the structure type is to take the corresponding default value for all types. Although the C # compiler sets the default type for each type, as an object-oriented design principle, we still need to correctly initialize the variable. In fact, this is also a C # recommended approach, and there is no initialization of the domain that causes the compiler to issue a warning message. In the C #, the domain is initialized to initialize both places - declarations, and initialize in the constructor. As mentioned earlier, the domain declaration is actually being executed as an assignment statement as an assignment statement in the first place within the constructor. Example variable initialization will be placed in an instance constructor, and static variable initialization will be placed in a static constructor. If we declare a static variable and initialize it, the compiler will construct a static constructor to put this initialization statement into an assignment statement. As a constant domain of const modifications, we can't count on initialization statements from strict sense, we can think of it as a macro change in C .

Attributes

Attributes can be said to be an innovation in the C # language. Of course, you can also say it. The reason is that the implementation behind it is actually two functions - an assignment function (GET), a value function (SET), which can be clearly seen from the intermediate language code generated. The reason is that it is indeed an appeal of a special interface for object-oriented programming has always been in the language-oriented programming. The design of the understanding of the design is the root of the tool we use this property. C # does not advocate the use of the domain's protection level to public and use the user outside the class or any operation - that is too not Oo, or the specific point is too unsafe! For all the domains other than the class, C # recommends expressing attributes. Attributes do not represent storage locations, which is the foundation of attributes and domains. Below is a typical property design:

Using system;

Class myclass {

Int integer;

Public int integer

{

Get {return integer;}

Set {integer = value;}

}

}

Class test

{

Public static void main ()

{

Myclass myObject = new myclass ();

Console.write (MyObject.integer);

MyObject.integer ;

Console.write (MyObject.integer);

}

}

As we look forward, the program output 0 1. We can see that attributes provide a friendly domain member access interface by providing the programmer to the programmer. The value here is the keyword of the C #, which is the implied parameters of the set when we perform attribute operation, that is, the right value when we execute the attribute write operation.

The attribute provides only three interface operations that read (GET), write (SET), read and write (GET, and SET). These three operations in the domain, we must declare them under the same attribute, instead of separating them, see the implementation below:

Class myclass

{

PRIVATE STRING NAME;

Public String Name

{

Get {return name;}

}

Public String Name

{

Set {name = value;}

}

}

The method of achieving the realization of the Name attribute above is wrong! We should put them together like the previous example. It is worth noting that three attributes (read-only, reading, reading, writing) are considered the same attribute name by c #, see the example below:

Class myclass

{

protected int num = 0;

Public int Num

{

set

{

Num = Value;

}

}

}

Class myclassderived: myclass

{

New public int num

{

get

{

Return Num;

}

}

}

Class test

{

Public static void main ()

{

Myclassderived myObject = new myclassderived ();

//MyObject.num= 1; // Error!

((MyClass) myObject) .Num = 1;

}

}

We can see the properties Num-get {} in myclassderived blocking the definition of attribute Num-set {} in MyClass.

Of course, attributes are far more than just limited to domain interface operations. The essence of the property is also a method, and we can perform some inspections, warnings, etc.

Class myclass

{

PRIVATE STRING NAME;

Public String Name

{

Get {return name;}

set

{

IF (value == null)

Name = "Microsoft";

Else

Name = value;

}

}

}

Due to the nature of the method, the attributes, of course, have a method of modification. Attributes also have five access modifiers, but attribute access modifications are often public interfaces, otherwise we will lose the meaning of the public interface of the class as a class. In addition to the multi-parameters of the method, the modifiers such as the method of the method do not have external, Virtual, Sealed, Override, Abstract and other modifiers are the same as the attribute and method, but because attributes are in nature of two methods, it Some behaviors need us to pay attention. See the example below: Abstract Class A

{

Int Y;

Public Virtual Int x

{

Get {return 0;}

}

Public Virtual Int Y

{

Get {return y;}

SET {y = value;}

}

Public Abstract Int Z {Get; SET;}

}

Class B: a

{

Int z;

Public Override Int X

{

Get {return base.x 1;}

}

Public Override Int Y

{

Set {base.y = value <0? 0: value;}

}

Public Override Int Z

{

Get {return z;}

Set {z = value;}

}

}

This example focuses on certain typical behaviors in inheriting the context in inheriting the context. Here, class A must be declared as Abstract due to the presence of abstract attribute z. The subclass B is referenced by base keywords to reference the properties of the parent class A. The class B can be covered with virtual attributes in class A through Y-SET.

Static properties and static methods can only access the static domain variables of the class. We can also declare external properties as doing external methods.

转载请注明原文地址:https://www.9cbs.com/read-123735.html

New Post(0)