"Core Java2 Volume I: Fundamentals" (original book 5th Edition) Mechanical Industry Press
I have seen the things about Java very fragmented, and I feel that I need to re-learn some basic knowledge of Java.
Chapter 1 Java Introduction
1. We believe that Java's advantages can be embodied in its application in new devices or new applications, rather than use to rewrite those existing applications. /// The so-called collar is ten years, which is roughly this; at the same time, it is meaningless.
2. Simplely said that object-oriented programming is to focus on data (equal to object) and a programming technology on the object interface. Take a carpenter as an example, a "object-oriented" carpenter will first care about the chair he wants, and then the tool to make this chair; and a "non-object-oriented" carpenter will first consider his tools. /// Only the way of thinking can improve the way you do. Terrible is: Tools that are often used to use objects, but use the process-oriented ideology
Chapter 2 Java Programming Environment
1, a simple and fun interface program, display .gif image
///Imageviewer.javaimport java.awt. *; Import java.awt.event. *; Import java.awt.Image. *; Import java.io. *; Import javax.swing. *;
public class ImageViewer {public static void main (String [] args) {JFrame frame = new ImageViewerFrame (); frame.setTitle ( "ImageViewer"); int w = 320, h = 400; Dimension ScreenSize = Toolkit.getDefaultToolkit (). GetScreensize (); Frame.setBounds ((in Screensize.width-W) / 2), (in Screensize.Height-H) / 2), W, H); Frame.setsize (W, H ); Frame.setdefaultcloseOperation (jframe.exit_on_close); frame.show ();}}
class ImageViewerFrame extends JFrame {public ImageViewerFrame () {// set menu JMenuBar menuBar = new JMenuBar (); setJMenuBar (menuBar); JMenu menu = new JMenu ( "File"); menuBar.add (menu); JMenuItem openItem = new JMenuItem ("Open"); menu.add (OpenItem); OpenItem.addActionListener (new fileopenlistener ()); jMenuItem exitItem = New jMenuitem ("exit"); menu.add (exitItem); exitItem.com.addActionListener (new activityListener () { public void actionPerformed (ActionEvent event) {System.exit (0);}}); // use a label to display the iamges label = new JLabel (); Container contentPane = getContentPane (); contentPane.add (label, "Center ");} private class FileOpenListener implements ActionListener {public void actionPerformed (ActionEvent evt) {// set up file chooser JFileChooser chooser = new JFileChooser (); chooser.setCurrentDirectory (new File (" ".)); // accept all files Ending with .gif chooser.setfilefilter (new javax.swing.filechooser.filefilter () {public boolean accept (file f) { Return F.GetName (). ToolowerCase (). endswith (". gif") || f.Indirectory ();} public string getdescription () {Return "gif images";}}; // show file chooser Dialog Int R = chooser.showopendialog (imageviewerframe.this); // if image file access, set it as icon of the label // Only now, we must implement our goal: Show pictures, the front is all prepared! IF (r == jfilechooser.Approve_option) {string name = chooser.getSelectedFile (). GetPath (); label.seticon (new imageicon (name);}}} private jlabel label;}
Chapter 3 Java Basic Programming Structure /// 略
Chapter 4 Objects and Types 1, OOP has three main features: a) Object behavior b) Object status c) identifier of the object // / better understanding
2, the relationship between the class A) Relying on Uses-AB) Polymerization HAS-AC) inherit IS-A
3, Note: The style we use is before, the field is before, and the field is in this way, and it may encourage the first-hearted interface, and the child is not concerned. /// Contains why the field is defined after the field is that! 4. Static fields and methods /// The fields here are attributes within the category a) If you define a field as static, there is only one such field in a class. /// My understanding: If you define a field with Static, this field belongs to the class, not an object. All objects will operate an address. B) Static variables are rare, but static constants are very common, as: public class math {// ... public static final double pi = 3.14159265358979323846; // ...} public class system {// ... public Static Final PrintStream Out = ...;} C) Static method We can regard a static method as a method without this parameter we can use a static method in both cases: (1) When a method does not need to access object status Time; (2) When a method only needs to access the static method in the class, there is another common use: Numberformat.getNumberinstance () This method is called factory method Factory Method
5. Java programming language always uses a pass value call. This means that the method gets a copy of all parameter values. In particular, the method cannot modify any parameter variable content delivered to it. /// First, mainly to this point, but then, the author does write this:
A method is impossible to change the parameters of the basic type, and the object parameters are different. We can easily - actually very common - ways to change a state of object parameters. the reason is simple. The method gets a copy of the object reference, while the original object variable and this copy point to the same object. ///Employee.javapublic class employee {string name; Double Salry;
Public Employee (String Name) {this.name = Name; this.salary = 1000.0;}
Public Double Getsalary () {Return Salary;
Public Static Void Triple (Employee EMP) {Emp.salary * = 3;
Public static void main (string [] args) {Employee Feng = New Employee ("feng"); Employee Yu = New Employee ("yu"); triple (feng); // Work Correctly! System.out.println (" Feng's Salry: " feng.getsalary ()); System.out.println (" Yu's Salry: " Yu.getsalary ());}} /// However, then the author said:
Some programmers claim that the Java programming language is called to the object. However, this understanding is wrong. Through an example: /// Note: The SWAP () in the example does not work as the expected public class Employee {String Name;
Public Employee (String Name) {this.name = name;} public string getname () {return name;}
Public Static Void Swap (Employee X, Employee Y) {Employee T = x; x = Y; Y = T;}
Public static void main (string [] args) {EMPLOYEE Feng = new Employee ("feng"); Employee Yu = New Employee ("yu"); swap (feng, yu); // cannot Work System.out.println FENG.GETNAME ()); system.out.println (yu.getname ());}}
/// For method parameters, the author is summarized as follows: a) Method cannot modify the basic type of parameter b) Method can change the status of the object parameters c) the method cannot cause an object parameter to point to a new object
6. The constructor is static initialization when the class is loaded by the first time. Static fields are initialized to 0, False or NULL unless explicit assignment. All static field initialization statements or initialization blocks are executed in sequence in order in the order in the class declaration. Below is a program without using the main method ///Hello.javapublic class hello {static {system.out.println ("Hello World!"); System.exit (0);}}
7. Document Note Note With / ** start, * / end, contain tags (@Author, @see, etc.) and free text (can contain HTML tag) Class Note: Generally put it after the Import statement, Class definition
8, class design skills 1) Private /// this will always start from practical, not from implementation, 2) Always initialize data // / Do not rely on default 3) Do not use too much in a class Basic type /// is better understood, use the Address class instead of the following fields in the Employee class: private string province; private string, private, private string; 4) Not all fields need your own field accessor And regulator setP () getP () 5) Use standard forms to class definitions: public, protected, private; each part: instance method, static method, instance field, static field 6) Too many duties to decompose 7) Make the names of the classes and methods reflect their responsibilities
Chapter 5 Inherited 1. Write a inheritance example: Employee's salary is just basic salary; Manager's salary is the basic salary bonus //managert.java
class Employee {public Employee (String name) {this.name = name;} public double getSalary () {return basicSalary;} public void showInfo () {System.out.println ( "name:" name); System.out .println ("Salary:" GetSalary ());
private String name = ""; private double basicSalary = 1000.0;} class Manager extends Employee {public Manager (String name) {super (name); // call the parent can use the super} public void setBonus (double b) {bonus = b;} public double getSalary () {setBonus (126.0); return super.getSalary () bonus;} private double bonus;} public class ManagerT {public static void main (String [] args) {Employee emp = new Employee ( "feng"); Emp.Showinfo (); Manager man1 = new manager ("wu"); man1.showinfo (); Employee Man2 = New Manager ("Song"); man2.showinfo ();}}
2, Final A class: can not be inherited; final A method: This method is not being set to Final has two reasons: a) efficiency. Dynamic binding is larger than the static binding overhead. B) Safe.
3, abstract class, Abstract Definition In order to be more clear, the class that specifies one or more abstract methods itself must be defined as Abstract in the Java language, the abstract method is a very important concept, in the "interface", will encounter a lot . /// java Access control symbol summary: private: Only for this class; public: It is visible to everything; protected: It is visible to this package and subclass; [Default]: Seeking this package /// // personal feeling best Don't use it province
To Be Continued