1, the difference between the two ways to create an object member variable assignment:
-
p1 = new Person ( "Zhang Tianyi", 23); each new re-create the equivalent of an object
-
Person p2 = new Person (); // empty argument constructor to create an object
-
Constructor: are initialized to
-
setXxx method
-
Modify the property value
-
These two methods in the development by setXxx some more, because the more flexible
-
class Demo3_Person {
public static void main(String[] args) {
Person p1 = new Person("张三",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());
}
}
class Person {
private String name; // 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;
}
}
2, create an object of step
Code [1]
class Demo4_Student {
public static void main(String[] args) {
Student s1 = new Student (); // empty configuration parameters
s1.setName ( "Joe Smith"); // set name
s1.setAge (23); // set the age
System.out.println ( "My name is:" + s1.getName () + ", my age is:" + s1.getAge ());
// getXxx () Gets the property value, you can print, can be assigned to other variables, do other operations
Student s2 = new Student ( "John Doe", 24);
s2.setName ( "King of Five");
s2.show (); // only for display attribute value
}
}
/*
* A: Case presentation
* Student categories:
* Member variables:
* name,age
* Construction method:
* No parameters, with two parameters
* Member method:
* getXxx()/setXxx()
* Show (): All members of the class output variable values
* B: to the member variable assignment:
* A: setXxx () method
* B: constructor
* C: mode output value of member variables:
* A: obtaining by splicing by getXxx (), respectively
* B: to get by calling the show () method
*/
class Student {
private String name; // Name
private int age; // Age
public Student () {} // null configuration parameters
public Student (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;
}
public void show() {
System.out.println ( "My name is:" + name + ", my age is:" + age);
}
}
[2] graphic
-
Student s = new Student();
-
Student.class loaded into memory
-
Student declare a reference type s
-
Create objects in heap memory,
-
To initialize the object attribute the default values
-
Properties for display initialization
-
Push constructor, assignment of object attributes, the constructor popping
-
The address of the object value is assigned to s
3. Create a rectangle Rectangle
class Test1_Rectangle { //Rectangle矩形
public static void main(String[] args) {
Rectangle r = new Rectangle(10,20);
System.out.println (r.getLength ()); // perimeter
System.out.println (r.getArea ()); // area
}
}
/*
* A: Case presentation
* Requirements:
* Define a rectangle class, method defined perimeter and area requirements,
* Then define a test class for testing.
analysis:
Member variables:
Wide width, high high
There are empty parameters arg constructor
Member method:
setXxx和getXxx
Find the circumference: getLength ()
Find the area: getArea ()
*/
class Rectangle {
private int width; //宽
private int high; //高
public Rectangle(){} //空参构造
public Rectangle(int width,int high) {
this.width = width; //有参构造
this.high = high;
}
public void setWidth(int width) {//设置宽
this.width = width;
}
public int getWidth() { //获取宽
return width;
}
public void setHigh(int high) { //设置高
this.high = high;
}
public int getHigh() { //获取高
return high;
}
public int getLength() { //获取周长
return 2 * (width + high);
}
public int getArea() { //获取面积
return width * high;
}
}