"Java from getting started to giving up" javase: Object-oriented Syntax one (introductory version)

Source: Internet
Author: User

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)

123456789101112131415 /** * 女神类 */publicclassGoddess {    publicString name;   //姓名    publicintage;       //年龄    publicdoublestature;    //身高        /**     * 自我介绍方法     */    publicvoidshow(){        System.out.println("我叫"+name+",今年"+age+"岁,身高为"+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)

12345 publicclass 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.)

12345678910111213141516171819202122232425 publicclass Test {    publicstaticvoidmain(String[] args) {        //创建三个女神类型的对象        Goddess lin = newGoddess();        Goddess chang = newGoddess();        Goddess jia = newGoddess();                //使用对象的属性        lin.name = "林志玲";        lin.age = 43;        lin.stature = 1.73;        chang.name = "苍井空";        chang.age = 34;        chang.stature = 1.55;        jia.name = "贾玲";        jia.age = 35;        jia.stature = 1.66;                //调用对象的方法        lin.show();        chang.show();        jia.show();             }}

Operation Result:

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!!!

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):

Women:

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.

12345678910111213141516171819202122232425262728293031323334353637 /** * 女性类 * */publicclassWoman {    publicString name;       //姓名    publicint age;           //年龄    publicdoublestature;    //身高        /**     * 自我介绍方法     */    publicvoidshow(){        System.out.println("我叫"+name+",今年"+age+"岁,身高为"+stature+"米.");    }}//////////////////////////////////////////////////////////////////////////////** * 女神类 * 继承了Woman类 */publicclassGoddess extendsWoman {    public intfaceValue;     //颜值}//////////////////////////////////////////////////////////////////////////////** * 女汉子类 * 继承了女性类 * */public classGirlMan extendsWoman {    publicintdomineeringValue;  //霸气值        publicvoidberserker(){        System.out.println("霸气值已充满,开始狂化中···");    }}

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:

1234567891011121314151617181920212223 publicclassTest {    public staticvoidmain(String[] args) {        //创建三个女神类型的对象        Goddess lin = newGoddess();    //女神对象        GirlMan jia = new GirlMan();    //女汉子对象                //使用对象的属性        lin.name = "林志玲";        lin.age = 43;        lin.stature = 1.73;        lin.faceValue = 99;       //颜值         jia.name = "贾玲";        jia.age = 35;        jia.stature = 1.66;        jia.domineeringValue = 95;    //霸气值                //调用对象的方法        lin.show(); //调用父类(女性类)的方法        jia.show();            jia.berserker();//调用自已特有的方法            }}

Operation Result:

As for encapsulation and polymorphism, let's leave it to the next article.

"Software Thinking" blog address:51CTO, Blog Park , interested in small partners can go to see the relevant other blog posts.

"Java from getting started to giving up" javase: Object-oriented Syntax one (introductory version)

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.