Javase constructor


Summary: 1.public class NoteBook {public void open () {system.out.println ( "")} public void close () {system.out.println ( "")} public static run () {system.out. the println ( "")}
public void useUsb (USB USB) {usb.cr (); usb.bc ()} here pass directly over the USB interface object using the principle of a multi-state, the implementation of which the main method who Biography of the implementation class objects, this is who transfer method
useUsb (keyboard), useUSB (mouse); USB as the interface here's class does not implement its own unique way so there is no need just polymorphic downcast, it intranceof do not need a judge;
2. constructor to create objects colleagues clear line object property values: modifier constructor name (and the name of the class where you want the same) (parameter list) {}; public Person (parameter) { }
3.public the Person (String name, int Age) {name = this.name; this.age} = Age added here this is a means of this type, to distinguish the name and pass over the age, this.name above represents the member variables of name;
this.name name = name of a member variable is assigned a value of parameter passing over the String name;
4. constructor The principle of the method using overloaded, because the name of the constructor of this class have the same name as it is necessary to distinguish by the different number of parameters of the order of a column type
5. In fact, we used before Dog dog = new Dog () is used to create the object constructor; new Dog () is to use the system default constructor,
6. constructor has no return value does not need to return void value, because it is to build an object, object creation after it disappeared (plus the return value of the method has become common);
the name of the method to construct and present like the class name; no specific construction method returns the value
7. compile java files when the system will automatically add the file to the default constructor, if we write a constructor, regardless of whether there is a return value of the system default constructor will disappear;
8. a class can have more than one constructor, directly through the constructor parameter of the parameter list are different; to distinguish, is overloaded methods
9. the configuration method may also be modified private, modified private the method of construction can not be other types of calls;
10. constructor: when the object is created (new object) is performed, and only once, no return value;
11. general procedure: when the object is created after performing call requires after the call was time to perform, it can be repeated calls, there is a return value
12. when a class inside a constructor did not sometimes; you default to fill an empty argument constructor but can not see See; that is, you can call Person p = new Person (); method new objects but when there are a class constructor parameters or whether there are
no parameters, the default constructor does not exist, can not be Person p = new Person (); unless they write about a modification of empty private constructor parameters, because private modified method can not be called outside the class is created, it can not be so
new objects, because the construction method of a private system comes disappears appears, there also did not create a constructor parameter, it can not be the object of new
13.Person p = new Person (); this is the default constructor call or an empty argument constructor, called the object is assigned the default value; \
Person p2 = new Person ( "Joe Smith", 12); this call is to have a reference structure, will be passed as a parameter to the constructor of the new object is assigned (name: Joe Smith, Age: 12)
14.this can call methods and properties of this class; by:.. this method name (); call this way the property name
15.this may call the constructor; this form (parameter list) is invoked; this (parameter list) can constructor this class is called the other constructors but must be placed on the first line of approach, because it needs to assign default values
16. the structure just will only assign objects when creating objects new objects; the latter can be; the object name. the setName ( "value") in the form of modified values;
time but this (name, age) with a call reference configuration; 17.public Student () {this ( " Joe Smith", 18) may be configured where constructor method call the actual value must be assigned, the this parameter can not be ( "John Doe", 18)}
and this () method is configured to be placed in the first row;
18. call the constructor for this class: this (parameter list); call the parent class constructor with no arguments: super (); call the parent class has a constructor parameters: super (parameter list)
19.super () is the parent class constructor; call Class attributes and methods:.. Super property name; Super method name
20. In each of the first row have a constructor hidden default super () null configuration parameters parent class, the subclass running empty reference construction will print out the constructor of the parent class
21. the empty argument constructor of the parent class privatized then the subclass can not inherit empty arg constructor of the parent class, subclass constructor this time on the need to assign the job
22 each construction Object class; a class of its own parent class structure, even if the parent also has its own parent structure
23. A method of operating a construction sequence:; constructor run subclass; parent class constructor first run or inherited default parent class constructor parameter space (super ()) the this () and super () does not exist
24 If that is you want to call this () and want to call super () then put the super () constructor into the subclass; then call this empty parameter configuration (); will be able to achieve that is to call this () and call Super ()
25.super () call the parent class constructor with no arguments; Super (parameter list) has to call the parent class reference configuration; Super (), and this () constructor should be placed in the first row;
26. final can be modified: a member (member variables + member method) class, class + and local variables (final modified variable is a constant, can not be changed after being assigned; (value of a variable can be changed)); can not be modified constructor
27 .final modified class can not be inherited but can inherit from other classes; final modified variables are called constants, a life-long assignment, it can only be assigned once; modified member variable assignments need before you create an object with a constructor or assignment
28. public student (String name, int age ) {super (name, age)}; public student (String name, int a ge) The method where the parameter list is passed over the main method of the received value
super (name, age); parameter list handle super class method is a value passed to the constructor student's parent class constructor; because this adjustment is super parent class method it is passed to the parent class; student method in the main method
is called in so that the method parameter list student and parent in order to receive value method pass over the main method does not matter, because the parent did not call method method student so it can not pass
So public student (String name, int age ) and the main transmission parameters related to the object and method calls only parent does not matter
29. The method of rewriting after inheriting it, the subclass inherits methods according to their need to change the content; overloading distinguished by different sequence number type parameter list of parameters occurring in this class which
30.final:final modified class can not be inherited, but others can be inherited; Final modified method of the conventional method can not be overridden inherited but may after adding final; final modified variables can not be changed once they are assigned
need to create a pre-assignment object when modified member variable; can also be assigned by the constructor
31 anonymous function: statements when you create an object only created but did not address assignment to a variable; that is, objects that are created with no name, can only be used once, each call is not the same object and the address is not the same as
ordinary objects created: Student stu = new Student ()
anonymous object creation: new Student ( )
so an anonymous object has no name; every time you create is not the same object, every time you create can only be used once

Guess you like

Origin www.cnblogs.com/hankai2735/p/11295265.html