C # keyword (base, protected, override ,....)

Source: Internet
Author: User

(1) Protected

TheProtectedKeyword is a member access modifier. A protected member is accessible from within the class in which it is declared, and from within any class derived from the class that declared this member.

// The method of proteted modification (this class and the derived class can be accessed internally)

ClassA {Protected IntX =123;}ClassB: {VoidF () {=NewA (); B=NewB (); A. x=10;//ErrorB. x =10;//OK}}

(2) Base

TheBaseKeyword is used to access members of the base class from within a derived class:

    • Call a method on the base class that has been overridden by another method.
    • Specify which base-class constructor shoshould be called when creating instances of the derived class.

(Members used to access the base class are limited to. Base. Method Name () within the derived class ())

A base class access is permitted only in a constructor, an instance method, or an instance property accessor.

(You can only use base to access the base class in the constructor, instance method, and instance attribute of the derived class (derived class)

 //  Keywords_base.cs  //  Accessing base class members  Using System;  Public   Class  Person {  Protected   String SSN = "  444-55-6666  "  ;  Protected   String Name = "  John L. malgraine  "  ; Public   Virtual   Void  Getinfo () {console. writeline (  "  Name: {0}  "  , Name); console. writeline (  "  SSN: {0}  "  , SSN );}}  Class  Employee: person {  Public  String Id = "  Abc567efg  "  ;  Public   Override   Void  Getinfo (){  //  Calling the base class getinfo method:           Base  . Getinfo (); console. writeline (  "  Employee ID: {0} "  , ID );}}  Class  Testclass {  Public   Static   Void  Main () {employee e = New  Employee (); E. getinfo ();}} 

Result:

Name: John L. malgraine

SSN: 444-55-6666

Employee ID: abc567efg

(3) Virtual-overrid (required)

The base class uses the virtual modifier to override in the derived class.

 Class  Testclass {  Public   Class  Shape {  Public   Const   Double Pi = Math. Pi;  Protected   Double  X, Y;  Public SHAPE (){}  Public SHAPE ( Double X, Double  Y ){  This . X = X;  This . Y = Y ;}  Public   Virtual   Double  Area (){  Return X *Y ;}}  Public   Class  Circle: Shape {  Public Circle ( Double R ): Base (R, 0  ){}  Public   Override   Double  Area (){  Return PI * x *X ;}}  Class  Sphere: Shape {  Public Sphere ( Double R ): Base (R, 0  ){}  Public   Override   Double  Area (){  Return   4 * PI * x *X ;}}  Class  Cylinder: Shape {  Public Cylinder ( Double R, Double H ): Base  (R, h ){}  Public   Override   Double  Area (){  Return   2 * PI ** x +2 * PI * x * Y ;}}  Static   Void  Main (){  Double R = 3.0 , H = 5.0  ; Shape C = New  Circle (r); shape s = New  Sphere (r); shape l = New Cylinder (R, H );  //  Display Results: Console. writeline ( "  Area of Circle = {0: F2}  "  , C. Area (); console. writeline (  "  Area of Sphere = {0: F2}  "  , S. Area (); console. writeline (  "  Area of cylinder = {0: F2}  " , L. Area ());}}  /*  Output: Area of Circle = 28.27 area of Sphere = 113.10 area of cylinder = 150.80  */ 

 

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.