What is behavior? The behavior specifies the request you can send to an object. Your class, that is, MM, you design it, and you are very bt, only two behaviors: loveme () and makelovewithme (). Then she can't accept other client classes, if in a certain class, you are written into mm.lovef4 (), then the compiler will go wrong. You will find the property of MM's properties into beauty, you don't want others to change this fact, then you have to define this property as Private, so that MM will not wake up on the next day to become the legendary KL. This is heading in the first chapter: hidden implementation details. A property, four modes, public, private, protected, empty (default, friendly). The representative it means that any object can be accessed. Internal objects can be accessed, the successor of this object (Class) can access, other objects of the same package (package) can be accessed. We always want to make your code as concise as much as possible, which requires repeated use of implementation. Java provides this way, there are two ways: combination and inheritance, suppose there is a class as a, A has a behavior dosomething (), there is a class B, you also want to do Something, then you can generate in B A method (i.e. function) DOSMETHINGTOO (A.DOSMETHING ();). This is called a combination. Inheritance is another way, you can use keywords Extends, let B inherit from A, then you don't have to specifically expose, B is also the ability of DOSMETHING in the outside world. Then we have to ask, when will we combine, when is it inherited? Bruce, when B is A A, we use inheritance. This is not very good, we will give an example of MM again. MM has the behavior of eating (ETYOU (eating; digestion)), MM is derived from a good color MM, love fart MM, but they are MM, which is the relationship of IS A, this time you want to design a color When MM is inherited, use Extends. The GG is not MM, but GG is Like A mm, because GG can eat. At this time, we don't have to write Eatyou () this method, write EATYOU (mm.eatyou (); then, GG also has a function of eating, digestion. In fact, we should often use the combination, in the program, the inheritance is relatively small. In inheritance, there are two special behaviors that we need to pay attention to overriding and overloading. Now you only need to remember: If the base class is the same, the number is the same, the number is overriding, the name is the same, and the number is overloading. "OVERLOADING. Object-oriented a significant advantage is multi-shaped (polymorphism).
I didn't want to write code in this first part, but it seems that this problem is not intuitive with language expression, so I wrote the simplest problem that can explain multi-faceted problems. See the code first. . . //Love.java class mm {public void titeegg () {system.out.println ("xxx");}}}} class haosemm extends mm {public void titeegg () {system.out.println ("temptation GG !!! ");}} Class Benfenmm EXTENDS MM {public void Toseegg () {system.out.println (" Merry ... ");}}}}}}}}} public class love {public static void lovegg (mm i) {i. Toseegg ();} public static void main (String [] args) {mm mm1 = new haosemm (); mm mm2 = new benfenmm (); lovegg (mm1); lovegg (mm2);}} The result is the result of the code. : Temptation GG !!! It's ashamed .... We see that there is a good color MM (Haosemm), this point MM (Benmm), mm may see handsome guy, but color mm and this points mm Look at the eyes of handsome guys is different, as defined in the program. In this class, we define a method LOVEGG, we deliver a parameter, the base class mm, then TOSEEGG (). Through the most beginning of this article, we know that the relationship between Haosemm and Benmm and MM is the relationship of Is A, so we don't have erroneous compilers when using LoveGG (MM1) and LoveGG (MM2). We see that the program automatically performs ToseeGGG () of Haosemm and Benfenmm, not printed "XXX". This is versatile. The reason why it is so magical is because the Java runtime environment provides dynamic binding technology. Dynamic binding will make you generated by your instructions in the Java runtime environment. OK, we don't need to know how dynamic bindings are doing, we just know what this is because we are still in the first chapter. Upcasting this concept is also raised here. In the method of LOVEGG (MM i), the method accepts MM, but LOVEGG also accepts Haosemm and Benfenmm, which is called traceable. We saw a code that did not use in the program, which is system.out.println ("xxx") in the mm class ;. No one cares about how to watch handsome guy, because it is just a template, so we just don't want this code, and we even {} don't want, directly rewrite this method for public abstract void titeegg (); then this Method is called an abstraction method (Abstract Method). The base class mm is not implemented for us, so we rewrite the class mm {...} as Abstract Class MM {...}, then this class is called an abstract class. We can't help but ask, then abstract classes can have non-abstract functions. Answer: Yes.