PM Code Android "Martial Arts articles: encapsulation, inheritance, polymorphism"

Source: Internet
Author: User

The Android "martial arts article: encapsulation, inheritance, polymorphism"

This is the sixth article in the Android series, in a previous article, has already understood the object-oriented basic concept, this article will pass the martial arts and lakes analogy, explains the object-oriented more content, thanks to the small partners have been supporting.

Three characteristics of Wulin Gate faction

1 Unique cheats (package)

2 Inheritance of the Gate faction (succession)

3 See the situation make Kung Fu and change posture to explain the problem (polymorphic)

"Package" 1 Portal unique cheats (package)

The previous article has said that, when the Dharma on the Songshan, the lake will formally enter the era of the door. Each gate is different from the other, it must be because this door faction has the unique skill cheats. Give me a chestnut and you'll understand:

  • Shaolin Faction unique cheats: "Easy rib Sutra", "Nine Yang Martial", "Shaolin 72 stunt"

  • Confraternity unique cheats: "Dragon 18 Palm", "hit the Dog Stick Law"

  • Carefree faction Unique cheats: "North of the Underworld", "Eight Liuhe", "the Tianshan fold plum Hand"

The corresponding object-oriented program, a class (pay attention to the pause ...) The important reason for distinguishing from another class is that the class has its own encapsulation properties. The role of encapsulation, is to protect certain properties and methods are not seen outside, like martial arts, each door faction's unique cheats and kungfu routines, basically is not transmitted inside the outside.

In other words, it is the unique secret of your portal (private property), others can not access, only through a certain way to access, so that the things that belong to you are more secure.

2 How to Package
武侠版帮派 丐帮{    独门秘籍 降龙十八掌;     独门秘籍 打狗棒法;}----------------------------------程序版class GaiBang{    private String Xianglong;    private String Dagou;}(程序封装是通过关键字private(加粗)哦)
3 Why do you need cheats?

(1) The creation of the portal needs cheats

If you are carefree old ancestor, it is no cliff son, Tianshan Dong, Li Qiushui master, virtual bamboo ancestor. You exhausted the life of the creation of martial arts, always need a secret place to store it, or casually be someone else to learn the past, you also how to be a top master AH? (I think the carefree Lao zu is the Jin Yong series of Kung Fu Top 5 of the ruthless role ...) )

门派 逍遥派{    独门秘籍 神秘的秘籍书;    穷尽心血写就秘籍(书){        神秘的秘籍书=书    }    获得之前写的秘籍(){        时空闪回,让你回忆起自己之前写的秘籍;    }    对外吹牛    {        我们逍遥派的绝顶神功是神秘的秘籍书(加粗),像你这样的,我们一个打你们八个,怎么样,加入我们吧,目前开班价9.9折,一共998,你就能把北冥神功带回家。    }}真实发生的故事(){    逍遥派 逍遥老祖;    逍遥老祖是逍遥派出身的;    逍遥老祖穷尽毕生心血写就“北冥神功”,并把它存入门派;    逍遥老祖在某次高峰论坛使用了技能“对外吹牛”;}

(2) in the program world, you need private properties (plus private), they are not easily accessible, only a certain way to access (through the set and get methods to complete the encapsulation properties of access and settings).

class XiaoYao{    private String book;//声明私有属性(秘籍)    //声明吹牛方法(加粗),用户向小白宣传你的门派    void talk(){        System.out.println(“我们逍遥派的绝顶神功是”+getBook()+“,像你这样的,我们一个打你们八个,怎么样,加入我们吧,目前开班价9.9折,一共998,你就能把北冥神功带回家。”)    }    //setBook方法用于将你毕生心血写就的秘籍存入私有属性book中,你终于可以安心的翻牌睡觉了。。。    public void setBook(String str){        book=str;    }    //getBook方法用于在你的门派内部将秘籍取出,记得洗干净手哈。。。    public void getBook(){        return book;    }}public class Test{    public static void main(String[] args){        XiaoYao XiaoYaoLaoZu=new XiaoYao;        XiaoYaoLaoZu.setBook(“北冥神功”);        XiaoYaoLaoZu.talk();}setBook把“北冥神功”传入了你的门派,然后你通过“吹牛方法”调用了getBook,将你刚才传入的秘籍说给大家听。
Inheritance

One of the most puzzling philosophers of all history: Is there an egg in the world or a chicken first? We do not discuss the ultimate question of philosophy here, what is the ultimate problem of philosophy? I'm kinda interested, have time to learn about ^_^.

Far away, the world of martial arts and the world of Java is really highly similar, even the succession is very consistent.

In the martial arts world, the gate faction also has the inheritance relations, carries a chestnut:

(1) The Shaolin faction is the ancestor of all the other portal factions.

(2) The day Eagle teaches from Ming Party

(3) The stars sent from the carefree school

Whatever the case, your door faction can only inherit another gate, not the same two-door faction.

(1) You create a door faction want to inherit the Wudang and confraternity at the same time, you think very beautiful, want to learn taijiquan at the same time and the Dragon 18 Palm, but you are willing to, others do not want, useless ...

(2) But such succession is permissible: the stars sent from the carefree faction, carefree sent from Shaolin Dharma

门派 少林{    武功 基础拳脚功夫;}门派 逍遥派 继承 少林派{    武功 八荒六合唯我独尊功;}门派 星宿派 继承 逍遥派{    武功 化功大法;}(因为是继承关系,逍遥派和星宿派都会少林派的基础拳脚功夫)

In the Java world, a class can inherit only another class, not multiple classes at the same time. For object-oriented programs, the essence of this is that the inheritance of classes can be based on existing classes and thus derive new classes.

class ShaoLin{    String BasicKongfu;}class XiaoYao extends ShaoLin{    String bahuangliuheweiwoduzungong;}class XingXiu extends XiaoYao{    String huagongdafa;}(Java中类的继承要用到关键字extends哦,不要忘记了)
"Polymorphic"

Polymorphism in a program is the overloading and rewriting of methods. What is overloading and rewriting?

(1) Overloading: Method name is the same, parameter type and number are different
Heavy-duty in the martial arts world, just like the situation to make Kung fu, according to your different routines, and choose different countermeasures. Shaolin Temple's Very famous "Shaolin 72 stunt", according to the different routines you use, the Dharma Institute of the palm of the monk can identify you are the use of the kung fu:

功夫 少林72绝技{    你耍了一套功夫(拳法){        你使用了少林罗汉拳;    }    你耍了一套功夫(拳法,指法){        你使用了少林罗汉拳和无相劫指;    }    你耍了一套功夫(拳法,脚法,指法){        你使用了你使用了少林罗汉拳和无相劫指,还有如影随形腿;    }}

In programming languages like the following, overloading is the choice of calling different construction methods based on the number of arguments you pass in.

class ShaoLin72JueJi{    public void ShaoLin72JueJi(String quan){            System.out.println("你使用了少林罗汉拳");    }    public void ShaoLin72JueJi(String quan,String zhi){        System.out.println("你使用了少林罗汉拳和无相劫指");    }    public void ShaoLin72JueJi(String quan,String jiao,String zhi){        System.out.println("你使用了少林罗汉拳和无相劫指,还有如影随形腿");    }}

(2) Override: Method name, Parameter type, return value type all the same, but complete function is different

Overrides often appear in the subclass inherits the parent class, the martial arts, the constellation sends out from the carefree faction, their exclusive internal strength is the same, the carefree faction declares their unique internal strength is "the North the Destiny", but the constellation sends the outward claim their special strength is "the Power Dafa". In fact, " a change of posture to explain the problem ."

门派 逍遥派{    对外吹牛(){        逍遥派独门内功——北冥神功;    }}门派 星宿派 继承 逍遥派{    对外吹牛(){        星宿派独门内功——化功大法;    }}

The program language is like this ...

class XiaoYao{    public void DuJiaNeiGong(){        System.out.println("逍遥派独门内功——北冥神功");    }}class XingXiu extends XiaoYao{    public void DuJiaNeiGong(){        System.out.println("星宿派独门内功——化功大法");    }}

PM Code Android "Martial Arts articles: encapsulation, inheritance, polymorphism"

Related Article

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.