See the old Luo Luo Shenyang's interview, can't help but admire, very young, I thought and yonghao a level of age, is also seen is not the beginning of high school programming one of the Daniel, after the interview, found that Lao Luo is also step by step people. Don't say what is difficult to do, do not, you have not to try, nor to insist.
If you can ' t fly so run,if you can ' t run then walk, if you can ' t walk then crawl,but
Whatever you do,you has to keep moving forward--martin Luther King.
Reuse class The title is very difficult to understand, in fact, reuse is the "use of ready-made things" means, in fact, the two methods of implementation is often heard in Java--combination and inheritance.
(1) Combination
The role of Has-a.
public class TV { show show; Public String toString () { return ' showgirl '; }} Class show{}
To mention the ToString method, when you need a string and you are an object, the compiler invokes the object's ToString method.
TV has show, now show is not initialized, is NULL, cannot call show method.
A combination of powerful, object-oriented look, if you are building a car class, then you can use the combination of glass,light,engine and so on the car these parts together.
(2) Inheritance
Is-a
Package Com.myown.iaiti;public class Father {public int i; void Get () {System.out.println ("father"); }}package son;import com.myown.iaiti.*;p ublic class Son extends father{Father f = new Father (); int j = F.I; Son son = new son (); Son.get (); } public void Get () {super.get (); System.out.println ("Son");} }
There is a packet access problem, if there is no public, the default is within the package member access, different package access, that is, son of the father member access to the Get method is not visible. And the public words are visible, so I access to get.
The private part is not inherited, belongs to the parent class private, and the public part, will inherit, need to modify the method, can be rewritten. The attributes to be added can be added separately.
and inherited methods, if the original Father public method is not added after the public, there will be cannot reduce the visibility of the inherited method from Father, That is, you cannot reduce the visibility of inherited methods in the parent class. Super refers to the parent class, which is father.
Also, in fact, all classes in Java implicitly inherit the object class. Object is a parent class, other classes are subclasses
Foreigners like to speak as a base class. Subclasses are also called export classes or derived classes.
(3) Agent
There is a more difficult to understand in the design mode-proxy mode, the author is very interesting, the agent is the combination and inheritance of the mean.
Package Son;class father{public Void Get () { System.out.println ("Father");} } public class Son extends father{public static void Main (string[] args) { Father f = new Father (); F.get (); }} Class fatherproxy{ private Father f = new Father (); public void Get () { f.get (); }}
like direct father as a member, then the Father method is exposed to this class, then we can use fatherproxy such proxy class, I myself define how get method is to take, I know is to call Father Get method, But the person who uses my agent does not know, I only tell him that you need to use the proxy get method on it. Encapsulation is the embodiment of the. The above is just a simple example of knocking at random.
(4) overriding and overloading
Class father{public void get (String s) { System.out.println ("Father"); } public void Get (Boolean b) { System.out.println ("Boolean");} } public class Son extends father{ @Override public void get (String s) { System.out.println ("Father"); } //@Override//There will be an error message because the parent class does not have the method, not the override public void get (int i) { System.out.println ("Sonint"); } public static void Main (string[] args) { son s = new Son (); S.get ("D"); S.get (false); S.get (1);} }
overrides are methods that re-overwrite the parent class, and if there are no overrides or overloads, then the child class invokes the parent class when it calls a method that does not have a subclass.
Overloading is the same method name, but the parameter name is different, in order to prevent you from overloading the error can be added @override tag, you will be prompted not to override the method.
(5) Protected
Java Programming Idea (iii)--control of access rights
In the previous article written in advance, because did not speak of inherited things.
Protected can be simply regarded as the inheritance of the parent class to the son, and other non-inheriting classes cannot be accessed.
(6) Final keyword
Plus the basic type of the final keyword, which means that the variable will not change after initialization. Like the C-Define, you want a variable in this program where this value does not need to be changed. You can use final.
public class son{ int = 2; public static void Main (string[] args) { final int i = 1; i = 2; The value can no longer be changed to final son son = new son (); son = new son (); The final local variable son cannot be assigned. It must is blank and not using a compound assignment //final decorated local variable son cannot be assigned, must be empty or not reassigned son.age = 4; Although the reference is constant, the object itself can be changed. } void Change (final int c) { ////c= this.age; cannot be given a new value because the value is determined only by the method parameter, the object reference and this similar //age + +; cannot be changed }}
Static initialization, which is used in conjunction with final, occupies a storage space that cannot be changed.
Static final is the compile-time constant, the constant name according to the C constant name tradition, all with uppercase letters, the words are separated by underscores.
Static final value_one = 1;
When the final decoration method
public class Print { final void Cannotprint () { System.out.println (1);} } public class Printson extends print{ //void cannotprint () {} //cannot be overridden because the public static void main is final decorated ( String[] args) { Printson PS = new Printson (); Ps.cannotprint (); }}
can be seen as a parent class that requires subclasses to inherit the non-modifiable property (ancestral). Private is implicitly specified as final, because private does not inherit from you at all. This is more private than it is for you to inherit but not modify.
By the way, clarify the permissions.
Public property, not just subclasses, other classes can also be used.
Final, ancestral treasures, reserved for subclasses, but not allowed to modify.
Private property, parent class, does not inherit from the subclass.
Protected, the parent class is reserved for the property of the subclass, and the other person cannot use it.
When the final modifier is a class, it is to make the class not inherit.
(7) Inheritance and initialization
The order problem here is a very interesting question. See example.
Class grandfather{ private static int i = print (); private static int print () { System.out.println ("G"); return 1;} } Class Father extends grandfather{ private static int i = print (); private static int print () { System.out.println ("F"); return 1;} } public class Son extends father{ private static int i = print (); private static int print () { System.out.println ("s"); return 1; } public static void Main (string[] args) { System.out.println ("First");} }
Is the result of printing first? Wrong.
Although the main method is executed, but see son this need static initialization of I did not, the result is S,first?
This also has the initialization problem, son is inherit father, then the compiler will load father, and initialize I, that father inherit grandfather, then the compiler will go to load grandfather, similar to recursion.
The last one that was initialized first was the grandfather I.
So the final result is: G,f,s,first.
As for the upward transformation mentioned in this chapter, it is related to polymorphism, so put it in the next article.
One of the three basic characteristics of object-oriented--java programming thought (v)-polymorphism.
Java Programming Idea (iv)--reuse class