The previous simple description of the concept of object-oriented programming, today we put these concepts through the Java syntax to implement, and then look at the effect.
Consider the first case: define the Goddess class, then create three goddess objects from the Goddess class, and use the properties and methods of the Goddess object .
First step: Define the Goddess Class (because Java itself does not have this type, so we create this type ourselves)
/** * Goddess Class */public class Goddess {public String name;//name public int age;//age public double stature;//height/** * Self Introduction method */publi c void Show () {System.out.println ("I Call" +name+ ", this year" +age+ "years old, height is" +stature ")
Step Two: Create the test class that contains the main method (the main method is equivalent to the starting point of the program's Run)
public class Test {public static void main (string[] args) {}}
Step Three: Create three objects of the Goddess class in the Main method (the most basic syntax is used because of the construction method, etc.)
public class Test {public static void main (string[] args) {//Create an object of three goddess types Goddess Lin = new goddess (); Goddess Chang = new Goddess (); Goddess Jia = new Goddess ();//Use the object's properties Lin.name = "Lin Chi ling"; lin.age = 43;lin.stature = 1.73;chang.name = "Cang jing empty"; chang.age = 34;ch Ang.stature = 1.55;jia.name = "Jialing"; jia.age = 35;jia.stature = 1.66;//method to invoke the object Lin.show (); Chang.show (); Jia.show ();}}
Operation Result:
650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M00/A4/98/wKioL1muSWSSrOpxAAAlkaUpMFQ112.png "title=" 11. PNG "alt=" Wkiol1muswssropxaaalkaupmfq112.png "/>
Look at the grammar is not very simple!!! If the object-oriented approach is not used, then we name to define three variables, age or define three variables, stature also to three variables, a total of nine variables, and now only requires three goddess types of variables to solve the problem, and the properties are the same name, but also easy to remember and use.
Now there are new needs to appear!!!
All the former are goddesses, now need to be subdivided into be and moe sister! ( statement: The author does not know what these mean, are through the network query to know, the author is simple!!! )
Then and goddess of the lateral and female men, female men are divided into genius and cock silk!
The Goddess and the female man are all women!!!
650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M00/A4/99/wKioL1muTwewewmDAAIJpA8VcBA875.png "title=" 11. PNG "alt=" Wkiol1mutwewewmdaaijpa8vcba875.png "/>
Finally, finally, according to the changing needs of the present, we have to consider whether there will be more kinds of goddesses in the future? such as the Queen, and will not be the goddess of the level of "dinosaur" appear?
This is possible, so let's consider modifying the code as follows ( with inheritance ):
650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M00/05/E8/wKiom1muUV7B6j_CAAA2Mh3Ipms158.png "title=" 12. PNG "alt=" Wkiom1muuv7b6j_caaa2mh3ipms158.png "/>
female :
Properties: Name, age, height
Method: Self Introduction ()
Goddess : All Behaviors (methods) and characteristics (attributes) with females
Unique Properties: Yan value
female man: All Behaviors (methods) and characteristics (attributes) with women
Unique properties: Domineering value
Unique Method: Madness ()
be : All behaviors (methods) and characteristics (attributes) that have a goddess
Unique Properties: Sexy values
Moe : All Behaviors (methods) and characteristics (attributes) that have a goddess
Unique method: Sell Moe ()
Genius : All Behaviors (methods) and characteristics (attributes) with female men
Unique attributes: Perception
Unique method: Anti-learning slag ()
Dick Silk : All Behaviors (methods) and characteristics (attributes) with female men
Unique method: Pin ()
I write the code of the female, goddess and female man, and the rest of the code believes that you crossing can achieve it by yourself.
/** * female class * */public class woman { public string name;//name public int age;//Age public double stature;//Height/** * Self Introduction Method */public void show () {System.out.println ("I Call" +name+ ", this year" +age+ "years old, height for" +stature+ "meters.");} ** * Goddess * Inherited the Woman class */public class goddess extends woman {public int facevalue;/ /Yan Value}//////////////////////////////////////////////////////////////////////////////** * female man class * inherited the female category * */public class GirlMan extends Woman {public int domineeringvalue;//domineering Value Public void berserker () {System.out.println ("domineering value is full, start to Madness");}}
Three classes are defined, in which the goddess and female man classes use the extends keyword to inherit the female class. after inheritance, goddess and female men can use the attributes and methods defined in the female class .
Write the test class code:
public class Test {public static void main (string[] args) {//Create an object of three goddess types Goddess Lin = new goddess ();//Goddess object Girlman Jia = NE W Girlman ();//Female Man object//Use the object's properties Lin.name = "Lin Chi ling"; lin.age = 43;lin.stature = 1.73;lin.facevalue = 99;//Yan Jia.name = "jialing"; jia.a GE = 35;jia.stature = 1.66;jia.domineeringvalue = 95;//domineering value//method of calling Object Lin.show ();//Call the parent class (female Class) method Jia.show (); jia.berserker ();//Call self-specific method}}
Operation Result:
650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M02/05/EB/wKiom1muaJLyWQa4AAAmw2irQC8560.png "title=" 13. PNG "alt=" Wkiom1muajlywqa4aaamw2irqc8560.png "/>
As for encapsulation and polymorphism, let's leave it to the next article. 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0028.gif "alt=" J_0028.gif "/>
"Software Thinking" blog address: 51CTO , Blog Park , interested small partners can go to see the relevant other blog posts.
This article is from the "Software Thinking" blog, please be sure to keep this source http://softi.blog.51cto.com/13093971/1962881
"Java from getting started to giving up" javase: Object-oriented Syntax one (introductory version)