Java programming reuse code details, java programming reuse details

Source: Internet
Author: User
Tags export class

Java programming reuse code details, java programming reuse details

This article focuses on the reusable classes in Java programming. The following describes what reuse classes are and how to use them.

After reading an interview with Luo shengyang, I couldn't help but admire it. I was very young. I thought that I was at a certain age with Luo Yonghao, and I was not one of the first High School programmers, after the interview, he found that Luo is also one step at a time. Don't say what is hard to do. If you can't do it, you simply don't have to try and stick to it.

If you can't fly then run, if you can't run then walk, if you can't walk then crawl,
Whatever you do, you have to keep moving forward -- Martin Luther King.

The title of the reuse class is hard to understand at the beginning. Later I went to the original English version of the book. In fact, the title is reusing classes. reusing classes actually means "using ready-made things, in fact, the two methods are often heard in java-combination and inheritance.

(1) Combination

The role of has-.

public class TV {   Show show;   public String toString(){    return "showgirl";   } }  class Show{ } 

When you need the toString method and you are an object, the compiler will call the toString method of the object.

TV has Show. The current show is not initialized and is null. You cannot call the show method.

The combination is powerful. For object-oriented purposes, if you are creating a Car class, you can easily combine Glass, Light, Engine, and other Car components.

(2) Inheritance

Is-

package com.myown.iaiti;public class Father {public int i;void get(){System.out.println("father");}}package son;import com.myown.iaiti.*;public 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 permission problem here. If public is not added, access is performed by members in the package by default. Different packages are accessed, that is, the Father member in Son cannot access the get method. Public is visible, so I can access it.

The private part cannot be inherited and belongs to the parent class. The public part inherits and can be rewritten as the method to be modified. You can add attributes separately.

And the inherited method, if the original father public method is not added to the public after being rewritten, there will be Cannot reduce the visibility of the inherited method from Father, that is, the visibility of inherited methods in the parent class cannot be reduced. Super refers to the parent class, that is, Father.

Another point is that all classes in java inherit the Object class implicitly. Object is the parent class, and other classes are subclasses.
Foreigners like to talk about the basics. A subclass is also called an export class or a derived class.

(3) proxy

The design pattern contains a relatively difficult-proxy pattern, which is interesting for the author. Proxy is the moderate way of combination and inheritance.

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 using Father as a member directly, the father method is exposed to this class. We can use a proxy class like FatherProxy. I have defined how to get the method, I know that I call the father get method, but I don't know which proxy I use. I only tell him that you can use the get method of the proxy. Encapsulation is embodied. The above is just a simple example.

(4) rewrite and reload

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 // an error is prompted because the parent class does not have this method, instead of overwriting 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 );}}

Override is a method that re-overrides the parent class. If it is not overwritten or overloaded, when the subclass calls a method that does not exist in a subclass, it actually calls the parent class.

The reload is the same method name, but the parameter name is different. To prevent incorrect reload, you can add the @ Override tag, which will prompt that you have not rewritten the method.

(5) protected

Detailed description of Java programming access control code

I wrote the previous article in advance because I didn't talk about inheritance before.

You can simply regard protected as the inheritance inherited by the parent class to the son. Other non-inheritance classes cannot be accessed.

(6) final keywords

The basic type of the final keyword indicates that the variable will not change after initialization. Similar to the define of c, you want a variable in this program to be the value that does not need to be changed. You can use final.

Public class Son {int age = 2; public static void main (String [] args) {final int I = 1; // I = 2; the final Son son = new Son (); // The final local variable son cannot be assigned. // It must be blank and not using a compound assignment // The son local variable modified by final cannot be assigned. It must be empty or do not allocate son again. age = 4; // although the reference remains unchanged, the object itself can be changed. } Void change (final int c) {// c = this. age; the new value cannot be assigned because the value is determined only by passing parameters in the method. The object reference is similar to this. // age ++; cannot be changed }}

Static is originally static initialization, and is used together with final to occupy a storage space that cannot be changed.

Static final is a constant in the compilation period. The constant names are traditionally named according to the constant name in c. They are all capital letters and separated by underscores.

static final VALUE_ONE = 1; 

Final modification method

Public class Print {final void cannotprint () {System. out. println (1) ;}} public class PrintSon extends Print {// void cannotprint () {}// cannot be rewritten because the public static void main (String [] args) is modified by final) {PrintSon ps = new PrintSon (); ps. cannotprint ();}}

It can be seen that the parent class requires that the subclass must inherit the property that cannot be modified (ancestral pass ). Private is implicitly specified as final, because private does not inherit from you. This is more private than inheriting from you but cannot be modified.

Clear the permissions by the way.

  • Public, public property, not only subclass, but other classes can also be used.
  • Final, the ancestral treasure, is left to the subclass, but cannot be modified.
  • Private: private Property of the parent class, which is not inherited by the Child class.
  • Protected: The property of the parent class that is reserved for sub-classes. Others cannot use it.

When final modifies a class, it aims to prevent the class from being inherited.

(7) Inheritance and initialization

The order here is an interesting question. Let's look at the 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 printed result first? Wrong.

Although the main method is executed, we can see that son does not have the I that requires static initialization. Is the result s and first?

This also has the initialization problem. son inherits father, so the compiler will load father and initialize I. That father inherits grandfather, then the compiler will load grandfather, similar to recursion.

The last Initialization is grandfather I.

So the final result is: g, f, s, first.

Summary

The above is all the details about the reusable Java programming code in this article, and I hope to help you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

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.