Learn Note Java_ static _ Inherit 2014.7.12

Source: Internet
Author: User

First, static

1. Constructor:
Characteristics:
1, the function name and the class name are the same.
2. You do not have to define a return value type (and void is not the same thing, and a constructor does not have to define a return value type at all).
3, can not write return statement.
When an object is established, the corresponding constructor is called.
Constructor function: Can be used to initialize an object. (What we do in real life is just a feature that comes into existence.)

For example: A person is born to cry, an initial will cry, cry is the behavior of this person)
Small details of the constructor:
1. When a constructor is not defined in a class, the system defaults to a constructor that adds an empty parameter to the class.
2. When the constructor is defined in the class itself, the default constructor is gone.
Constructors and general functions differ in their notation.
There are also differences in execution:
1. The constructor is executed when the object is established.

Initializes the object.


The general method is to run the object call. is the ability to add objects to an object.


2, an object is established. The constructor executes only once.
The general method can be called multiple times by the object.


Methods are called by the object (before the static is learned)
When do you define a constructor function:
When an object is analyzed, it has some characteristics or behavior. Then the content is defined in the constructor.

2. Main function: public static void Main (string[] args)//arguments
The main function is a special function. Can be called by the JVM as the entry for the program.


The definition of the main function:
Public: Represents the maximum access permission for this function.


Static, which represents the main function as the class is loaded into existence.


void: There is no detailed return value for the main function.
Main: Not a keyword, but a special word that can be identified by the JVM.
(string[] arr): The function's number of references. The parameter type is an array, and the elements in the array are strings. An array of string types.
The main function is in fixed format: JVM recognition.


When the JVM calls the main function, it passes in the new String[0]:

Class Test{public static void Main (string[] args)//new string[] {System.out.println (args);  [Ljava.lang.string;@1175422system.out.println (args.length);  0}}//args can only receive two values//string[] args = new string[3];//string[] args = null;

3. When to use static?
To start with two sides:
Because statically decorated content has member variables and member functions.
When to define static variables (class variables):
When shared data is present in an object, the data is decorated statically.
The unique data in the object to be defined as non-static exists in heap memory.
When to define a static function:
Non-static data (unique data for the object) is not visited within the function. Then the function can be defined as static.

4, the static application:
Each application has common features that can be extracted, individually encapsulated, for reuse.
Although these tool methods can be used to create Arraytool objects, an array is manipulated. Some problems were found:
1. Objects are used to encapsulate data. However, the Arraytool object does not encapsulate the unique data.
2. Each method of manipulating an array is not practical to the unique data in the Arraytool object.
At this point consider, let the program more rigorous, is not necessary objects. It is possible to define the methods in Arraytool as static and call them directly through the class name.
After the methods are static. It is easy to use, but the class can be used by other programs to create objects. To be more rigorous, force the class to fail to establish an object. The ability to privatize the constructor by private Arraytool () {}

5. Static code block
Format:
Static
{
A run statement in a static code block.
}
Feature: Run as the class is loaded and run only once.

Used to initialize a class.

6, person p = new person ("Zhangsan", 20);
What did the sentence do:
1, because new is used to Person.class, the Person.class file will be found and loaded into memory first.
2. Run the static block of code in the class, assuming there is one. Initializes the Person.class class.
3. Open up space in heap memory and allocate memory address.
4. Establish the unique properties of the object in heap memory and initialize it by default.
5, the property is displayed initialization.
6, the object is constructed code block initialization.


7. Initialize the corresponding constructor function for the object.


8. Assign the memory address to the P variable in the stack memory.

7. Design mode:
The most effective way to solve a problem.
23 Design Patterns in Java:
Singleton design pattern: Resolves a class that only has one object in memory.
To keep the object unique:
1, in order to avoid other programs to set up such objects too much, first prohibit other programs to establish such objects.
2. In order to allow other programs to access this class of objects. Just in this class, you define an object yourself.
3, in order to facilitate other procedures on their own definition of the object of access, to provide some access to the external way.
How do these three steps show up in code:
1. Privatization of the constructor function.
2. Create an object of this class in the class.
3. Provides a way to get to the object.
How to describe how things should be described, and how to describe the narrative. When it is necessary to ensure that the object of the thing is unique in memory. Just add the three steps above and you can.
A hungry man-lazy try Null (interview lazy asked the most, ask how to solve the problem. Multithreading has also been tested to synchronized. High ratio of compound type)
Remember the principle: Define a singleton. It is recommended to use a hungry man style.

Ii. inheritance

1, Inheritance Overview:

Code: Test.java

Class person{string Name;int age;} Class Student extends Person{void study () {}}class Worker extends person{void work () {}}class test{public static void Main (S Tring[] args) {Student s = new Student (); s.name = "Zhangsan"; System.out.println (s.name);  Have to Zhangsan   Hey, why now without ToString can normal output ah? }}

Code: Extendsdemo.java

class person//The general description of the students and individuals to extract the narrative, separate descriptive narrative. It is only necessary for students and individuals to have a relationship with the class describing the narrative individually (extends). You can have {String name;int age;} /* Inherit: 1, improve the reusability of the code.

2, let the class and the class has a relationship.

With this relationship, there is a polymorphic nature.

Note: Do not inherit from simplifying the code in order to get the functionality of other classes.

Must be a relationship between the class and the class (Student is a person.) talent enough to inherit.

For example: Class C//c is to find the common content of a and B, extract {void Demo1 () {}}class A extends c{//void demo1 () {}void Demo2 () {}}class B extends c{//void Demo1 () {}//a and B all have demo1 (), and if B inherits a, a, a, a, and a demo1 method is obtained. But at the same time also got Demo2 (), think B should get Demo2 () it. No. Then there is no inheritance relationship between them.

Then they do have the same demo1 () Yes, we'll add a C to inherit C. void Demo3 () {}}*/class Student extends person/*keywordextends allows students to have a point relationship with person. The student is the subclass of person, and the person is the child of the student. Person is also called a superclass, a base class. In the Java language: Java only supports single inheritance and does not support multiple inheritance (that is, a class can inherit multiple classes.) This is not a serious one to say).

Java does not support the advantage of multiple inheritance is to optimize the C + + part, due to C + + support.

For example: Student inherits the person class and cannot inherit other classes. Because multiple inheritance easy poses a security risk: when the same functionality is defined in multiple parent classes, the subclass object is not sure which to execute when the feature is different. However, Java retains such a mechanism, and uses a form of representation to complete the expression: multi-implementation (Java to multi-inheritance improvement).

Java supports multilayer inheritance (c Inherits B.) b inherit a grandchild of three generations). is an inheritance system. How to use a function in an inheritance system: To use the system, first refer to the description of the system parent class, because the parent class is defined in the system of the common functions. By understanding the common features, you can know the basic functions of the system. Then the system has been able to use the basic.

Then in the verbose call, create the object of the most child class. Why: First, it is possible that the parent cannot create an object (such as an abstract class), and the second is to create a subclass object that can use many other functions, including the primary and the unique. A simple sentence: Check the parent class feature to create a subclass object usage feature.

Class a{void Show () {System.out.println ("A");}} Class b{void Show () {System.out.println ("B");}} Class C extends A, b{}c C = new extends A, b;c.show (); Q: The print is a or B ah, is not sure AH.

*/{void Study () {System.out.println ("good Study");}} Class Worker extends person {void work () {System.out.println (' good work ');}} Class Extendsdemo{public static void Main (string[] args) {Student s = new Student (); s.name = "Zhangsan";}}

There is not only a relationship between an object and an object, or between a class and a class, or between things.
Aggregation: Has a (typically with more aggregation relationships than inheritance)
1, Aggregation: (the player is one of the team.) There are players in the team)
2. Combination: Things are more closely connected (the hand is a part of the human body.) The heart is a part of the human body)

2, the characteristics of the variables in the child parent class:

Code: Extendsdemo2.java

Class Fu{int NUM1 = 4;} Class Zi extends Fu{int num2 = 5;} Class Extendsdemo2{public static void Main (string[] args) {Zi z = new Zi (); System.out.println (Z.num1 + "..." + z.num2);  Get 4 ..... 5}}

Code: Extendsdemo2.java

Class Fu{int num = 4;} Class Zi extends Fu{int num = 5;} Class Extendsdemo2{public static void Main (string[] args) {Zi z = new Zi (); System.out.println (Z.num + "..." + z.num);  Get 5 ..... 5  subclasses are the same as the variables of the parent class. Calling the subclass}}

Code: Extendsdemo2.java

Class Fu{int num = 4;} Class Zi extends Fu{int num = 5;void Show () {System.out.println (num);  This is omitted before Num. (This represents a reference to this class of objects, visiting the member variables and member methods of this class)}}class extendsdemo2{public static void Main (string[] args) {Zi z = new Zi () ; Z.show ();  Get 5}}

Code: Extendsdemo2.java

Class Fu{int num = 4;} Class Zi extends Fu{//int num = 5;  Let's say you look at this sentence. void Show () {System.out.println (num);  The super is omitted here.}} Class Extendsdemo2{public static void Main (string[] args) {Zi z = new Zi (); Z.show ();  Get 4}}

Code: Extendsdemo2.java

/* After the child parent class appears, the class member features: Class Members: 1, variable 2, function 3, constructor 1, the variable assumes that a non-private member variable of the same name appears in the subclass, the subclass to access the variables in this class, with the this subclass to access the name of the parent class variable, The use of supersuper and the use of this is almost consistent. This represents a reference to this class of objects. Super represents a reference to a parent class object. */class fu{int num = 4;} Class Zi extends Fu{//int num = 5;  Suppose to look at the sentence with The Void Show () {System.out.println (this.num);  This is added here.   Num or 4}}class extendsdemo2{public static void Main (string[] args) {Zi z = new Zi (); Z.show ();  Get 4}}

Code: Extendsdemo2.java

class fu{private int num = 4;public void setnum (int num) {this.num = num;} public int Getnum () {return this.num;}} Class Zi extends Fu{void show () {System.out.println (num);  Num is privatized, how can I access num? }}class extendsdemo2{public static void Main (string[] args) {Zi z = new Zi (); Z.show ();}}

3, the characteristics of the function in the child parent class:

Code: Extendsdemo3.java

Class Fu{void Show1 () {System.out.println ("Fu Show");}} Class Zi extends Fu{void Show2 () {System.out.println ("Zi Show");}} Class Extendsdemo3{public static void Main (string[] args) {Zi z = new Zi (); Z.show1 ();  Fu Showz.show2 ();  Get Zi Show}}

Learn Note Java_ static _ Inherit 2014.7.12

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.