Error | object
Using System;
Namespace Demo
{
Class ClassA
{
protected string a = "test";
public void Display ()
{
Console.WriteLine (a);
}
}
Class Classb:classa
{
protected string A = "another Test";
}
/**////<summary>
Summary description for Class1.
</summary>
Class Class1
{
/**////<summary>
The main entry point is for the application.
</summary>
[STAThread]
static void Main (string[] args)
{
//
Todo:add code to start application
//
CLASSB instance = new ClassB ();
Instance. Display ();
((ClassA) instance). Display ();
Console.ReadLine ();
}
}
}
At this point, the contents of the console output are both test
But if you make the following modifications:
Using System;
Namespace Demo
{
class ClassA
{
private String _a = "Test";
protected virtual string a
{
get
{
return _a;
}
Set
{
_a = value;
}
}
public void Display ()
{
Console.WriteLine (a);
}
}
Class Classb:classa
{
private string _deriveda = "another string";
protected override String A
{
Get
{
return _deriveda;
}
Set
{
_deriveda = value;
}
}
}
/**////<summary>
Summary description for Class1.
</summary>
Class Class1
{
/**////<summary>
The main entry point is for the application.
</summary>
[STAThread]
static void Main (string[] args)
{
//
Todo:add code to start application
//
CLASSB instance = new ClassB ();
Instance. Display ();
((ClassA) instance). Display ();
Console.ReadLine ();
}
}
}
So the console output is: Another test
If we make the following modifications,
Using System;
Namespace Demo
{
class ClassA
{
private String _a = "Test";
protected virtual string a
{
get
{
return _a;
}
Set
{
_a = value;
}
}
public virtual void Display ()
{
Console.WriteLine (a);
}
}
class Classb:classa
{
private string _deriveda = "another string";
protected New string a
{
get
{
return _deriveda;
}
Set
{
_deriveda = value;
}
}
Public new void Display ()
{
Console.WriteLine (a);
}
}
/**////<summary>
Summary description for Class1.
</summary>
Class Class1
{
/**////<summary>
The main entry point is for the application.
</summary>
[STAThread]
static void Main (string[] args)
{
//
Todo:add code to start application
//
CLASSB instance = new ClassB ();
Instance. Display ();
((ClassA) instance). Display ();
Console.ReadLine ();
}
}
}
The result of the program's output is:
Another test
Test
Interesting thing because I found a bug in the source code of the project that was caused by misuse
As follows:
Using System;
Namespace Demo
{
Class ClassA
{
protected string a = NULL;
public void Display ()
{
Console.WriteLine (a);
}
}
Class Classb:classa
{
protected string A = "another Test";
}
/**//**//**////<summary>
Summary description for Class1.
</summary>
Class Class1
{
/**//**//**////<summary>
The main entry point is for the application.
</summary>
[STAThread]
static void Main (string[] args)
{
//
Todo:add code to start application
//
CLASSB instance = new ClassB ();
Instance. Display ();
Console.ReadLine ();
}
}
Then the NullReferenceException is generated in the running of the program
Posted on 2005-06-15 16:32 Dark Reading (1468) Comments (9) Edit collection to 365Key
Comments
# Re: A common error response in C # and similar object-oriented systems (estimated in Java)
In a derived class, if you want to write a function that replaces a function with the same name as the parent class, you must use the new operator, or the actual execution effect is indeterminate.
For example, if you want a CLASSB instance to perform its own display without override, define display like this:
Public new void Display ()
{
...
}
2005-06-15 16:58 | Teddy ' s knowledge Base
# Re: A common error response in C # and similar object-oriented systems (estimated in Java)
You have this kind of problem, can only explain OO not home, but also to learn a good ah!
2005-06-15 17:21 | Hon Young
# Re: A common error response in C # and similar object-oriented systems (estimated in Java)
The effect of actual execution is OK, whether it's in. NET or in the Java system, how to determine in Java I am not very clear, the problem is that these object-oriented rules that determine the specific behavior of the rules of the best to be standardized in the ISO way, whether in the C++,.net, In Java or Python, we can all get the same semantics
2005-06-15 17:26 | Mixed it Up
# Re: A common error response in C # and similar object-oriented systems (estimated in Java)
is to understand OO errors.
Class ClassA
{
protected string a = "test";
public void Display ()
{
Console.WriteLine (a);
}
}
Class Classb:classa
{
protected string A = "another Test";
}
Equal to ClassA there is a string A,CLASSB also has a string a, the difference is 2 variables.
2005-06-15 17:34 | Pierce
# Re: A common error response in C # and similar object-oriented systems (estimated in Java)
No... I understand the third case .... After the cast type, the variable also uses the variable of the base class ... Is it that I have always misunderstood ...
2005-06-15 18:44 | Patch
# Re: A common error response in C # and similar object-oriented systems (estimated in Java)
This output is consistent with the OO principle
2005-06-15 20:27 | Man
# Re: A common error response in C # and similar object-oriented systems (estimated in Java)
It's called a mistake? Learn oo and do things again, dizzy.
2005-06-16 09:36 | Yun
# Re: A common error response in C # and similar object-oriented systems (estimated in Java)
To all:
I can suggest that you take a look at Houjie's C + + object model, and you can really understand why. For example, a subclass object is coerced into a base class object, in fact the compiler processing is to treat the object as a base class object (that is, the object's slip). Of course the method invoked is also a method of the base class, regardless of the subclass.
To Hon Young: In fact you are wrong, this cannot be said to be the category of Oo (Oo is just a thought), should be the compiler object model category (is a storage technology)
Anyway, you know, after compiling, all the memory distributions and calls (address offsets) have been determined ....
Please visit the "Hope this article helps to understand C # 's object Model": http://www.cnblogs.com/caomao/archive/2005/06/16/175459.html
2005-06-16 10:08 | Zendyhu
# Re: A common error response in C # and similar object-oriented systems (estimated in Java)
First collection, make a mark.