Object-oriented learning 2: Inheritance

Source: Internet
Author: User

Object-oriented learning 2: Inheritance

1. Accessibility of members:

Access Modifier

Meaning

Public

The external and internal scope of member definitions are completely visible, that is, access to public members is unrestricted.

Protected

The Member is only visible to the definition class and its derived class.

Internal

The Member is visible anywhere inside the Assembly that contains it, including defining any scope beyond the class and within the Assembly.

Private

The members are only visible within the defined class. This is the strictest form of access and is the default access level for class members.

2. Member hiding:

Inheritance can be extended, but cannot be removed. For example, the public methods available in the base class are available in the instances of the derived classes and derived classes. These functions cannot be removed from the derived class. See the following code:

Namespace hidden member {public class A {public void DoSomething () {Console. writeLine (". doSomething ") ;}} public class B: A {public new void DoSomething () {Console. writeLine ("B. doSomething ");} public void DoSomethingElse () {Console. writeLine ("B. doSomethingElse ") ;}} class Program {static void Main (string [] args) {B B B = new B (); B. doSomething (); B. doSomethingElse (); A a = B;. doSomething (); Console. readKey ();}}}


Analysis:

The program running result is:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/21410W419-0.jpg "title =" 1.jpg"/>

Although Class B hides the DoSomething Implementation of Class A, it does not remove it, but is hidden when called through B reference. You can see in the Main method that this can be easily bypassed. By implicit conversion, instance B can be converted to instance A. The implementation of A. dosomething can be called through instance A reference. Therefore, A. dosomething is not lost, but hidden. You need to do more work to find it.


3. override and new methods:

To override a method in a derived class, the override modifier must be used to mark the method. Otherwise, the compiler prompts a warning that the new modifier or override must be provided in the derived method. Besides, the compiler uses the new modifier by default. Let's look at the following code:

Namespace inheritance and virtual methods {public class A {public virtual void SomeMethod () {Console. writeLine (". someMethod ") ;}} public class B: A {public void SomeMethod () {Console. writeLine ("B. someMethod ") ;}} class Program {static void Main (string [] args) {B B B = new B (); A a = B;. someMethod (); Console. readLine ();}}}

Analysis:

The running result is: Call A. SomeMethod.

The New Keyword breaks the virtual function chain in this hierarchy. When a virtual method is called through object reference, the called method depends on the method table at runtime. If the method is virtual, at runtime, the system searches for the hierarchy to find the lowest-level derived version of the method in the inheritance system, and then calls it class B ). However, if you encounter A method marked with the new modifier during the search process, it will return to the upper-level class in the inheritance system. Class A here) and use the method in this class. Because C # uses the new modifier by default when neither new nor override exists, this is why A. SomeMethod is called.

If B. SomeMethod is marked as override, this code calls B. SomeMethod.


4. Base Keyword: base allows access to the Base class implementation of an instance. Let's look at the following code:

Using System; using System. collections. generic; using System. linq; using System. text; namespace inherits {public class A {private int x; public A (int var) {this. x = var;} public virtual void DoSomething () {Console. writeLine (". doSomething ") ;}} public class B: A {public B (): base (123) {}public override void DoSomething () {Console. writeLine ("B. doSomething "); base. doSomething () ;}} class Program {static void Main (string [] args) {B B B = new B (); B. doSomething (); Console. readKey ();}}}

Analysis:

In this example, we can see that the base keyword is used in two places,

B. doSomething implementation: B. when DoSomething is used,. doSomething implementation. Therefore, you can. doSomething calls A directly using the base keyword in implementation. doSomething.

Generally, calling the virtual method of an instance calls the underlying implementation of the virtual method, that is, B. DoSomething. However, if you use the base keyword to call the derived method at the bottom of the base class. Because A is A base class and A. DoSomething is the base class method of DoSomething relative to Class A, base. DoSomething will call A. DoSomething. In this way, you can implement an override method and borrow the implementation of the base class.


This article is from the "Left pupil | @ "blog. For more information, contact the author!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.