Object-oriented core concepts
(1) Package
Encapsulated class = data + operations on this data (that is, property + methods)
In layman's words, encapsulation is: wrap what the outside world does not need to know, only to reveal what is available to the outside world.
(2) Abstract
When designing a software system using an object-oriented approach, the first thing to do is to distinguish between the types of things the real world belongs to.
The idea of analyzing the problem:
First of all, the general character of the real grasp out. (That is, to abstract it)
Then the opposite sex is analyzed individually. (i.e. specific)
(3) Inheritance
It is not the inheritance of heredity that we believe, but the meaning of inclusion.
(4) polymorphic
The properties that a base class has, subclasses must have,
Programming with an abstract class.
2 Classes and objects
Class is the basic unit of object-oriented programming. (Everything is an object)
Only variables and functions declared as public can be called by the outside world, and the rest of the variables and functions declared private are proprietary and can only be used by their own classes.
Case 1,
Size of the swimming pool:
classYongchi { Public intR; Public DoubleZhouchang () {Doublec =2*3.14*R; returnC; } Public DoubleMianji () {Doubles =3.14* R *R; returns; } } classProgram {Static voidMain (string[] args) {Yongchi Small=NewYongchi (); Yongchi Big=NewYongchi (); Console.Write ("half-warp of the swimming pool"); SMALL.R=Convert.ToInt32 (Console.ReadLine ()); Console.Write ("the half-warp of the square"); BIG.R=Convert.ToInt32 (Console.ReadLine ()); Console.WriteLine ("the perimeter of the swimming pool is"+Small.zhouchang ()); Console.WriteLine ("the area of the swimming pool is"+Small.mianji ()); Console.WriteLine ("the perimeter of the square is"+Big.zhouchang ()); Console.WriteLine ("the area of the square is"+ (Big.mianji ()-Small.mianji ())); } }
:
Case 2,
Dog Eat Dog
classDog { Public stringName; Public inthp= -; Public voidBark (Dog op) {console.write (Name+"bit."+op. name+"Mouthful,"); Op. HP-=5; Console.Write (Op. Name+"The remaining blood volume is"+op. hp+"\ n"); } } classClass1 {Static voidMain (string[] args) {Dog D1=NewDog (); Dog D2=NewDog (); D1. Name="Wong Choy"; D2. Name="Rhubarb"; D1. Bark (D2); D2. Bark (D1); D1. Bark (D2); D2. Bark (D1); } }
:
Case 3,
classRen { Public stringName; Private intHeight; Private intWeight; Public voidH1 (inth) {if(H <=0|| H >= -) {Console.WriteLine ("you've entered a wrong height."); } Else{Height=h; } } Public voidW1 (intW) {if(W <=0|| W >= +) {Console.WriteLine ("you've entered a wrong weight."); } Else{Weight=W; } } Public voidSpeak () {Console.WriteLine ("My name"+ Name +", Weight"+ Weight +"kg, height"+ Height +"centimeters"); } Public voidYundong () {}}classClass2 {Static voidMain (string[] args) {Ren R1=NewRen (); R1. Name="Xiao Ming"; Console.Write ("Please enter your height:"); intH1 =Convert.ToInt32 (Console.ReadLine ()); Console.Write ("Please enter your weight:"); intW1 =Convert.ToInt32 (Console.ReadLine ()); R1. H1 (H1); R1. W1 (W1); R1. Speak (); } }
:
February 1 C # Object oriented