usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceObject-oriented {//Create Person class Public classPerson {//Fields: Field Private string_name; Private int_age; Private Char_gender; //Properties: Attributes Public stringName {Get{return This. _name; }//when you get the property value, call the Get method Set{ This. _name = value; }//when assigning a value to a property, call the Set method } Public intAge {Get{return This. _age;} //qualifying the Set property Set { if(Value <0|| Value > -) {Value=0; } This. _age =value; } } Public CharGender {//to qualify a Get property Get { if(_gender! ='male'&& _gender! ='female') { return_gender ='male'; } Else { return_gender; } } Set{_gender =value;} } //Methods: Method (Behavior) Public voidChlss () {Console.WriteLine ("My name is {0}, I am {1} years old this year, I am {2} born, I can eat and drink to sleep yo ~ ~", This. Name, This. Age, This. Gender); } }}
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceObject-oriented {classProgram {Static voidMain (string[] args) { //to create an object of the person classPerson Lucy =NewPerson (); Lucy.name="Lucy"; Lucy.age= - at; Lucy.gender='Spring'; LUCY.CHLSS (); Console.ReadLine (); } }}
Chapter 1. Object-Oriented (class, Object, field, method, property, constructor)