Dark Horse Programmer--java Basic Learning Note 8

Source: Internet
Author: User
Tags bit set



Dark Horse Programmer--java Basic Learning Note 8

I. Summary of the contents of the notes:

Object-oriented (array tool object creation-document annotations-Singleton Design patterns & memory plots), inheritance, single inheritance, and multiple inheritance.

Second, the introduction of common content:

1. Benefits of Inheritance: Improved reusability of code, relationship between classes and classes, development of the ultimate core content: constantly creating objects, using objects, and maintaining relationships between objects.

2. When do I use inheritance?

When there is a relationship between the class and the class, it defines the inheritance; Java does not directly support multiple inheritance, because if there are the same members in multiple parents, there will be the uncertainty of the call; in Java, implements is indirectly embodied by multiple implementations.

3. How to learn an inheritance system?

View the top-level classes in the system, understand the basic functions of the system, and create the most sub-class objects in the system to complete the use of the function.

4. Characteristics of member variables:

For the value of the variable, the subclass does not look for the parent class, if the local has a value, it will not find the value of the member variable;

Detail A: When the members and local members of this class have the same name, use this to differentiate between the parent class when the member variable has the same name, the super, and the detail B: the subclass cannot directly access the private content in the parent class, but can invoke the private content in the parent class from the public method that the parent class provides externally.

5. The difference between overloading and rewriting:

Overloads occur in the same class, where overrides occur in the child parent class, and when the subclass method overrides the parent class method, the subclass permission must be greater than or equal to the parent class's permission, and the static method can only overwrite the static method of the parent class or be overridden by a static method of its next subclass.

Iii. Classic examples and explanations:

1. Use the tool class to find the maximum value of the array.

Tool Class Tools.java

Package com.date8;/** * from a memory space saving angle * You can define a method in a tool class as static, so that it can be called directly from the class name, without the need to create an object. * * Create a tool class for manipulating arrays that contains common array operations such as: maximum, sort, etc. * @author Zengwei * @version 1.0.1 */public class Tools {/* Does not allow this class to create objects */private Tools () {}/** * Gets the maximum value of an integer array */public static int Getmax (int[] arr) {int index = 0;for (int i=1;i<arr.length;i++) {if (arr[i]& Gt;arr[index]) index = i;} return Arr[index]; }/** * Bubble Sort * @param arr */public static void Maopaosort (int[] arr) {for (int. i=arr.length-1;i>1;i--) {for (Int. j=1;j<i; J + +) {if (arr[j]>arr[j+1]) {int temp = Arr[j];arr[j] = arr[j+1];arr[j+1] = temp;}}} /** * Select sort */public static void Selectsort (int[] arr) {for (int. i=0;i<arr.length-1;i++) {for (int j=i+1;j<arr.length;) J + +) {if (arr[i]>arr[j]) {int temp = 0;temp = Arr[i];arr[i] = arr[j];arr[j] = temp;}}} /** * Insert Sort * @param arr */public static void insertion (int[] arr) {for (int i=1;i<arr.length;i++) {//insertindex unordered element to insert bit Set int Insertindex = I;int current = arr[i];for (int j=i-1;j>=0;j--) {if (Current<arr[j]) {//compare number of moves arr[j+1] = arr[j];//the insertion position before moving--insertindex;} Else{break;}} Insert data directly into arr[insertindex] = current;}} /** * Quick Find method */public static int getindex (int[] arr,int key) {for (int i=0;i<arr.length;i++) {if (arr[i]==key) return i;} return-1;}}

Test code

Package com.date8;//uses the tool class method to find the maximum value of the array (using object-oriented thinking) public class Test1 {public static void main (string[] args) {int[] arr = {1, 4, 7,2,5,8,3,6,9};int max = Tools.getmax (arr); Tools.insertion (arr); for (int i = 0; i < arr.length; i++) {System.out.print (arr[i]+ "");} System.out.println ("max=" +max); System.out.println (Tools.getindex (arr, 9));}}

2. Introduction to the single case design pattern

Package com.date8;/** * Singleton design mode  */class single{private static single s = new One ();//s belongs to reference member variable private privatization for controllable Priva Te single () {}//privatisation constructor public static single getinstance () {return s;}} public class Test2 {public static void main (string[] args) {//single S1 = single.getinstance ();//single s2 = Single.getinst Ance ();//system.out.println (S1==S2); Exam T1 = exam.getinstance (); Exam t2 = exam.getinstance (); t1.setnum; T2.setnum (20); System.out.println (T1.getnum ()); System.out.println (T2.getnum ());}} class Exam{private int num;private static Exam e = new Exam ();//Privatisation Object Private Exam () {}//privatization constructor public static Exam Getinsta NCE () {//Get instance object return e;} public void setnum (int num) {this.num = num;} public int Getnum () {return num;}}

3. Two cases of single-case design pattern (a hungry man and lazy)

Package Com.date8;public class Test3 {public static void main (string[] args) {}}/** * a hungry man type  */class easy{private static E Asy s = new Easy ();/** * Privatisation constructor to prevent other classes from creating this class object */private easy () {}/** * Get instance object */public static Easy getinstance () {return s;}} /** * Lazy (there is a thread-safety hazard) need to add the lock */class lazy{private static lazy L = null;private lazy () {}public static synchronized lazy Getins Tance () {if (l==null) {L = new lazy ();} return l;}}

4. Examples of inheritance (combined with the above theory)

Package com.date8;/** * Inheritance Benefits: */class person{//Parent class string Name;int age;} Class Student extends Person{void study () {System.out.println (name+ ... Student study ... "+age);}} Class Worker extends Person{void work () {System.out.println (name+ "... Worker work ... "+age);}} public class Test4 {public static void main (string[] args) {Student p = new Student ();p. Name = ' Zhang San ';p. Age = 18;p.study (); Orker w = new Worker (); w.name = "Harry"; w.age = 30;w.work ();}}

5. Characteristics of member variables in the Child parent class: (combined with the above theory)

Package com.date8;/*** * In child parent class, members are characterized by: */class fu{private int money = 100;//Subclass cannot directly access int num = 4;public int Getmoney () {RET Urn Money;}} Class Zi extends Fu{int num = 5;void Show () {System.out.println (num+ "..." +super.num+ "..." +super.getmoney ());}} public class Test5 {public static void main (string[] args) {Zi z = new Zi (); Z.show ();}}

6. Features of member functions in the child parent class:

Package com.date8;/** * Second, member function features in child parent class  * 1. When there is a member function with the same name in the child parent class, the subclass overrides the parent class * 2. The following Eat method cannot be overridden because the child class cannot directly access the private members and methods of the parent class, *    So there is no way to overwrite this saying * 3. When an extension of a subclass (software upgrade) is performed on a class, the subclass needs to preserve the functionality of the parent class *   but also define the unique content of the feature in the subclass, using the overwrite operation. *  */class father{public String s = "read book";p ublic Void Show () {System.out.println ("fu ..." +s);} void Eat () {System.out.println ("Fu ... Eat watermelon ");}} Class son extends father{public String s = "Play the game";p ublic Void Show () {//directly overrides the parent class's method System.out.println ("son ..." +s);} public void Eat () {System.out.println ("son ... Drink Coke ");}} public class Test6{public static void Main (string[] args) {new Son (). Show (); new son (). Eat ();}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Dark Horse Programmer--java Basic Learning Note 8

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.