01_ Object Oriented (constructor Constructor Summary and formats)
* A: Overview and effect constructor
* The data (attribute) of an object is initialized
* B: Constructor Format Characteristics
* A: the same as the class name and method name (also consistent with the size of the class name)
* B: no return type, not even void
* C: No specific return value return;
configuration class Demo1_Constructor {// Constructor
public static void main(String[] args) {
Person p = new Person (); // create an object at a time, the system will help me to call the constructor
//p.Person (); // can not be called by an object constructor
p.show();
Person p2 = new Person (); // create the object again
p2.show();
}
}
/*
* A: Overview and effect constructor
* The data (attribute) of an object is initialized
* B: Constructor Format Characteristics
* A: the same as the class name and method name (also consistent with the size of the class name)
* B: no return type, not even void
* C: No specific return value return;
*/
class Person {
private String name;
private int age;
//Construction method
public Person() {
//System.out.println("Hello World!");
// return; // constructor is also a return statement, the format is return;
name = "John Doe";
age = 23;
}
public void show() {
System.out.println(name + "..." + age);
}
}
What is the role and format characteristics constructors are?
* A: the same as the class name and method name (also consistent with the size of the class name)
* B: no return type, not even void
* C: No specific return value return;
Thoughts: constructor can override it?
can
02_ object-oriented (overloaded constructor and precautions)
* A: Case presentation
* overloaded constructor
* Overload: The same method name, has nothing to do with the return value type (constructor has no return value), look at the list of parameters
* B: Constructor Note
* a: If we do not give the construction method, the system will automatically provide a constructor parameter no.
* B: if we give the construction method, the system will no longer provide a default constructor with no arguments.
* Note: This time, if we want to use the no-argument constructor, you must give yourself. Recommend never give their no-argument constructor
class Demo2_Person {
public static void main(String[] args) {
Person p1 = new Person();
p1.show();
System.out.println("---------------------");
Person p2 = new Person("张三",23);
p2.show();
System.out.println("---------------------");
Person p3 = new Person("李四",24);
p3.show();
}
}
/*
* A: Case presentation
* Overloaded constructor
* Overload: same method name, has nothing to do with the return value type (constructor does not return value), but only the parameter list
* B: constructor Notes
* A: If we do not give construction method, the system will automatically provide a constructor parameter no.
* B: if we give the construction method, the system will no longer provide a default constructor with no arguments.
* Note: This time, if we want to use the no-argument constructor, you must give yourself. Recommend never give their no-argument constructor
*/
class Person {
private String name; //姓名
private int age; // Age
public Person () {// null configuration parameters
System.out.println ( "empty argument constructor");
}
public Person(String name,int age) {
this.name = name;
this.age = age;
System.out.println ( "configuration have parameters");
}
public void show() {
System.out.println(name + "..." + age);
}
}
When using the constructor method, what precautions?
* A: If we do not give construction method, the system will automatically provide a constructor parameter no.
* B: if we give the construction method, the system will no longer provide a default constructor with no arguments.
* Note: This time, if we want to use the no-argument constructor, you must give yourself. Recommend never give their no-argument constructor
Thoughts: constructor, set method can give member variable assignment, the assignment of these two ways What is the difference?
(Difference to the member variable assignment of two ways) 03_ object-oriented
A: setXxx () method
* modify the attribute value
* B: constructor
* to initialize the object attribute
class Demo3_Person {
public static void main(String[] args) {
Person p1 = new Person ( "Joe Smith", 23);
// p1 = new Person ( "Zhang Tianyi", 23); // run this way to see the result looks like a renamed, in fact, the original object becomes garbage
System.out.println(p1.getName() + "..." + p1.getAge());
System.out.println("--------------------");
Person p2 = new Person (); // empty argument constructor to create an object
p2.setName ( "John Doe");
p2.setAge(24);
p2.setName ( "Li Gui");
System.out.println(p2.getName() + "..." + p2.getAge());
}
}
/*
Construction method
Initialized to the property
setXxx method
Modify the property value
These two methods in the development by setXxx some more, because the more flexible
*/
class Person {
private String name; //姓名
private int age; // Age
public Person () {// null configuration parameters
}
public Person (String name, int age) {// configuration parameters have
this.name = name;
this.age = age;
}
public void setName (String name) {// set name
this.name = name;
}
public String getName () {// get name
return name;
}
public void setAge (int age) {// set the age
this.age = age;
}
public int getAge () {// get the age
return age;
}
}