Sealed and final modifiers are not actually a product of a language platform, they have their own locale, but these two keywords are. NET platform, then the use of both geometry, with this article to explore.
I.Sealed
The sealed modifier can be applied to classes, instance methods, and properties. When used in a class, the class is called a sealed class, and the sealed class cannot be inherited; When used in a method, the method is called a sealing method, and the sealing method overrides the method in the base class; When applied to a method or property, the sealed modifier must always be used with the override; The structure is implicitly sealed, so they cannot be inherited.
Description Method:
Error:cannot be sealed because it isn't an override
public sealed string func ()
{
Return "";
}
Ok
public sealed override string Func ()
{
Return "";
}
Methods in Inheritance: (cannot override any method in TestChild2)
Description property:
Public sealed override Double Hours
{
get {return 0.1;}
set {}
}
Description variables:
Error the modifier ' sealed ' is not valid for this item
sealed override string A;
Description Interface:
Interface Itesta
{
Error cannot be sealed because it isn't an override
Sealed string Geta ();
}
Sealed can improve performance optimization?
Some friends think that when an element is marked as sealed, there are 2 reasons to improve the performance of the system:
1 helps JIT inline.
2 the covariance and inversion and late binding are eliminated, allowing the CLR to execute this instance directly.
This seems to make sense, but how much performance will it take to improve performance while losing what?
First of all, the "1th", prompting the JIT inline code is a lot of factors, JIT will not because a class is sealed, it will be loaded into the content (see. Net Discovery Series six-in layman's words. NET real-time compilation mechanism (bottom), which does not conform to the principle of program locality. http://www.cnblogs.com/isline/archive/2009/12/27/1633453.html), and for internal methods of the sealed class, a large part of the reason is also determined by the method slot mapping relationship, The role of sealed remains to be verified.
2nd, because the sealed class is not derived or inherited, it does omit some of the CLR's extra work at run time, but these work is just something like "addressing", because the virtual method table has completed the runtime's correspondence with the compile time, and the purely runtime is only looking for those relationships. So sealed omitted only a part of the more complex addressing relationship, because even if there is no inheritance, there is inevitably a method table application.
And what did this lose? What do you think is the object-oriented reason? Is it to improve performance? Obviously not, the object-oriented is only a high-level language, the final run of code is in the form of sequential processes, the essence of object-oriented is "abstract", it solves the SOFTWARE PRODUCT "control" problem, become uncontrollable and unpredictable risk as a predictable risk, so if you want to improve performance, And the sealed of most of the classes is not a significant weakening of the object-oriented abstraction ability?
Sealed cannot be abstract at the same time?
Perhaps abstract in high-level languages must be implemented and sealed non-inheritable is a contradiction, but IL exposes some different details, let us analyze this IL code:
This code is simple enough to declare a class, but this class is abstract and sealed, guess what modifier this class used to decorate it?
Well, in fact, the corresponding modifier in the high-level language is static.
The static class cannot be instantiated (abstract) and cannot be derived (Sealed), and I think the abstract is Sealed at the same time, but this will make semantics two semantic, in order to avoid this effect is not required in the editor abstract+sealed, The original intention of the static modifier class I think so, actually, when static is decorated with a class, it is a class that contains the implemented abstract+sealed, which cannot be instantiated or derive a new class.
two. Final
the final modifier qualifies variables, fields, methods, and classes. When used in a variable, the variable can only be assigned one time and cannot be modified; When used in a method, the method cannot be overridden or hidden; When used in a class, the class cannot be inherited.
The members of an interface cannot use the keyword, and cannot use final as the abstract class.
It is worth mentioning that if you use a field in the final decorated class, then the field must be assigned a value in the constructor, otherwise it is not possible to invoke the class instance by assigning a value to the field, which is simple, when the class is instantiated, it assigns an initial value to each member field. After that, if you invoke the final field again by instance, it is two times the assignment, which is not allowed. The method of assigning a value to a final variable in a constructor is called "Delay Assignment" (Java), and the corresponding final variable is called "blank Final" (Java).
Final is not a keyword in C #, but often appears in C # interview questions, such as "Final, Finally, finalize the difference", in fact, this has gone beyond the category of C #, these three keywords examined J #,. Net fault-tolerant methods, respectively. NET garbage collection mechanism, oddly, every time I interview C # program personnel, most of the people to final this keyword is not unfamiliar with the sense, but the answer is well-reasoned, it seems that before the interview, early on the online preview, hehe.
Example (excerpt from MSDN, translated):
public class Value
{
public int i = 1;
}
public class FinalData
{
can be considered equivalent to the compile time constant
Final int i1 = 9;
static final int i2 = 99;
Public constants:
public static final int i3 = 999;
Not as a compile-time constant:
Final int i4 = (int) (Math.random () * 11);
static final int i5 = (int) (Math.random () * 11);
Value V1 = new value ();
Final Value v2 = new value ();
Static final Value V3 = new Value ();
Array:
Final int[] A = {1, 2, 3, 4, 5, 6};
public void print (String ID)
{
System.out.println (id + ":" + "I4 =" + I4 + ", i5 =" + i5);
}
public static void Main (string[] args)
{
FinalData fd1 = new FinalData ();
Error:can ' t change value! (I1 is described as FIANL)
fd1.i1++;
Ok. Object isn ' t constant (although V2 is fianl, but the variables are not subject to this constraint)
fd1.v2.i++;
Ok. Not final.
FD1.V1 = new Value ();
for (int i = 0; i < fd1.a.length; i++)
{
fd1.a[i]++; Ok. Object isn ' t constant. (As with the V2 above, the array is final, but the array elements are not constrained)
}
Error:can ' t change handle! (V2 is final)
Fd1.v2 = new Value ();
Error:can ' t change handle! (V3 is static final, equal to constant)
Fd1.v3 = new Value ();
Error:can ' t change handle! (the array itself is final, not new)
fd1.a = new Int[3];
Fd1.print ("Fd1");
System.out.println ("Creating new FinalData");
FinalData fd2 = new FinalData ();
Fd1.print ("Fd1");
Fd2.print ("Fd2");
}
}
Answer:
The dividing line of-------------------------------------------------crab-------------------------------------------------------
FD1:I4 = 0, i5 = 7
Creating New FinalData
FD1:I4 = 0, i5 = 7
FD2:I4 = 8, i5 = 7
Summary:final is a modifier in J # that discards J # in VS2008 and later versions, unlike sealed, where fianl can modify variables and sealed cannot, but you can do this by readonly keywords.
On the performance of the two to enhance the role, I think to be verified, from a theoretical point of view, for the difficult to prove the performance factors and deliberately use this keyword is not worth the candle.
--------------------------------------------------------------------------------------------------------------- ----
Note: This article is reproduced in: http://www.cnblogs.com/isline/archive/2010/08/31/1813396.html, thank the original author!
Final comparison in sealed and J # in C # (reprint)