C # syntax exercise (9): class [1]-access restrictions, methods, fields, and attributes

Source: Internet
Author: User
By default, all classes inherit from system. Object (or object ):
 
Using system; Class myclass1 {} class myclass2: object {} class myclass3: system. object {} class program {static void main () {myclass1 obj1 = new myclass1 (); myclass2 obj2 = new myclass2 (); myclass3 obj3 = new myclass3 (); console. writeline (obj1.tostring (); // myclass1 console. writeline (obj2.tostring (); // myclass2 console. writeline (obj3.tostring (); // myclass3 console. readkey ();}}

  

Class access restrictions and abstract classes, seal class:

Using system;/* Internal: used only for the current project class, which is omitted by default * // * public: public class */class myclass1 {} internal class myclass2 {} public class myclass3 {}/* Abstract: only classes that can be inherited and cannot be directly instantiated */abstract class myclass4 {} internal abstract class myclass5 {} public abstract class myclass6 {} // sealed: class sealed class myclass7 {} internal sealed class myclass8 {} public sealed class myclass9 {} class program {static void main () {myclass1 obj1 = new myclass1 (); myclass2 obj2 = new myclass2 (); myclass3 obj3 = new myclass3 ();/* myclass4, myclass5, and myclass6 are abstract classes and cannot be instantiated */myclass7 obj7 = new myclass7 (); myclass8 obj8 = new myclass8 (); myclass9 obj9 = new myclass9 (); console. writeline (obj1.tostring (); // myclass1 console. writeline (obj2.tostring (); // myclass2 console. writeline (obj3.tostring (); // myclass3 console. writeline (obj7.tostring (); // myclass7 console. writeline (obj8.tostring (); // myclass8 console. writeline (obj9.tostring (); // myclass9 console. readkey ();}}
 
   
 

Method access restrictions:

 
Using system; Class myclass {/* Private: Private method used by the class. This is the default */string Method1 () {return "Method1";} private string method2 () {return "method2";}/* protected: method that can be inherited by subclass */protected string method3 () {return "method3" ;}/ * Internal: methods available for the current project */internal string method4 () {return "method4";}/* Public: Public Method */Public String method5 () {return "method5" ;}} class program {static void main () {myclass OBJ = new myclass ();/* due to access level restrictions, neither Method1, method2, or method3 of myclass can access */console. writeline (obj. method4 (); // method4 console. writeline (obj. method5 (); // method5 console. readkey ();}}

  

Static Method:

Using system; Class myclass {/* Static Method */public static string fun1 () {return "fun1";} internal static string fun2 () {return "fun2 ";} /* General Method */Public String fun3 () {return "fun3";} internal string fun4 () {return "fun4" ;}} class program {static void main () {/* static method called by Class Name: */console. writeline (myclass. fun1 (); // fun1 console. writeline (myclass. fun2 (); // fun2/* non-static method called through object: */myclass OBJ = new myclass (); console. writeline (obj. fun3 (); // fun3 console. writeline (obj. fun4 (); // fun4 console. readkey ();}}

  

Field:

Using system; Class myclass {public int F1;/* fields also have the following differences: public, internal, protected, and private. The default value is private */public static int F2;/* static fields, read/write through the class name */Public const int F3 = 2008;/* constant field, read through the class name, read-only */Public readonly int F4;/* read-only field, read through the object. Only values */Public myclass () {F4 = 2010;} class program {static void main () can be assigned to the object during declaration and in the constructor () {/* You can access F2 and F3 using the class name, But F3 is read-only */console. writeline (myclass. f2); // 0 console. writeline (myclass. f3); // 2008 myclass. f2 = 2009; console. writeline (myclass. f2); // 2009/* You can access F1 and F4 through an object. However, F4 is read-only */myclass OBJ = new myclass (); console. writeline (obj. f1); // 0 console. writeline (obj. f4); // 2010 obj. f1 = 2009; console. writeline (obj. f1); // 2009 console. readkey ();}}

  

Attribute:

Using system; Class myclass {private int Myint; Public int myproperty {get {return Myint;} set {If (value> = 0) Myint = value; else Myint = 100 ;}} class program {static void main () {myclass OBJ = new myclass (); obj. myproperty = 2009; console. writeline (obj. myproperty); // 2009 obj. myproperty =-1; console. writeline (obj. myproperty); // 100 console. readkey ();}}

  

Read-Only attributes:

 
Using system; Class myclass {private int Myint = 100; Public int myproperty {get {return Myint ;}} class program {static void main () {myclass OBJ = new myclass (); console. writeline (obj. myproperty); // 100 console. readkey ();}}

  

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.