Static voidMain (string[] args) { #regionDefine parent class father (surname LastName, property, Blood type Bloodtype)//sons son class (play game PlayGame method), daughter daughter Class (Dance Dance method),//Call the Parent class constructor (: Base ()) to assign a value to the subclass field//son son = new son ("Zhang San", 10m, "AB", ""); //son. SayHello (); //son. PlayGame (); //daughter dh = new daughter ("Zhang Meimei", 100m, "O"); //DH. SayHello (); //DH. Dance (); #endregion #regionDefine Car Class vehicle properties (brand, color), method run//sub-class truck (Truck) Properties weight load, method pull, Sedan (car) Properties passenger passenger quantity, method passengerTruck T=NewTruck ("Dongfeng","Green",Ten); //T.brand = "Dongfeng"; //T.color = "green"; //t.weight = ten;T.sayhello (); T.lahuo (); T.run (); Car C=NewCar ("Mercedes","Black",5); //C.brand = "Mercedes-Benz"; //C.color = "BLACK"; //C.passenger = 5;C.sayhello (); C.zaike (); C.run (); #endregionConsole.readkey (); }
First Exercise:
classFather { Public stringLastName {Get;Set; } Public decimalProperty {Get;Set; } Public stringBloodtype {Get;Set; } PublicFather (stringLastName,decimalPropertystringbloodtype) { This. LastName =LastName; This. property =Property ; This. Bloodtype =Bloodtype; } Public voidSayHello () {Console.WriteLine ("My name is {0}, I have {1} yuan, I am {2} type of blood", This. LastName, This. property, This. Bloodtype); } }
classSon:father { Public stringName {Get;Set; } PublicSon (stringLastName,decimalPropertystringBloodtype,stringname):Base(LastName, property, Bloodtype) { This. Name =name; } Public voidPlayGame () {Console.WriteLine ("son loves to play games"); } }
class Daughter:father { public daughter (stringdecimalstring bloodtype) Base (LastName, property, Bloodtype) { } publicvoid Dance () { Console.WriteLine (" daughter loves to dance ");} }
A second exercise:
/// <summary> ///car's parent class/// </summary> classVehicle {Private string_brand; Public stringBrand {Get{return_brand;} Set{_brand =value;} } Private string_color; Public stringColor {Get{return_color;} Set{_color =value;} } PublicVehicle (stringBrandstringcolor) { This. Brand =brand; This. Color =color; } Public voidRun () {Console.WriteLine ("cars will run ."); } //Public void SayHello ()//{ // //Console.WriteLine ("I am {0} car, I am {1} color", this.) Brand, this. Color); //} Public Virtual voidSayHello () {}}
classtruck:vehicle {Private int_weight; Public intWeight {Get{return_weight;} Set{_weight =value;} } PublicTruck (stringBrandstringColorintweight):Base(brand, color) { This. Weight =weight; } Public Override voidSayHello () {Console.WriteLine ("I am the {0} card {1} color truck, payload {2} ton", This. Brand, This. Color, This. Weight); } Public voidLahuo () {Console.WriteLine ("{0} Big truck can pull a lot of goods oh ~", This. Brand); } }
classcar:vehicle {Private int_passenger; Public intPassenger {Get{return_passenger;} Set{_passenger =value;} } PublicCar (stringBrandstringColorintpassenger):Base(brand, color) { This. Passenger =passenger; } Public Override voidSayHello () {Console.WriteLine ("I am the { 0} card {1} color sedan, the number of passengers is {2} people", This. Brand, This. Color, This. Passenger); } Public voidZaike () {Console.WriteLine ("{0} The car sits comfortably and the guests love to sit", This. Brand); } }
. NET Learning notes----2015-07-22 (C # Basics Review 10, object-oriented 2 small exercises)